-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper_test.go
69 lines (66 loc) · 1.43 KB
/
keeper_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
package keeper
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestExecWithContext(t *testing.T) {
tests := []struct {
name string
timeout time.Duration
f func() (interface{}, error)
want interface{}
wantErr error
}{
{
name: "timeout",
timeout: 400 * time.Millisecond,
f: func() (interface{}, error) {
time.Sleep(1 * time.Second)
return []int{1, 2, 3}, nil
},
want: nil,
wantErr: context.DeadlineExceeded,
},
{
name: "finish in time",
timeout: 400 * time.Millisecond,
f: func() (interface{}, error) {
return []int{1, 2, 3}, nil
},
want: []int{1, 2, 3},
wantErr: nil,
},
{
name: "finish in time 2",
timeout: 400 * time.Millisecond,
f: func() (interface{}, error) {
time.Sleep(300 * time.Millisecond)
return []int{1, 2, 3}, nil
},
want: []int{1, 2, 3},
wantErr: nil,
},
{
name: "fail func",
timeout: 400 * time.Millisecond,
f: func() (interface{}, error) {
time.Sleep(300 * time.Millisecond)
return nil, errors.New("error")
},
want: nil,
wantErr: errors.New("error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), tt.timeout)
defer cancel()
got, err := ExecWithContext(ctx, tt.f)
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.wantErr, err)
})
}
}