-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_test.go
141 lines (122 loc) · 3.62 KB
/
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
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
package worker
import (
"testing"
"sync"
)
var (
checkCounter = 0
checkMutex = new(sync.Mutex)
)
type increaseNumber struct {
mutex *sync.Mutex
number int
}
func (job *increaseNumber) Go(n int) Result {
job.mutex.Lock(); defer job.mutex.Unlock()
job.number++
return &checkResult{ job.number }
}
type checkResult struct {
number int
}
func (job *checkResult) Go(n int) Result {
checkMutex.Lock(); defer checkMutex.Unlock()
checkCounter++
return nil
}
func TestWorker(t *testing.T) {
job := new(increaseNumber)
job.mutex = new(sync.Mutex)
w, num := New(), 10000
w.SpawnN(3)
for i := 0; i < num; i++ {
w.Do(job)
}
w.Stop()
if job.number != num { t.Errorf("wrong job number") }
if checkCounter != num { t.Errorf("wrong number of replies") }
}
func TestSentry(t *testing.T) {
w := New()
w.SpawnN(5)
job := new(increaseNumber)
job.mutex = new(sync.Mutex)
sentry, num := w.Sentry(), 100000
for i := 0; i < num; i++ {
sentry.Guard(job)
}
results := sentry.Wait()
if n := len(results); n != num { t.Errorf("%v != %v", n, num) } else {
for i, result := range results {
if res, ok := result.(*checkResult); ok {
//if res.number != i { t.Errorf("%v != %v", res.number, i) }
if res.number <= 0 || num < res.number {
t.Errorf("%v (%v)", res.number, i)
}
} else {
// ...
}
}
}
w.Stop()
}
var (
job0Executed = 0
job1Executed = 0
job2Executed = 0
job0Mutex = new(sync.Mutex)
job1Mutex = new(sync.Mutex)
job2Mutex = new(sync.Mutex)
)
type job0 struct {
tag string
}
func (job *job0) Go(n int) Result {
job0Mutex.Lock(); defer job0Mutex.Unlock()
job0Executed++
return new(job1)
}
type job1 struct {
tag string
}
func (job *job1) Go(n int) Result {
job1Mutex.Lock(); defer job1Mutex.Unlock()
job1Executed++
return new(job2)
}
type job2 struct {
tag string
}
func (job *job2) Go(n int) Result {
job2Mutex.Lock(); defer job2Mutex.Unlock()
job2Executed++
return "done"
}
func TestJobChain(t *testing.T) {
job := new(job0)
w, num := New(), 10000
w.SpawnN(3)
for i := 0; i < num; i++ {
w.Do(job)
}
w.Stop()
if job0Executed != num { t.Errorf("job0: %v != %v", job0Executed, num) }
if job1Executed != num { t.Errorf("job1: %v != %v", job1Executed, num) }
if job2Executed != num { t.Errorf("job2: %v != %v", job2Executed, num) }
job0Executed = 0
job1Executed = 0
job2Executed = 0
w = SpawnN(1)
sentry := w.Sentry()
sentry.Guard(&job0{"job0"})
sentry.Guard(&job1{"job1"})
sentry.Guard(&job2{"job2"})
for _, result := range sentry.Wait() {
if s, ok := result.(string); !ok || s != "done" {
t.Errorf("%v != done", result)
}
}
if job0Executed != 1 { t.Errorf("job0: %v != %v", job0Executed, 1) }
if job1Executed != 2 { t.Errorf("job1: %v != %v", job1Executed, 2) }
if job2Executed != 3 { t.Errorf("job2: %v != %v", job2Executed, 3) }
}