-
Notifications
You must be signed in to change notification settings - Fork 14
/
gorpool_test.go
68 lines (63 loc) · 1.41 KB
/
gorpool_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
// Create by Yale 2018/1/8 14:48
package gorpool
import (
"fmt"
"math/rand"
"runtime"
"testing"
"time"
)
func TestNewPool(t *testing.T) {
rn := rand.New(rand.NewSource(time.Now().UnixNano()))
p := NewPool(3, 10).SetIdleDuration(1 * time.Second).Start()
defer p.StopAll()
for i := 0; i < 10; i++ {
count := i
p.AddJob(func() {
time.Sleep(1 * time.Second)
fmt.Printf("v %d\r\n", count)
})
p.AddJob(func() {
time.Sleep(1 * time.Second)
fmt.Printf("v %d\r\n", count+100)
})
time.Sleep(time.Duration(rn.Intn(3)) * time.Second)
fmt.Printf("w %d\r\n", p.WorkerLength())
}
time.Sleep(60 * time.Second)
}
func TestPool_EnableWaitForAll(t *testing.T) {
p := NewPool(5, 10).Start().
EnableWaitForAll(true)
for i := 0; i < 100; i++ {
count := i
p.AddJob(func() {
time.Sleep(10 * time.Millisecond)
fmt.Printf(" %d\r\n", count)
})
}
p.WaitForAll()
p.StopAll()
}
func TestPool_StopAll(t *testing.T) {
rn := rand.New(rand.NewSource(time.Now().UnixNano()))
rnum := runtime.NumGoroutine()
p := NewPool(5, 10).SetIdleDuration(3 * time.Second).Start().
EnableWaitForAll(true)
defer func() {
p.StopAll()
if rnum != runtime.NumGoroutine() {
t.Error("Goroutine not stop")
} else {
t.Log("Goroutine stoped")
}
}()
for i := 0; i < 100; i++ {
count := i
p.AddJob(func() {
time.Sleep(time.Duration(rn.Intn(5)) * time.Second)
fmt.Printf("%d\r\n", count)
})
}
p.WaitForAll()
}