Source file
src/context/example_test.go
1
2
3
4
5 package context_test
6
7 import (
8 "context"
9 "errors"
10 "fmt"
11 "net"
12 "sync"
13 "time"
14 )
15
16 var neverReady = make(chan struct{})
17
18
19
20
21 func ExampleWithCancel() {
22
23
24
25
26
27 gen := func(ctx context.Context) <-chan int {
28 dst := make(chan int)
29 n := 1
30 go func() {
31 for {
32 select {
33 case <-ctx.Done():
34 return
35 case dst <- n:
36 n++
37 }
38 }
39 }()
40 return dst
41 }
42
43 ctx, cancel := context.WithCancel(context.Background())
44 defer cancel()
45
46 for n := range gen(ctx) {
47 fmt.Println(n)
48 if n == 5 {
49 break
50 }
51 }
52
53
54
55
56
57
58 }
59
60
61
62 func ExampleWithDeadline() {
63 d := time.Now().Add(shortDuration)
64 ctx, cancel := context.WithDeadline(context.Background(), d)
65
66
67
68
69 defer cancel()
70
71 select {
72 case <-neverReady:
73 fmt.Println("ready")
74 case <-ctx.Done():
75 fmt.Println(ctx.Err())
76 }
77
78
79
80 }
81
82
83
84 func ExampleWithTimeout() {
85
86
87 ctx, cancel := context.WithTimeout(context.Background(), shortDuration)
88 defer cancel()
89
90 select {
91 case <-neverReady:
92 fmt.Println("ready")
93 case <-ctx.Done():
94 fmt.Println(ctx.Err())
95 }
96
97
98
99 }
100
101
102
103 func ExampleWithValue() {
104 type favContextKey string
105
106 f := func(ctx context.Context, k favContextKey) {
107 if v := ctx.Value(k); v != nil {
108 fmt.Println("found value:", v)
109 return
110 }
111 fmt.Println("key not found:", k)
112 }
113
114 k := favContextKey("language")
115 ctx := context.WithValue(context.Background(), k, "Go")
116
117 f(ctx, k)
118 f(ctx, favContextKey("color"))
119
120
121
122
123 }
124
125
126
127 func ExampleAfterFunc_cond() {
128 waitOnCond := func(ctx context.Context, cond *sync.Cond, conditionMet func() bool) error {
129 stopf := context.AfterFunc(ctx, func() {
130
131
132
133 cond.L.Lock()
134 defer cond.L.Unlock()
135
136
137
138
139
140
141
142
143
144 cond.Broadcast()
145 })
146 defer stopf()
147
148
149
150
151 for !conditionMet() {
152 cond.Wait()
153 if ctx.Err() != nil {
154 return ctx.Err()
155 }
156 }
157
158 return nil
159 }
160
161 cond := sync.NewCond(new(sync.Mutex))
162
163 var wg sync.WaitGroup
164 for range 4 {
165 wg.Go(func() {
166 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
167 defer cancel()
168
169 cond.L.Lock()
170 defer cond.L.Unlock()
171
172 err := waitOnCond(ctx, cond, func() bool { return false })
173 fmt.Println(err)
174 })
175 }
176 wg.Wait()
177
178
179
180
181
182
183 }
184
185
186
187 func ExampleAfterFunc_connection() {
188 readFromConn := func(ctx context.Context, conn net.Conn, b []byte) (n int, err error) {
189 stopc := make(chan struct{})
190 stop := context.AfterFunc(ctx, func() {
191 conn.SetReadDeadline(time.Now())
192 close(stopc)
193 })
194 n, err = conn.Read(b)
195 if !stop() {
196
197
198 <-stopc
199 conn.SetReadDeadline(time.Time{})
200 return n, ctx.Err()
201 }
202 return n, err
203 }
204
205 listener, err := net.Listen("tcp", "localhost:0")
206 if err != nil {
207 fmt.Println(err)
208 return
209 }
210 defer listener.Close()
211
212 conn, err := net.Dial(listener.Addr().Network(), listener.Addr().String())
213 if err != nil {
214 fmt.Println(err)
215 return
216 }
217 defer conn.Close()
218
219 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
220 defer cancel()
221
222 b := make([]byte, 1024)
223 _, err = readFromConn(ctx, conn, b)
224 fmt.Println(err)
225
226
227
228 }
229
230
231
232 func ExampleAfterFunc_merge() {
233
234
235 mergeCancel := func(ctx, cancelCtx context.Context) (context.Context, context.CancelFunc) {
236 ctx, cancel := context.WithCancelCause(ctx)
237 stop := context.AfterFunc(cancelCtx, func() {
238 cancel(context.Cause(cancelCtx))
239 })
240 return ctx, func() {
241 stop()
242 cancel(context.Canceled)
243 }
244 }
245
246 ctx1, cancel1 := context.WithCancelCause(context.Background())
247 defer cancel1(errors.New("ctx1 canceled"))
248
249 ctx2, cancel2 := context.WithCancelCause(context.Background())
250
251 mergedCtx, mergedCancel := mergeCancel(ctx1, ctx2)
252 defer mergedCancel()
253
254 cancel2(errors.New("ctx2 canceled"))
255 <-mergedCtx.Done()
256 fmt.Println(context.Cause(mergedCtx))
257
258
259
260 }
261
View as plain text