-
Notifications
You must be signed in to change notification settings - Fork 12
/
context_test.go
58 lines (55 loc) · 1.3 KB
/
context_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
package gohalt
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestContext(t *testing.T) {
cctx, cancel := context.WithCancel(context.Background())
cancel()
testerr := errors.New("test")
ctx := WithParams(
context.Background(),
time.Now(),
1,
0,
"",
nil,
nil,
)
table := map[string]struct {
ctx context.Context
err error
}{
"Context with throttler should be done on throttling": {
ctx: WithThrottler(context.Background(), tmock{aerr: testerr}, ms1_0),
err: testerr,
},
"Context with throttler should be done on throttling after": {
ctx: WithThrottler(context.Background(), NewThrottlerAfter(1), ms1_0),
err: ErrorThreshold{Throttler: "after", Threshold: strpair{current: 3, threshold: 1}},
},
"Context with throttler should be done with canceled context": {
ctx: WithThrottler(cctx, tmock{}, ms1_0),
err: cctx.Err(),
},
"Context with throttler should not be done after timeout": {
ctx: WithThrottler(ctx, tmock{}, ms1_0),
},
}
for tname, tcase := range table {
t.Run(tname, func(t *testing.T) {
tick := time.NewTicker(ms3_0)
defer tick.Stop()
select {
case <-tcase.ctx.Done():
err := tcase.ctx.Err()
assert.Equal(t, tcase.err, err)
case <-tick.C:
assert.Nil(t, tcase.err)
}
})
}
}