-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_test.go
104 lines (91 loc) · 1.78 KB
/
timer_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package timer
import (
"errors"
"testing"
"time"
)
// testContext - test context
type testContext struct {
Ch chan struct{}
err error
}
// newTestContext - constructor
func newTestContext() *testContext {
return &testContext{
Ch: make(chan struct{}),
}
}
// Deadline ...
func (tc *testContext) Deadline() (deadline time.Time, ok bool) {
return time.Time{}, false
}
// Done ...
func (tc *testContext) Done() <-chan struct{} {
return tc.Ch
}
// Err ...
func (tc *testContext) Err() error {
return tc.err
}
// Value ...
func (tc *testContext) Value(_ interface{}) interface{} {
return nil
}
// SetError ...
func (tc *testContext) SetError(err error) {
tc.err = err
}
func TestTimer(t *testing.T) {
type args struct {
c *testContext
d time.Duration
e error
f func(ctx *testContext)
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "success",
args: args{
c: newTestContext(),
d: 100 * time.Millisecond,
e: nil,
f: nil,
},
wantErr: false,
},
{
name: "success_done",
args: args{
c: newTestContext(),
d: 100 * time.Second,
e: errors.New("test error"),
f: func(ctx *testContext) {
close(ctx.Ch)
},
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt // escape: Using the variable on range scope `tt` in loop literal.
t.Run(tt.name, func(t *testing.T) {
if tt.args.e != nil {
tt.args.c.SetError(tt.args.e)
}
if tt.args.f != nil {
tt.args.f(tt.args.c)
if err := RunWithContext(tt.args.c, tt.args.d); (err != nil) != tt.wantErr {
t.Errorf("RunWithContext() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
if err := Run(tt.args.d); (err != nil) != tt.wantErr {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}