-
Notifications
You must be signed in to change notification settings - Fork 36
/
task_runner_test.go
109 lines (89 loc) · 2.11 KB
/
task_runner_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
package workers
import (
"errors"
"log"
"os"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTaskRunner_process(t *testing.T) {
testLogger := log.New(os.Stdout, "test-go-workers2: ", log.Ldate|log.Lmicroseconds)
msg, _ := NewMsg(`{}`)
t.Run("handles-panic", func(t *testing.T) {
tr := newTaskRunner(testLogger, func(m *Msg) error {
panic("task-test-panic")
})
err := tr.process(msg)
assert.EqualError(t, err, "task-test-panic")
})
t.Run("returns-error", func(t *testing.T) {
var errorToRet error
tr := newTaskRunner(testLogger, func(m *Msg) error {
return errorToRet
})
err := tr.process(msg)
assert.NoError(t, err)
errorToRet = errors.New("ret me")
err = tr.process(msg)
assert.EqualError(t, err, errorToRet.Error())
})
}
func TestTaskRunner(t *testing.T) {
msgCh := make(chan *Msg)
doneCh := make(chan *Msg)
readyCh := make(chan bool)
syncCh := make(chan bool)
noSyncMsg := func() *Msg {
m, _ := NewMsg(`{}`)
return m
}
syncMsg := func() *Msg {
m, _ := NewMsg(`{"sync": true}`)
return m
}
tr := newTaskRunner(Logger, func(m *Msg) error {
if m.Get("sync").MustBool() {
syncCh <- true
<-syncCh
}
return nil
})
var wg sync.WaitGroup
wg.Add(1)
go func() {
tr.work(msgCh, doneCh, readyCh)
wg.Done()
}()
t.Run("consumes-messages", func(t *testing.T) {
msgCh <- noSyncMsg()
doneMsg := <-doneCh
assert.NotNil(t, doneMsg)
assert.NotZero(t, doneMsg.startedAt)
msgCh <- noSyncMsg()
doneMsg = <-doneCh
assert.NotNil(t, doneMsg)
assert.NotZero(t, doneMsg.startedAt)
})
t.Run("sends-to-ready-when-no-message", func(t *testing.T) {
<-readyCh
})
t.Run(".inProgressMessage", func(t *testing.T) {
msgCh <- syncMsg()
<-syncCh
ipm := tr.inProgressMessage()
assert.NotNil(t, ipm)
assert.NotZero(t, ipm.startedAt)
syncCh <- true
doneMsg := <-doneCh
assert.NotNil(t, doneMsg)
assert.NotZero(t, doneMsg.startedAt)
ipm = tr.inProgressMessage()
assert.Nil(t, ipm)
})
t.Run(".quit", func(t *testing.T) {
tr.quit()
// wg.Wait will cause the test to timeout if tr.quit() doesn't shut down the taskRunner
wg.Wait()
})
}