-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
282 lines (209 loc) · 4.3 KB
/
example_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package state
import (
"context"
"errors"
"fmt"
"log"
"time"
)
func ExampleWithShutdown() {
st := func() State {
st, tail := WithShutdown()
ticker := time.NewTicker(1 * time.Second)
go func() {
for {
select {
// receive a shutdown signal
case <-tail.End():
ticker.Stop()
// send a signal that the shutdown is complete
tail.Done()
return
case <-ticker.C:
// some job
}
}
}()
return st
}()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := st.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
}
func ExampleWithShutdown_dependency() {
runJob := func(name string) State {
st, tail := WithShutdown()
go func() {
<-tail.End()
fmt.Println("shutdown " + name)
tail.Done()
}()
return st
}
st1 := runJob("job 1")
st2 := runJob("job 2")
st3 := runJob("job 3")
// st3 will be shut down first, then st2, then st1
st := st1.DependsOn(st2).DependsOn(st3)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := st.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
// Output: shutdown job 3
// shutdown job 2
// shutdown job 1
}
func ExampleWithShutdown_dependencyWrap() {
st1 := func() State {
st, tail := WithShutdown()
go func() {
<-tail.End()
fmt.Println("shutdown job 1")
tail.Done()
}()
return st
}()
// st1 will be shut down first, then st2
st2, tail := WithShutdown(st1)
go func() {
<-tail.End()
fmt.Println("shutdown job 2")
tail.Done()
}()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := st2.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
// Output: shutdown job 1
// shutdown job 2
}
func ExampleWithWait() {
st := func() State {
st, tail := WithWait()
for i := 1; i <= 3; i++ {
tail.Add(1)
go func(i int) {
<-time.After(time.Duration(i) * 50 * time.Millisecond)
fmt.Printf("job %d ended\n", i)
tail.Done()
}(i)
}
return st
}()
// blocks until state's WaitGroup counter is zero
st.Wait()
// Output: job 1 ended
// job 2 ended
// job 3 ended
}
func ExampleWithError() {
st := func() State {
return WithError(errors.New("error"))
}()
if err := st.Err(); err != nil {
fmt.Println(err)
}
// Output: error
}
func ExampleWithErrorGroup() {
st := func() State {
st, tail := WithErrorGroup()
go func() {
tail.Error(errors.New("error"))
}()
return st
}()
time.Sleep(100 * time.Millisecond)
if err := st.Err(); err != nil {
fmt.Println(err)
}
// Output: error
}
func ExampleWithAnnotation() {
st := func() State {
st, tail := WithErrorGroup()
go func() {
tail.Error(errors.New("error"))
}()
return WithAnnotation("my job", st)
}()
time.Sleep(100 * time.Millisecond)
if err := st.Err(); err != nil {
fmt.Println(err)
}
// Output: my job: error
}
func ExampleWithAnnotation_shutdown() {
st := func() State {
st, tail := WithShutdown()
go func() {
<-tail.End()
<-time.After(1 * time.Second)
tail.Done()
}()
return WithAnnotation("my job", st)
}()
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
err := st.Shutdown(ctx)
if err != nil {
fmt.Println(err)
}
// Output: my job: timeout expired
}
func ExampleMerge() {
runJob := func(name string, duration time.Duration) State {
st, tail := WithShutdown()
go func() {
<-tail.End()
<-time.After(duration)
fmt.Println("shutdown " + name)
tail.Done()
}()
return st
}
st1 := runJob("job 1", 50*time.Millisecond)
st2 := runJob("job 2", 100*time.Millisecond)
st3 := runJob("job 3", 150*time.Millisecond)
st := Merge(st1, st2, st3)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := st.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
// Output: shutdown job 1
// shutdown job 2
// shutdown job 3
}
func ExampleWithValue() {
type key int
var greetingKey key
getGreeting := func(st State) (c chan string, ok bool) {
value := st.Value(greetingKey)
if value != nil {
return value.(chan string), true
}
return
}
st := func() State {
c := make(chan string)
st := WithValue(greetingKey, c)
go func() {
c <- "hi"
}()
return st
}()
c, ok := getGreeting(st)
if ok {
fmt.Println(<-c)
}
// Output: hi
}