-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
118 lines (104 loc) · 2.99 KB
/
pool_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
package workpool
import (
"log"
"os"
"testing"
"time"
)
func TestPool(t *testing.T) {
task := NewTask(func() error {
log.Println("hello")
return nil
})
p := NewPool(
WithExecInterval(100*time.Millisecond),
WithEntryCap(10), WithJobCap(10), WithWorkerCap(10),
WithLogger(log.New(os.Stderr, "", log.LstdFlags)),
)
// p := NewPool(WithEntryCap(3), WithWorkerCap(10))
go func() {
i := 0
for {
//if i > 100000 {
// break
//}
p.AddTask(task)
//log.Println("i = ", i)
i++
}
}()
p.Run()
}
func TestShutdown(t *testing.T) {
task := NewTask(func() error {
log.Println("hello")
return nil
})
p := NewPool(
WithExecInterval(100*time.Millisecond),
WithEntryCap(10), WithJobCap(1000),
WithWorkerCap(1000), WithLogger(log.New(os.Stderr, "", log.LstdFlags)),
WithEntryCloseWait(2*time.Second),
WithShutdownWait(3*time.Second),
)
go func() {
i := 0
for {
if i > 1200000 {
p.Shutdown()
break
}
p.AddTask(task)
log.Println("i = ", i)
i++
}
}()
p.Run()
}
/**
go test -v -test.run TestPool
2020/07/04 11:33:01 i = 937710
2020/07/04 11:33:01 current worker id: 68 will exit...
2020/07/04 11:33:01 current worker id: 40 will exit...
2020/07/04 11:33:01 current worker id: 447 will exit...
2020/07/04 11:33:01 current worker id: 734 will exit...
2020/07/04 11:33:01 current worker id: 579 will exit...
2020/07/04 11:33:01 current worker id: 737 will exit...
2020/07/04 11:33:01 current worker id: 20 will exit...
2020/07/04 11:33:01 i = 937711
2020/07/04 11:33:01 i = 937712
2020/07/04 11:33:01 i = 937713
2020/07/04 11:33:01 current worker id: 995 will exit...
2020/07/04 11:33:01 work pool shutdown success
2020/07/04 11:33:01 i = 937719
2020/07/04 11:33:01 i = 937720
2020/07/04 11:33:01 i = 937721
--- PASS: TestShutdown (4.58s)
2020/07/04 11:33:01 i = 937731
PASS
ok github.com/daheige/workpool 4.586s
go test -v -test.run TestShutdown
2020/07/04 11:16:07 hello
2020/07/04 11:16:07 current worker id: 457
2020/07/04 11:16:07 hello
2020/07/04 11:16:07 current worker id: 245
2020/07/04 11:16:09 work pool will shutdown...
2020/07/04 11:16:09 recv signal: terminated
2020/07/04 11:16:11 current worker id: 740 will exit...
2020/07/04 11:16:11 current worker id: 245 will exit...
2020/07/04 11:16:11 current worker id: 1000 will exit...
2020/07/04 11:16:11 current worker id: 6 will exit...
2020/07/04 11:16:11 current worker id: 723 will exit...
2020/07/04 11:16:11 current worker id: 334 will exit...
2020/07/04 11:16:11 current worker id: 754 will exit...
2020/07/04 11:16:11 work pool shutdown success
2020/07/04 11:16:11 current worker id: 324 will exit...
2020/07/04 11:16:11 current worker id: 423 will exit...
--- PASS: TestShutdown (127.41s)
2020/07/04 11:16:11 current worker id: 830 will exit...
2020/07/04 11:1PASS
6:11 current worker id: 693 will exit...
2020/07/04 11:16:11 current worker id: 552 will exit...
2020/07/04 11:16:11 current worker id: 394 will exit...
2020/07/04 11:16:11 current worker id: 14 will exit...
*/