-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_pool_test.go
More file actions
174 lines (151 loc) · 3.42 KB
/
go_pool_test.go
File metadata and controls
174 lines (151 loc) · 3.42 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package g
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestNewGoPool(t *testing.T) {
test1()
test2()
test3()
}
func test1() {
p := NewGoPool(1 << 16)
// When the coroutine pool exits, customize the to-do task list, If not set, all pending tasks will be discarded.
p.Clean(func(t GoTask) {
// Currently in the cleanup task, maybe there is no need to execute the original task logic.
t.Execute() // It may need to be commented out.
// Implement your own custom cleanup logic. TODO...
i, _ := t.Load()
fmt.Printf("test1 call by clean: %p %v\n", t, i)
})
all := int32(0)
{
wg := sync.WaitGroup{}
p.Start()
for i := 0; i < 500; i++ {
wg.Add(1)
go func(a int) {
defer wg.Done()
for j := 0; j < 1000; j++ {
tmp := NewGoTask()
tmp.Store(nil, func(i any) {
defer atomic.AddInt32(&all, 1)
// TODO your logic
})
p.Submit(tmp)
}
}(i)
}
wg.Wait()
p.Stop()
}
// Secondary startup.
{
wg := sync.WaitGroup{}
p.Start()
for i := 0; i < 500; i++ {
wg.Add(1)
go func(a int) {
defer wg.Done()
for j := 0; j < 1000; j++ {
tmp := NewGoTask()
tmp.Store(nil, func(i any) {
defer atomic.AddInt32(&all, 1)
// TODO your logic
})
p.Submit(tmp)
}
}(i)
}
wg.Wait()
p.Stop()
}
fmt.Println("test1", all)
}
func test2() {
input := int32(0)
done := int32(0)
success := int32(0)
count := int32(5000000)
stopCount := count / 10 * 7
p := NewGoPool(1 << 16)
p.Clean(func(t GoTask) {
// Currently in the cleanup task, maybe there is no need to execute the original task logic.
t.Execute() // It may need to be commented out.
// Implement your own custom cleanup logic. TODO...
i, _ := t.Load()
fmt.Printf("test2 call by clean: %p %v\n", t, i)
})
p.Start()
go func() {
for i := int32(0); i < count; i++ {
tmp := NewGoTask()
tmp.Store(i, func(i any) {
defer atomic.AddInt32(&done, 1)
// custom logic
if iv, ok := i.(int32); ok {
if iv&1 == 1 {
return
}
}
atomic.AddInt32(&success, 1)
})
if p.Submit(tmp) {
atomic.AddInt32(&input, 1)
}
}
}()
// In some cases, you may need to stop the GoPool.
go func() {
for {
if atomic.LoadInt32(&input) >= stopCount {
p.Stop()
fmt.Println("test2", "stop called")
break
}
}
}()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
if p.Status() != GoPoolStatusRunning {
break
}
<-ticker.C
fmt.Println("test2", "pending:", p.Pending(), "input:", atomic.LoadInt32(&input), "done:", atomic.LoadInt32(&done), "success:", atomic.LoadInt32(&success))
}
}
func test3() {
p := NewGoPool(1 << 16)
p.Clean(func(t GoTask) {
// Currently in the cleanup task, maybe there is no need to execute the original task logic.
// t.Execute(t) // It may need to be commented out.
// Implement your own custom cleanup logic. TODO...
i, _ := t.Load()
fmt.Printf("test3 call by clean: %p %v\n", t, i)
})
p.Start()
count := 10000000
added := int32(0)
called := int32(0)
for a := 0; a < 10; a++ {
go func() {
total := count / 10
for i := 0; i < total; i++ {
tmp := NewGoTask()
tmp.Store(i, func(i any) {
atomic.AddInt32(&called, 1)
})
if p.Submit(tmp) {
atomic.AddInt32(&added, 1)
}
}
}()
}
time.Sleep(time.Second * 3)
p.Stop()
fmt.Println("test3", "count:", count, "called:", atomic.LoadInt32(&called), "add:", atomic.LoadInt32(&added))
}