-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker_test.go
63 lines (57 loc) · 987 Bytes
/
worker_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
package gopool
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
func TestWorker_Init(t *testing.T) {
w := new(Worker)
w.Init()
}
func TestWorker_Do(t *testing.T) {
w := new(Worker)
w.Init()
wg := sync.WaitGroup{}
loopTimes := 100
wg.Add(loopTimes)
for i := 0; i < loopTimes; i++ {
temp := i
f := func() {
fmt.Printf("Test :job id=%d, time=%s\n", temp, time.Now())
time.Sleep(time.Millisecond * time.Duration(rand.Intn(50)))
wg.Done()
}
e := w.Do(f)
if e != nil {
t.Error(e)
}
}
wg.Wait()
}
// This test should be panic
func TestWorker_Kill(t *testing.T) {
w := new(Worker)
w.Init()
wg := sync.WaitGroup{}
loopTimes := 100
wg.Add(loopTimes)
for i := 0; i < loopTimes; i++ {
temp := i
f := func() {
time.Sleep(time.Second)
fmt.Printf("Test :job id=%d, time=%s\n", temp, time.Now())
wg.Done()
}
e := w.Do(f)
if e != nil {
t.Error(e)
}
}
go func() {
time.Sleep(time.Second * 10)
w.Kill()
}()
wg.Wait()
}