-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkbalancer_test.go
148 lines (129 loc) · 3.42 KB
/
workbalancer_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
146
147
148
package workbalancer
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var exists = struct{}{}
func TestBalancing_Ok(t *testing.T) {
var workloads []Workload
n := 32
for i := 0; i < n; i++ {
workloads = append(workloads, &incWork{i: i})
}
balancer := NewChannelBalancer(4)
actualResults := make(map[int]struct{})
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
for result := range balancer.GetResults() {
intResult, ok := result.(int)
if !ok {
t.Fatalf("A result was not of correct type. %v", result)
}
actualResults[intResult] = exists
}
wg.Done()
}()
balancer.Balance(workloads)
wg.Wait()
for i := 0; i < n; i++ {
_, ok := actualResults[i+1]
assert.True(t, ok, "expected work result %d wasn't found in results", i+1)
}
}
func TestSingleWorker_Ok(t *testing.T) {
var workloads []Workload
n := 8
for i := 0; i < n; i++ {
workloads = append(workloads, &incWork{i: i})
}
balancer := NewChannelBalancer(1)
actualResults := make(map[int]struct{})
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
for result := range balancer.GetResults() {
intResult, ok := result.(int)
if !ok {
t.Fatalf("A result was not of correct type. %v", result)
}
// log.Infof("result=%v", intResult)
actualResults[intResult] = exists
}
wg.Done()
}()
balancer.Balance(workloads)
wg.Wait()
for i := 0; i < n; i++ {
_, ok := actualResults[i+1]
assert.True(t, ok, "expected work result %d wasn't found in results", i+1)
}
}
func TestNoWorker_Ok(t *testing.T) {
var workloads []Workload
n := 8
for i := 0; i < n; i++ {
workloads = append(workloads, &incWork{i: i})
}
balancer := NewChannelBalancer(0)
actualResults := make(map[int]struct{})
wg := make(chan bool)
go func() {
for result := range balancer.GetResults() {
t.Fatalf("A result appeared, but with no workers this should not have been possible. result=%v", result)
}
wg <- true
}()
timer := time.NewTimer(time.Second)
select {
case <-timer.C:
return
case <-wg:
assert.Equal(t, actualResults, 0, "Result should be 0 because there are no workers.")
balancer.Balance(workloads)
t.Fatalf("Shouldn't end here as there are no workers to process work, hence no results.")
}
}
// This basically tests that if there are two greater workloads, and a lot of small ones, and only two threads,
// then the total amount of time will be just the sum of the workloads and no threads will block waiting, not doing anything.
func TestTimeSumsWorkloadTimePerNumberOfWorkersEvenOnUnevenWorkUnits_Ok(t *testing.T) {
var workloads []Workload
n := 16
u := 50
workloads = append(workloads, &timeWork{t: time.Duration(n*u) * time.Millisecond})
for i := 0; i < n-2; i++ {
workloads = append(workloads, &timeWork{t: time.Duration(u) * time.Millisecond})
}
workloads = append(workloads, &timeWork{t: time.Duration(n*u) * time.Millisecond})
balancer := NewChannelBalancer(2)
start := time.Now()
nActualResults := 0
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
for _ = range balancer.GetResults() {
nActualResults++
}
wg.Done()
}()
balancer.Balance(workloads)
wg.Wait()
expectedEnd := start.Add(time.Duration((2*n+1)*u) * time.Millisecond)
assert.Equal(t, n, nActualResults)
assert.True(t, time.Now().Before(expectedEnd))
}
type incWork struct {
i int
}
func (w *incWork) Do() Result {
return w.i + 1
}
type timeWork struct {
t time.Duration
}
func (w *timeWork) Do() Result {
time.Sleep(w.t)
return true
}