-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout_test.go
68 lines (65 loc) · 1.11 KB
/
timeout_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
package pp
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestTimeout(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
run func(t *testing.T)
}{
{
name: "empty",
run: func(t *testing.T) {
require.NotPanics(t, func() {
resp := Timeout(0)
require.Empty(t, resp)
})
},
},
{
name: "dont timeout",
run: func(t *testing.T) {
a := 0
f := func(_ context.Context) (err error) {
a++
return
}
steps := Timeout(time.Second,
f, f, f,
)
err := Run(ctx, steps...)
require.NoError(t, err)
require.Equal(t, 3, a)
},
},
{
name: "timeout",
run: func(t *testing.T) {
a := 0
f := func(ctx context.Context) (err error) {
time.Sleep(400 * time.Millisecond)
if ctx.Err() != nil {
return
}
a++
return
}
steps := Timeout(time.Second,
f, f, f,
)
err := Run(ctx, steps...)
require.Error(t, err)
require.Equal(t, 2, a)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.run(t)
})
}
}