-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext_test.go
More file actions
133 lines (106 loc) · 2.74 KB
/
context_test.go
File metadata and controls
133 lines (106 loc) · 2.74 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package glock
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestContextValues(t *testing.T) {
t.Run("value can be added to and retrieved from context", func(t *testing.T) {
t.Parallel()
clock := NewMockClock()
ctx := context.Background()
ctx = WithContext(ctx, clock)
ctxClock := FromContext(ctx)
assert.Same(t, clock, ctxClock)
})
t.Run("default to returning real clock", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctxClock := FromContext(ctx)
assert.NotNil(t, ctxClock)
assert.IsType(t, &realClock{}, ctxClock)
})
}
func TestContext(t *testing.T) {
t.Parallel()
testContextWithTimeout(t, func(state *contextTestState) {})
}
func TestContextCancelParent(t *testing.T) {
t.Parallel()
testContextWithTimeout(t, func(state *contextTestState) {
state.err1 = context.Canceled
state.err2 = context.Canceled
state.err3 = context.Canceled
state.cancel1()
})
}
func TestContextCancel(t *testing.T) {
t.Parallel()
testContextWithTimeout(t, func(state *contextTestState) {
state.err2 = context.Canceled
state.err3 = context.Canceled
state.cancel2()
})
}
func TestContextCancelChild(t *testing.T) {
t.Parallel()
testContextWithTimeout(t, func(state *contextTestState) {
state.err3 = context.Canceled
state.cancel3()
})
}
func TestContextDeadline(t *testing.T) {
t.Parallel()
testContextWithTimeout(t, func(state *contextTestState) {
state.err2 = context.DeadlineExceeded
state.err3 = context.DeadlineExceeded
state.clock.BlockingAdvance(time.Second)
})
}
type contextTestState struct {
clock *MockClock
ctx1 context.Context
ctx2 context.Context
ctx3 context.Context
cancel1 context.CancelFunc
cancel2 context.CancelFunc
cancel3 context.CancelFunc
err1 error
err2 error
err3 error
}
func testContextWithTimeout(t *testing.T, f func(state *contextTestState)) {
clock := NewMockClock()
ctx := context.Background()
ctx1, cancel1 := context.WithCancel(ctx)
ctx2, cancel2 := ContextWithTimeout(ctx1, clock, time.Second)
ctx3, cancel3 := context.WithCancel(ctx2)
defer cancel1()
defer cancel2()
defer cancel3()
state := &contextTestState{
clock: clock,
ctx1: ctx1,
ctx2: ctx2,
ctx3: ctx3,
cancel1: cancel1,
cancel2: cancel2,
cancel3: cancel3,
}
assertDoneAndErr(t, ctx1, nil)
assertDoneAndErr(t, ctx2, nil)
assertDoneAndErr(t, ctx3, nil)
f(state)
assertDoneAndErr(t, ctx1, state.err1)
assertDoneAndErr(t, ctx2, state.err2)
assertDoneAndErr(t, ctx3, state.err3)
}
func assertDoneAndErr(t *testing.T, ctx context.Context, err error) {
if err != nil {
eventually(t, structChanReceives(ctx.Done()))
assert.Equal(t, ctx.Err(), err)
return
}
consistently(t, structChanDoesNotReceive(ctx.Done()))
}