-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher_test.go
94 lines (75 loc) · 1.55 KB
/
dispatcher_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
package dispatcher
import (
"runtime"
"testing"
)
var (
workerCount = 4
queueSize = runtime.NumCPU()
)
func TestNew(t *testing.T) {
d := New(workerCount, queueSize)
if d == nil {
t.Error("New returned nil")
}
if d.pool == nil {
t.Error("Worker pool is not initialized.")
}
if d.queue == nil {
t.Error("Tasker queue is not initialized.")
}
if d.quit == nil {
t.Error("Quit channel is not initialized.")
}
if d.workers == nil {
t.Error("Workers not initialized.")
}
poolCap := cap(d.pool)
if poolCap != workerCount {
t.Errorf("want %v\ngot %v", workerCount, poolCap)
}
queueCap := cap(d.queue)
if queueCap != queueSize {
t.Errorf("want %v\ngot %v", queueSize, queueCap)
}
workerCnt := len(d.workers)
if workerCnt != workerCount {
t.Errorf("want %v\ngot %v", workerCount, workerCnt)
}
for i, w := range d.workers {
if w == nil {
t.Errorf("%d : Worker is nil.", i)
}
if w.dispatcher != d {
t.Errorf("%d : Invalid dispatcher.", i)
}
if w.quit == nil {
t.Errorf("%d : Quit channel is not initialized.", i)
}
if w.task == nil {
t.Errorf("%d : Tasker channel is not initialized.", i)
}
}
}
type testTasker struct {
done bool
}
func (t *testTasker) Run() {
t.done = true
}
func TestDispatcher(t *testing.T) {
d := New(workerCount, queueSize)
tasks := make([]*testTasker, queueSize)
for i := 0; i < queueSize; i++ {
task := &testTasker{false}
d.Enqueue(task)
tasks[i] = task
}
d.Start()
d.Wait()
for i, task := range tasks {
if !task.done {
t.Errorf("Tasker %d has never run.", i)
}
}
}