-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
136 lines (99 loc) · 2.69 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
package golang_context
import (
"context"
"fmt"
"runtime"
"testing"
"time"
)
// Pengenalan Context
func TestContext(t *testing.T) {
background := context.Background()
fmt.Println(background)
todo := context.TODO()
fmt.Println(todo)
}
// parent dan child context
// context with value
func TestContextWithValue(t *testing.T) {
contextA := context.Background()
contextB := context.WithValue(contextA, "b", "A")
contextC := context.WithValue(contextA, "c", "C")
contextD := context.WithValue(contextB, "d", "D")
contextE := context.WithValue(contextB, "e", "E")
contextF := context.WithValue(contextC, "f", "F")
fmt.Println(contextA)
fmt.Println(contextB)
fmt.Println(contextC)
fmt.Println(contextD)
fmt.Println(contextE)
fmt.Println(contextF)
fmt.Println(contextF.Value("f"))
fmt.Println(contextF.Value("c"))
fmt.Println(contextF.Value("b"))
fmt.Println(contextA.Value("b"))
}
// context with cancel
func CreateCounter(ctx context.Context) chan int {
destination := make(chan int)
go func() {
defer close(destination)
counter := 1
for {
select {
case <-ctx.Done():
return
default:
destination <- counter
counter++
time.Sleep(2 * time.Second)
}
}
}()
return destination
}
func TestContextWithCancel(t *testing.T) {
fmt.Println("Total Goroutine", runtime.NumGoroutine())
parent := context.Background()
ctx, cancel := context.WithCancel(parent)
destination := CreateCounter(ctx)
fmt.Println("Total Goroutine", runtime.NumGoroutine())
for n := range destination {
fmt.Println("Counter", n)
if n == 10 {
break
}
}
cancel() // mengirim sinyal ke context
time.Sleep(2 * time.Second)
fmt.Println("Total Goroutine", runtime.NumGoroutine())
}
// context with time out
func TestContextWithTimeOut(t *testing.T) {
fmt.Println("Total Goroutine", runtime.NumGoroutine())
parent := context.Background()
ctx, cancel := context.WithTimeout(parent, 5*time.Second)
defer cancel()
destination := CreateCounter(ctx)
fmt.Println("Total Goroutine", runtime.NumGoroutine())
for n := range destination {
fmt.Println("Counter", n)
}
cancel() // mengirim sinyal ke context
time.Sleep(2 * time.Second)
fmt.Println("Total Goroutine", runtime.NumGoroutine())
}
// context with deadline
func TestContextWithDeadline(t *testing.T) {
fmt.Println("Total Goroutine", runtime.NumGoroutine())
parent := context.Background()
ctx, cancel := context.WithDeadline(parent, time.Now().Add(5*time.Second))
defer cancel()
destination := CreateCounter(ctx)
fmt.Println("Total Goroutine", runtime.NumGoroutine())
for n := range destination {
fmt.Println("Counter", n)
}
time.Sleep(2 * time.Second)
fmt.Println("Total Goroutine", runtime.NumGoroutine())
}