-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcancel_by_context_test.go
69 lines (60 loc) · 1.4 KB
/
cancel_by_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
package go_25
import (
"context"
"fmt"
"testing"
"time"
)
/*
Context
关联任务的取消,就是用来当前线程被取消,相关联的子线程也会被取消的场景
- 根Context:通过context.Background()创建
- 子Context:context.WithCancel(parentContext)创建
ctx,cancel=context.WithCancel(context.Background())
- 当前Context被取消时,基于他的子context都会被取消
- 接受取消通知<-ctx.Done()
*/
func isCancelled(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}
func TestCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
for i := 0; i < 5; i++ {
go func(i int, ctx context.Context) {
for {
if isCancelled(ctx) {
fmt.Println("isCancelled")
break
}
fmt.Println("isSleep")
time.Sleep(time.Millisecond * 1)
}
fmt.Println(i, "Cancelled")
}(i, ctx)
}
//关闭渠道 关闭之后上面多线程里面会自动关闭循环
cancel()
time.Sleep(time.Second * 10)
}
/*
运行结果 执行cancel_2方法后关闭了渠道 多线程for循环里面就立马关闭了循环,注释掉cancel_2再执行,会for循环打印isSleep直到10秒后退出
=== RUN TestCancel
isCancelled
3 Cancelled
isCancelled
4 Cancelled
isCancelled
1 Cancelled
isCancelled
2 Cancelled
isCancelled
0 Cancelled
--- PASS: TestCancel (1.00s)
PASS
Process finished with the exit code 0
*/