-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathants_test.go
80 lines (65 loc) · 1.48 KB
/
ants_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
package ants
import (
"fmt"
"github.com/panjf2000/ants/v2"
"sync"
"testing"
"time"
)
var runTimes = 1000
// Use the common pool.
func TestCommonPool(t *testing.T) {
// 关闭协程池
defer ants.Release()
var wg sync.WaitGroup
syncCalculateSum := func() {
demoFunc()
wg.Done()
}
// 执行100次调用
for i := 0; i < runTimes; i++ {
wg.Add(1)
err := ants.Submit(syncCalculateSum)
if err != nil {
panic(err)
}
}
wg.Wait()
fmt.Printf("running goroutines: %d\n", ants.Running())
fmt.Printf("finish all tasks.\n")
}
// Use the pool with a function,
// set 10 to the capacity of goroutine pool and 1 second for expired duration.
func TestPoolWithFunc(t *testing.T) {
var wg sync.WaitGroup
// 带方法的协程池
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
myFunc(i)
wg.Done()
})
// 关闭协程池
defer p.Release()
for i := 0; i < runTimes; i++ {
wg.Add(1)
// 调用协程池中我们定义的方法
_ = p.Invoke(int32(i))
}
wg.Wait()
fmt.Printf("running goroutines: %d\n", p.Running())
fmt.Printf("finish all tasks, result is %d\n", sum)
}
func TestWithOptions(t *testing.T) {
p, _ := ants.NewPool(3, ants.WithExpiryDuration(time.Second*1))
defer p.Release()
for i := 0; i < 10; i++ {
err := p.Submit(func() {
fmt.Printf("执行方法....... %d \n ", i)
time.Sleep(1 * time.Second) // 模拟耗时操作
fmt.Println("Hello With Options")
})
if err != nil {
fmt.Println(err.Error())
}
}
time.Sleep(5 * time.Second)
}