-
Notifications
You must be signed in to change notification settings - Fork 166
/
worker_test.go
139 lines (101 loc) · 2.76 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
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
package workers
import (
"github.com/customerio/gospec"
. "github.com/customerio/gospec"
"time"
)
var testMiddlewareCalled bool
var failMiddlewareCalled bool
type testMiddleware struct{}
func (l *testMiddleware) Call(queue string, message *Msg, next func() bool) (result bool) {
testMiddlewareCalled = true
return next()
}
type failMiddleware struct{}
func (l *failMiddleware) Call(queue string, message *Msg, next func() bool) (result bool) {
failMiddlewareCalled = true
next()
return false
}
func confirm(manager *manager) (msg *Msg) {
time.Sleep(10 * time.Millisecond)
select {
case msg = <-manager.confirm:
default:
}
return
}
func WorkerSpec(c gospec.Context) {
var processed = make(chan *Args)
var testJob = (func(message *Msg) {
processed <- message.Args()
})
manager := newManager("myqueue", testJob, 1)
c.Specify("newWorker", func() {
c.Specify("it returns an instance of worker with connection to manager", func() {
worker := newWorker(manager)
c.Expect(worker.manager, Equals, manager)
})
})
c.Specify("work", func() {
worker := newWorker(manager)
messages := make(chan *Msg)
message, _ := NewMsg("{\"jid\":\"2309823\",\"args\":[\"foo\",\"bar\"]}")
c.Specify("calls job with message args", func() {
go worker.work(messages)
messages <- message
args, _ := (<-processed).Array()
<-manager.confirm
c.Expect(len(args), Equals, 2)
c.Expect(args[0], Equals, "foo")
c.Expect(args[1], Equals, "bar")
worker.quit()
})
c.Specify("confirms job completed", func() {
go worker.work(messages)
messages <- message
<-processed
c.Expect(confirm(manager), Equals, message)
worker.quit()
})
c.Specify("runs defined middleware and confirms", func() {
Middleware.Append(&testMiddleware{})
go worker.work(messages)
messages <- message
<-processed
c.Expect(confirm(manager), Equals, message)
c.Expect(testMiddlewareCalled, IsTrue)
worker.quit()
Middleware = NewMiddleware(
&MiddlewareLogging{},
&MiddlewareRetry{},
&MiddlewareStats{},
)
})
c.Specify("doesn't confirm if middleware cancels acknowledgement", func() {
Middleware.Append(&failMiddleware{})
go worker.work(messages)
messages <- message
<-processed
c.Expect(confirm(manager), IsNil)
c.Expect(failMiddlewareCalled, IsTrue)
worker.quit()
Middleware = NewMiddleware(
&MiddlewareLogging{},
&MiddlewareRetry{},
&MiddlewareStats{},
)
})
c.Specify("recovers and confirms if job panics", func() {
var panicJob = (func(message *Msg) {
panic("AHHHHHHHHH")
})
manager := newManager("myqueue", panicJob, 1)
worker := newWorker(manager)
go worker.work(messages)
messages <- message
c.Expect(confirm(manager), Equals, message)
worker.quit()
})
})
}