-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker_test.go
More file actions
177 lines (155 loc) · 3.62 KB
/
worker_test.go
File metadata and controls
177 lines (155 loc) · 3.62 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package fastexec
import (
"bytes"
"strconv"
"strings"
"testing"
"time"
)
type FakeJob struct {
data []byte
}
// Execute a Job with associated data
func (j *FakeJob) Execute() error {
j.data = bytes.ToUpper(j.data)
return nil
}
// Get input data from a Job
func (j FakeJob) GetData() []byte {
return j.data
}
// Get output result from a Job
func (j FakeJob) GetResult() []byte {
return j.data
}
// Get err result from a Job
func (j FakeJob) GetErr() []byte {
return j.data
}
func TestWorker(t *testing.T) {
testCases := []struct {
about string
jobs []FakeJob
expected [][]byte
}{
{
about: "fake single job execution",
jobs: []FakeJob{FakeJob{data: []byte("fakedata")}},
expected: [][]byte{[]byte("FAKEDATA")},
},
{
about: "fake multiple job execution",
jobs: []FakeJob{
FakeJob{data: []byte("fakedata1")},
FakeJob{data: []byte("fakedata2")},
},
expected: [][]byte{
[]byte("FAKEDATA1"),
[]byte("FAKEDATA2")},
},
}
for _, c := range testCases {
in := make(chan Job, 100)
out := make(chan Job, 100)
for _, j := range c.jobs {
// make sure the value is copied on stack
j := j
in <- &j
}
close(in)
Worker(in, out)
if len(out) != len(c.jobs) {
t.Errorf("testcase: %s, expected processed %d got %d", c.about, len(c.jobs), len(out))
}
close(out)
i := 0
for j := range out {
if !bytes.Equal(j.GetResult(), c.expected[i]) {
t.Errorf("testcase: %s, expected result %v got %v", c.about, string(c.expected[i]), string(j.GetResult()))
}
i++
}
}
}
func generateFakeJob(i int) FakeJob {
msg := []string{"fake job ID:", strconv.Itoa(i)}
data := strings.Join(msg, " ")
j := FakeJob{data: []byte(data)}
return j
}
type TimeRange struct {
min time.Duration
max time.Duration
}
func TestRateLimitedWorker(t *testing.T) {
testCases := []struct {
about string
jobNumber int
chunks int
ratelimit int64
t TimeRange
}{
{
about: "fake job run without ratelimit",
jobNumber: 1,
chunks: 1,
ratelimit: -1,
t: TimeRange{min: time.Duration(0), max: 100 * time.Microsecond},
},
{
about: "fake job run ratelimit 1/s chunk 1 - burst",
jobNumber: 1,
chunks: 1,
ratelimit: 1,
t: TimeRange{min: time.Duration(0), max: 100 * time.Microsecond},
},
{
about: "fake job run ratelimit 2/s chunk 1 - burst",
jobNumber: 4,
chunks: 1,
ratelimit: 2,
t: TimeRange{
min: 1*time.Second - 10*time.Millisecond,
max: 1*time.Second + 10*time.Millisecond},
},
{
about: "fake job run ratelimit 1/s chunk 2",
jobNumber: 2,
chunks: 2,
ratelimit: 1,
t: TimeRange{
min: 4*time.Second - 10*time.Millisecond,
max: 4*time.Second + 10*time.Millisecond},
},
}
for _, c := range testCases {
Config.chunks = c.chunks
Config.ratelimit = c.ratelimit
initWorkers()
in := make(chan Job, 100)
out := make(chan Job, 100)
for i := 0; i < c.jobNumber; i++ {
j := generateFakeJob(i)
in <- &j
}
close(in)
start := time.Now()
Worker(in, out)
if len(out) != c.jobNumber {
t.Errorf("testcase: %s, expected processed %d got %d", c.about, c.jobNumber, len(out))
}
close(out)
i := 0
for j := range out {
expected := strings.Join([]string{"FAKE JOB ID:", strconv.Itoa(i)}, " ")
if !bytes.Equal(j.GetResult(), []byte(expected)) {
t.Errorf("testcase: %s, expected result %v got %v", c.about, expected, string(j.GetResult()))
}
i++
}
elapsed := time.Now().Sub(start)
if elapsed < c.t.min || elapsed > c.t.max {
t.Errorf("testcase: %s, expected time [%v, %v] got %v", c.about, c.t.min, c.t.max, elapsed)
}
}
}