-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlaborunion_test.go
145 lines (117 loc) · 2.92 KB
/
laborunion_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
142
143
144
145
package laborunion
import (
"sync/atomic"
"testing"
)
func Test_BasicInit(t *testing.T) {
p := New()
p.SetBatchSize(20)
p.ResizeInChan(1000)
p.SetWorker(func([]interface{}) error {
return nil
})
if p.GetInChan() == nil {
t.Fatalf("After resizing InChan, it should not be nil")
}
if cap(p.GetInChan()) != 1000 {
t.Fatalf("Failed to resize InChan correctly")
}
p.SetWorkerCount(3)
if p.GetWorkerCount() != 3 {
t.Fatalf("Failed to spawn 3 workers")
}
}
func Test_BasicHooks(t *testing.T) {
p := New()
p.SetBatchSize(1)
p.ResizeInChan(1000)
p.SetWorker(func([]interface{}) error {
return nil
})
onNewWorker := false
p.SetOnNewWorker(func() {
onNewWorker = true
})
onDeleteWorker := false
p.SetOnDeleteWorker(func() {
onDeleteWorker = true
})
beforeBatchingHookCalled := false
p.SetBeforeBatchingHook(func() {
beforeBatchingHookCalled = true
})
p.SetWorkerCount(1)
if !onNewWorker {
t.Fatalf("Failed to trigger onNewWorker hook")
}
p.SetWorkerCount(0)
if !onDeleteWorker {
t.Fatalf("Failed to trigger onDeleteWorker hook")
}
p.SetWorkerCount(1)
p.GetInChan() <- true
if !beforeBatchingHookCalled {
t.Fatalf("Failed to trigger beforeBatchingHookCalled hook")
}
}
func Test_BasicSpawnDespawn(t *testing.T) {
p := New()
p.SetWorker(func([]interface{}) error {
return nil
})
changes := p.SetWorkerCount(10)
if changes != 10 {
t.Errorf("Expected to spawn 10 workers. Got: %v", changes)
}
if p.GetWorkerCount() != 10 {
t.Errorf("Expected to have 10 workers. Got: %v", p.GetWorkerCount())
}
changes = p.SetWorkerCount(7)
if changes != 3 {
t.Errorf("Expected to reduce 3 workers. Got: %v", changes)
}
if p.GetWorkerCount() != 7 {
t.Errorf("Expected to have 7 workers. Got: %v", p.GetWorkerCount())
}
}
func Test_BasicSetterGetter(t *testing.T) {
p := New()
p.SetWorker(func([]interface{}) error {
return nil
})
p.SetWorkerCount(2)
if p.GetWorkerCount() != 2 {
t.Errorf("Failed to set number of workers. Got: %v", p.GetWorkerCount())
}
if p.RetryDuration() <= 0 {
t.Errorf("Failed to set default retry duration")
}
p.SetRetries(2)
if p.GetRetries() != 2 {
t.Errorf("Failed to set number of workers. Got: %v", p.GetRetries())
}
}
func Test_BasicConsumption(t *testing.T) {
var workDone int64
expectedWorkDone := int64(10)
p := New()
p.SetWorker(func(tasks []interface{}) error {
atomic.AddInt64(&workDone, 1)
for _, task := range tasks {
println(task.(int64))
}
return nil
})
changes := p.SetWorkerCount(1)
if changes != 1 {
t.Fatalf("Expected to spawn 1 workers. Got: %v", changes)
}
for i := int64(0); i < expectedWorkDone; i++ {
p.GetInChan() <- i
}
finalWorkDone := atomic.LoadInt64(&workDone)
// This test failed in a weird way, we clearly performed the work 10 times, but recorded only 9 times.
if finalWorkDone != expectedWorkDone {
t.Fatalf("Failed to process all %v jobs. Completed only: %v", expectedWorkDone, finalWorkDone)
}
}