-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_test.go
61 lines (54 loc) · 1.03 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
package iocast
import (
"context"
"testing"
)
func TestWorkerPool(t *testing.T) {
p := NewWorkerPool(1, 1)
p.Start(context.Background())
defer p.Stop()
p2 := NewWorkerPool(0, 0) // will refuse to enqueue
p2.Start(context.Background())
defer p2.Stop()
args := "test"
taskFn := NewTaskFunc(context.Background(), args, testTaskFn)
task := TaskBuilder("ok", taskFn).Build()
task2 := TaskBuilder("full queue", taskFn).Build()
tests := []struct {
name string
expected string
p *WorkerPool
}{
{
"ok",
args,
p,
},
{
"full queue",
args,
p2,
},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if i == 0 {
ok := tt.p.Enqueue(task)
if !ok {
t.Errorf("unexpected full queue")
}
} else {
ok := tt.p.Enqueue(task2)
if ok {
t.Errorf("unexpected not full queue")
}
}
if i == 0 {
result := <-task.Wait()
if result.Out != tt.expected {
t.Errorf("unexpected result out: got %v want %v", result.Out, tt.expected)
}
}
})
}
}