forked from pibigstar/go-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
52 lines (45 loc) · 1009 Bytes
/
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
package context
import (
"context"
"fmt"
"testing"
"time"
)
func TestContextWithCancel(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
cancel()
select {
case <-ctx.Done():
fmt.Println("手动取消")
return
}
}
func TestContextWithValue1(t *testing.T) {
kv := make(map[string]interface{})
kv["name"] = "pibigstar"
kv["age"] = 20
contextWithValue(kv)
}
func TestContextWithDeadLine(t *testing.T) {
ctx := context.Background()
deadTime := time.Now().Add(time.Second * 3)
ctx, _ = context.WithDeadline(ctx, deadTime)
select {
case <-ctx.Done():
fmt.Println("到达deadLine,结束执行")
return
}
}
func TestContextWithTimeout(t *testing.T) {
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Second*3)
select {
case <-ctx.Done():
fmt.Println("已超时,结束执行")
}
}
func TestContextWithValue(t *testing.T) {
ctx := context.WithValue(context.Background(), "name", "派大星")
fmt.Println(ctx.Value("name"))
}