-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre_test.go
75 lines (68 loc) · 1.15 KB
/
re_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
72
73
74
75
package re
import (
"errors"
"testing"
"time"
)
func TestTry(t *testing.T) {
// means try task 5 times in a second
const (
period = time.Millisecond * 200
timeout = time.Second
)
task := func(currHit, targetHit int) error {
if currHit <= targetHit {
return errors.New("not yet")
}
return nil
}
var hit int
tests := []struct {
name string
task func() error
wantErr bool
}{
{
name: "nil task",
task: nil,
wantErr: true,
},
{
name: "no err func",
task: func() error { return nil },
wantErr: false,
},
{
name: "create err 4 times",
task: func() error {
hit++
return task(hit, 4)
},
wantErr: false,
},
{
name: "create err 5 times",
task: func() error {
hit++
return task(hit, 5)
},
wantErr: false,
},
{
name: "create err 6 times",
task: func() error {
hit++
return task(hit, 6)
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hit = 0
if err := Try(tt.task, period, timeout); (err != nil) != tt.wantErr {
t.Errorf("Try() err: %v, wantErr %v", err, tt.wantErr)
}
})
}
}