-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_test.go
160 lines (139 loc) · 3.57 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package scope
import (
"fmt"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestContext(t *testing.T) {
Convey("Context data", t, func() {
Convey("Local Get/GetOK/Set", func() {
ctx := New()
So(ctx.Get("test"), ShouldBeNil)
val, ok := ctx.GetOK("test")
So(ok, ShouldBeFalse)
So(val, ShouldBeNil)
ctx.Set("test", "value")
So(ctx.Get("test"), ShouldEqual, "value")
val, ok = ctx.GetOK("test")
So(ok, ShouldBeTrue)
So(val, ShouldEqual, "value")
})
Convey("Copy-on-write", func() {
root := New()
child := root.Fork()
gchild := child.Fork()
root.Set("a", 1)
root.Set("b", 2)
root.Set("c", 3)
So(gchild.Get("a"), ShouldEqual, 1)
child.Set("b", 4)
root.Set("a", 5)
So(child.Get("a"), ShouldEqual, 1)
So(child.Get("b"), ShouldEqual, 4)
// gchild should still refer to root's data
So(gchild.Get("a"), ShouldEqual, 5)
So(gchild.Get("b"), ShouldEqual, 2)
})
})
Convey("Context lifecycle", t, func() {
Convey("Local Cancel/Terminate/Err/Done", func() {
ctx := New()
So(ctx.Err(), ShouldBeNil)
select {
case <-ctx.Done():
t.Error("ctx.Done() should not be ready")
default:
}
ctx.Cancel()
<-ctx.Done()
So(ctx.Err(), ShouldEqual, Cancelled)
})
Convey("Termination propagates down tree", func() {
root := New()
child := root.Fork()
gchild := child.Fork()
sib := root.Fork()
err := fmt.Errorf("test error")
child.Terminate(err)
<-gchild.Done()
So(gchild.Err(), ShouldEqual, err)
<-child.Done()
So(child.Err(), ShouldEqual, err)
So(root.Err(), ShouldBeNil)
So(sib.Err(), ShouldBeNil)
root.Cancel()
So(sib.Err(), ShouldEqual, Cancelled)
So(root.Err(), ShouldEqual, Cancelled)
So(child.Err(), ShouldEqual, err)
So(gchild.Err(), ShouldEqual, err)
})
})
Convey("Shared wait group", t, func() {
root := New()
child := root.Fork()
gchild := child.Fork()
sib := root.Fork()
for _, ctx := range []Context{child, gchild, sib} {
So(ctx.WaitGroup(), ShouldEqual, root.WaitGroup())
}
})
Convey("Breakpoints", t, func() {
ctx := New()
f := func() error { return ctx.Check("test") }
Convey("Unlatched", func() {
So(f(), ShouldBeNil)
})
Convey("Latched", func() {
ctrl := ctx.Breakpoint("test")
ch := make(chan error)
go func() { ch <- f() }()
testErr := fmt.Errorf("test error")
<-ctrl
ctrl <- testErr
So(<-ch, ShouldEqual, testErr)
})
Convey("Cancellation", func() {
Convey("Before synchronization", func() {
root := New()
root.Breakpoint("test")
child := root.Fork()
ready := make(chan bool)
ch := make(chan error)
go func() {
ready <- true
ch <- child.Check("test")
}()
<-ready
root.Cancel()
So(<-ch, ShouldEqual, Cancelled)
})
Convey("After synchronization", func() {
root := New()
ctrl := root.Breakpoint("test")
child := root.Fork()
ch := make(chan error)
go func() { ch <- child.Check("test") }()
<-ctrl
root.Cancel()
So(<-ch, ShouldEqual, Cancelled)
})
})
Convey("Timeout", func() {
Convey("Timeout expires", func() {
start := time.Now()
ctx := New().ForkWithTimeout(10 * time.Millisecond)
<-ctx.Done()
So(time.Now().Sub(start), ShouldBeGreaterThanOrEqualTo, 10*time.Millisecond)
So(ctx.Err(), ShouldEqual, TimedOut)
})
Convey("Context terminates before expiration", func() {
ctx := New().ForkWithTimeout(10 * time.Millisecond)
time.Sleep(5 * time.Millisecond)
ctx.Terminate(nil)
time.Sleep(10 * time.Millisecond)
So(ctx.Err(), ShouldBeNil)
})
})
})
}