forked from grab/async
/
repeat_test.go
71 lines (58 loc) · 1.3 KB
/
repeat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2019 Grabtaxi Holdings PTE LTE (GRAB), All rights reserved.
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
package async
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRepeat(t *testing.T) {
assert.NotPanics(t, func() {
out := make(chan bool, 1)
task := Repeat(context.TODO(), time.Nanosecond*10, func(context.Context) (any, error) {
out <- true
return nil, nil
})
<-out
v := <-out
assert.True(t, v)
task.Cancel()
})
}
func ExampleRepeat() {
out := make(chan bool, 1)
task := Repeat(context.TODO(), time.Nanosecond*10, func(context.Context) (any, error) {
out <- true
return nil, nil
})
<-out
v := <-out
fmt.Println(v)
task.Cancel()
// Output:
// true
}
/*
func TestRepeatFirstActionPanic(t *testing.T) {
assert.NotPanics(t, func() {
task := Repeat(context.TODO(), time.Nanosecond*10, func(context.Context) (any, error) {
panic("test")
})
task.Cancel()
})
}
func TestRepeatPanic(t *testing.T) {
assert.NotPanics(t, func() {
var counter int32
task := Repeat(context.TODO(), time.Nanosecond*10, func(context.Context) (any, error) {
atomic.AddInt32(&counter, 1)
panic("test")
})
for atomic.LoadInt32(&counter) <= 10 {
}
task.Cancel()
})
}
*/