-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
deprecated_test.go
103 lines (81 loc) · 2.09 KB
/
deprecated_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
package breaker_test
import (
"context"
"os"
"testing"
"time"
. "github.com/kamilsk/breaker"
)
func TestMultiplexTwo(t *testing.T) {
t.Parallel()
br := MultiplexTwo(
BreakByDeadline(time.Now().Add(-delta)),
BreakByTimeout(time.Hour),
)
checkBreakerIsReleased(t, br)
}
func TestMultiplexThree(t *testing.T) {
t.Parallel()
br := MultiplexThree(
BreakByDeadline(time.Now().Add(-delta)),
BreakBySignal(os.Kill),
BreakByTimeout(time.Hour),
)
checkBreakerIsReleased(t, br)
}
func TestWithContext(t *testing.T) {
t.Parallel()
t.Run("cancel context", func(t *testing.T) {
t.Parallel()
timeout := time.Hour
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
br, ctx := WithContext(ctx)
checkBreakerIsNotReleased(t, br)
cancel()
checkBreakerIsReleasedFast(t, br)
checkContextIsDone(t, ctx)
})
t.Run("propagate timeout", func(t *testing.T) {
t.Parallel()
timeout := 5 * delta
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
br, ctx := WithContext(ctx)
start := time.Now()
<-br.Done()
checkDuration(t, start.Add(timeout), time.Now())
checkBreakerIsReleasedFast(t, br)
checkContextIsDone(t, ctx)
})
t.Run("deadline has already passed", func(t *testing.T) {
t.Parallel()
timeout := -delta
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
br, ctx := WithContext(ctx)
checkBreakerIsReleasedFast(t, br)
checkContextIsDone(t, ctx)
})
t.Run("close breaker", func(t *testing.T) {
t.Parallel()
timeout := time.Hour
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
br, ctx := WithContext(ctx)
checkBreakerIsNotReleased(t, br)
br.Close()
checkBreakerIsReleasedFast(t, br)
checkContextIsDone(t, ctx)
})
t.Run("close breaker multiple times", func(t *testing.T) {
t.Parallel()
timeout := time.Hour
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
br, ctx := WithContext(ctx)
checkBreakerIsNotReleased(t, br)
closeBreakerConcurrently(br, times)
checkBreakerIsReleasedFast(t, br)
checkContextIsDone(t, ctx)
})
}