-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoyride_test.go
240 lines (193 loc) · 6.48 KB
/
joyride_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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package joyride
import (
"context"
"testing"
"time"
"github.com/smarty/assertions/should"
"github.com/smarty/gunit"
)
func TestJoyrideFixture(t *testing.T) {
gunit.Run(new(JoyrideFixture), t)
}
type JoyrideFixture struct {
*gunit.Fixture
ctx context.Context
task *TracingTask
io *FakeExternalIO
handler *ExampleHandler
runner Runner
}
func (this *JoyrideFixture) Setup() {
this.io = &FakeExternalIO{}
this.ctx = context.Background()
this.runner = NewRunner(WithStorageReader(this.io), WithStorageWriter(this.io), WithMessageDispatcher(this.io))
this.task = NewTracingTask()
this.handler = NewExampleHandler(this.runner, this.task)
}
func (this *JoyrideFixture) TestRunnerDropsNilExecutablesWithoutPanicking() {
this.So(func() { this.runner.Run(this.ctx, nil) }, should.NotPanic)
}
func (this *JoyrideFixture) TestCompositeTaskDropsNilResultFromExecutableWithoutPanicking() {
this.handler = NewExampleHandler(this.runner, NewNilResultTask())
this.So(func() { this.handler.Handle(this.ctx, 42) }, should.NotPanic)
}
func (this *JoyrideFixture) TestHandlerClearsTasksAfterExecuted() {
this.handler.Handle(this.ctx, 42)
this.handler.Handle(this.ctx, 42)
this.So(this.task.executed, should.HaveLength, 1)
}
func (this *JoyrideFixture) TestMessageHandled_TaskExecuted() {
this.handler.Handle(this.ctx, 42)
this.So(this.handler.handled, should.Resemble, []interface{}{42})
this.So(this.io.readCalls, should.Equal, 1)
this.So(this.io.writeCalls, should.Equal, 1)
this.So(this.io.dispatchCalls, should.Equal, 1)
this.So(this.io.reads, should.Resemble, this.task.reads)
this.So(this.io.writes, should.Resemble, this.task.writes)
this.So(this.io.messages, should.Resemble, this.task.messages)
this.So(this.task.Times(), should.BeChronological)
this.So(this.handler.handleContext, should.Equal, this.ctx)
this.So(this.task.executeContext, should.Equal, this.ctx)
this.So(this.io.readContext, should.Equal, this.ctx)
this.So(this.io.writeContext, should.Equal, this.ctx)
this.So(this.io.dispatchContext, should.Equal, this.ctx)
}
func (this *JoyrideFixture) TestCannotHandleMessage_Panic() {
this.handler.canHandle = false
this.So(func() { this.handler.Handle(this.ctx, 42) }, should.Panic)
this.So(this.handler.handled, should.Resemble, []interface{}{42})
this.So(this.io.reads, should.BeEmpty)
this.So(this.io.writes, should.BeEmpty)
this.So(this.io.messages, should.BeEmpty)
}
func (this *JoyrideFixture) TestChainedTasksAreExecuted() {
next := NewTracingTask()
this.task.next = next
this.handler.Handle(this.ctx, 42)
if this.So(next.executed, should.HaveLength, 1) {
this.So(next.executed[0].IsZero(), should.BeFalse)
}
this.So(next.Times(), should.BeChronological)
}
func (this *JoyrideFixture) TestAddedTasksAreExecuted() {
next := NewTracingTask()
this.handler.Add(next)
this.handler.Handle(this.ctx, 42)
if this.So(next.executed, should.HaveLength, 1) {
this.So(next.executed[0].IsZero(), should.BeFalse)
}
this.So(next.Times(), should.BeChronological)
}
func (this *JoyrideFixture) TestIOAvoidedWhenThereIsNoWorkToBeDone() {
task := NewZeroIOTask()
this.handler = NewExampleHandler(this.runner, task)
this.handler.Handle(this.ctx, 42)
this.So(this.io.readCalls, should.Equal, 0)
this.So(this.io.writeCalls, should.Equal, 0)
this.So(this.io.dispatchCalls, should.Equal, 0)
}
func (this *JoyrideFixture) TestNilTask_NoPanic() {
this.handler.Add(NewNilTask())
this.So(func() { this.handler.Handle(this.ctx, 42) }, should.NotPanic)
this.handler.Handle(this.ctx, 42)
}
///////////////////////////////////////////////////////////////
type ExampleHandler struct {
*Handler
handleContext context.Context
handled []interface{}
canHandle bool
}
func NewExampleHandler(runner TaskRunner, task Executable) *ExampleHandler {
this := &ExampleHandler{canHandle: true}
this.Handler = NewHandler(this, runner, task)
return this
}
func (this *ExampleHandler) HandleMessage(ctx context.Context, message interface{}) bool {
this.handleContext = ctx
this.handled = append(this.handled, message)
// Note: Normally, data from the message would now be provided
// to the tasks already registered with the handler.
return this.canHandle
}
//////////////////////////////////////////////////////////////
type TracingTask struct {
*Base
initialized time.Time
read time.Time
executed []time.Time
executeContext context.Context
}
func NewTracingTask() *TracingTask {
base := New()
base.AddRequiredReads(1, 2, 3)
base.AddPendingWrites("4", "5", 6.0)
base.AddPendingMessages(7, "eight", 9, true)
return &TracingTask{
Base: base,
initialized: time.Now().UTC(),
}
}
func (this *TracingTask) Times() []time.Time {
return append([]time.Time{
this.initialized,
this.read,
}, this.executed...)
}
func (this *TracingTask) RequiredReads() []interface{} {
this.read = time.Now().UTC()
return this.Base.RequiredReads()
}
func (this *TracingTask) Execute(ctx context.Context) TaskResult {
this.executeContext = ctx
this.executed = append(this.executed, time.Now().UTC())
return this.Base.Execute(ctx)
}
/////////////////////////////////////////////////////////////
type NilResultTask struct{ *Base }
func NewNilResultTask() *NilResultTask {
return &NilResultTask{Base: New()}
}
func (this *NilResultTask) Execute(_ context.Context) TaskResult {
return nil
}
/////////////////////////////////////////////////////////////
type ZeroIOTask struct{ *Base }
func NewZeroIOTask() *ZeroIOTask {
return &ZeroIOTask{Base: New()}
}
func (this *ZeroIOTask) Execute(_ context.Context) TaskResult {
return this
}
/////////////////////////////////////////////////////////////
type FakeExternalIO struct {
readContext context.Context
writeContext context.Context
dispatchContext context.Context
reads []interface{}
writes []interface{}
messages []interface{}
readCalls int
writeCalls int
dispatchCalls int
}
func (this *FakeExternalIO) Read(ctx context.Context, items ...interface{}) {
this.readCalls++
this.readContext = ctx
this.reads = append(this.reads, items...)
}
func (this *FakeExternalIO) Write(ctx context.Context, items ...interface{}) {
this.writeCalls++
this.writeContext = ctx
this.writes = append(this.writes, items...)
}
func (this *FakeExternalIO) Dispatch(ctx context.Context, items ...interface{}) {
this.dispatchCalls++
this.dispatchContext = ctx
this.messages = append(this.messages, items...)
}
/////////////////////////////////////////////////////////////
type NilTask struct{ *Base }
func NewNilTask() *NilTask {
return nil
}