-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker_test.go
71 lines (57 loc) · 1.36 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
62
63
64
65
66
67
68
69
70
71
package piper
import (
"context"
"testing"
"time"
)
type testDispatcher struct {
statusCh chan *status
batchExecFn BatchExecFn
}
func newTestDispatcher() *testDispatcher {
fn := testBatchExecEvensFailFn{}
return &testDispatcher{
batchExecFn: fn.Execute,
statusCh: make(chan *status),
}
}
func TestWorker_NewWorker(t *testing.T) {
td := newTestDispatcher()
w := newWorker(td.batchExecFn, td.statusCh)
if w == nil {
t.Fatal("newWorker returned nil")
}
}
func TestWorker_StartDispatchStop(t *testing.T) {
td := newTestDispatcher()
w := newWorker(td.batchExecFn, td.statusCh)
ctx := context.TODO()
w.exec.start(ctx)
// Create some jobs and put it in a batch
numJobs := 10
b := newBatch(numJobs)
js := newTestJobs(numJobs)
b.add(js...)
// Wait for a status report from the worker
status := <-w.statusCh
// Send the batch
status.address <- b
w.exec.stop(ctx)
}
func TestWorker_CancelContext1(t *testing.T) {
td := newTestDispatcher()
w := newWorker(td.batchExecFn, td.statusCh)
ctx, cancel := context.WithCancel(context.Background())
cancel()
w.exec.start(ctx)
w.exec.stop(ctx)
}
func TestWorker_CancelContext2(t *testing.T) {
td := newTestDispatcher()
w := newWorker(td.batchExecFn, td.statusCh)
ctx, cancel := context.WithCancel(context.Background())
w.exec.start(ctx)
<-time.After(100 * time.Millisecond)
cancel()
w.exec.stop(ctx)
}