-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools_test.go
52 lines (50 loc) · 948 Bytes
/
tools_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
package silly_kits
import (
"context"
"errors"
"testing"
"time"
)
func TestFind(t *testing.T) {
idx, val, err := Find([]int{0, 1, 2, 3, 4, 5}, func(i int) bool {
return i%2 == 1
})
if idx != 1 || val != 1 || err != nil {
t.Logf("idx=%d val= %d err=%s", idx, val, err)
t.Fail()
}
}
func TestForEach(t *testing.T) {
if err := ForEach([]int{1, 2, 3, 10}, func(i int) error {
if i > 5 {
return errors.New("unknown")
}
return nil
}); err == nil {
t.Fail()
}
}
func TestRetry(t *testing.T) {
var c = 0
val, err := Retry(func() (int, error) {
c += 1
if c < 3 {
return -1, errors.New("unknown")
}
return 1023, nil
}, 3, time.Second*3)
if err != nil || val != 1023 {
t.Fail()
}
}
func TestWaitAll(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
fn1 := func(ctx context.Context) {
<-ctx.Done()
}
go func() {
time.Sleep(time.Second * 5)
cancel()
}()
WaitAll(ctx, fn1, fn1)
}