generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_test.go
306 lines (284 loc) · 8.95 KB
/
process_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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package configkit_test
import (
"context"
"errors"
"reflect"
. "github.com/dogmatiq/configkit"
"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
"github.com/dogmatiq/enginekit/message"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("func FromProcess()", func() {
var handler *ProcessMessageHandlerStub
BeforeEach(func() {
handler = &ProcessMessageHandlerStub{
ConfigureFunc: func(c dogma.ProcessConfigurer) {
c.Identity("<name>", processKey)
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.HandlesEvent[EventStub[TypeB]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
dogma.SchedulesTimeout[TimeoutStub[TypeA]](),
)
},
}
})
When("the configuration is valid", func() {
var cfg RichProcess
JustBeforeEach(func() {
cfg = FromProcess(handler)
})
Describe("func Identity()", func() {
It("returns the handler identity", func() {
Expect(cfg.Identity()).To(Equal(
MustNewIdentity("<name>", processKey),
))
})
})
Describe("func MessageNames()", func() {
It("returns the expected message names", func() {
Expect(cfg.MessageNames()).To(Equal(
EntityMessages[message.Name]{
message.NameOf(CommandA1): {
Kind: message.CommandKind,
IsProduced: true,
},
message.NameOf(EventA1): {
Kind: message.EventKind,
IsConsumed: true,
},
message.NameOf(EventB1): {
Kind: message.EventKind,
IsConsumed: true,
},
message.NameOf(TimeoutA1): {
Kind: message.TimeoutKind,
IsProduced: true,
IsConsumed: true,
},
},
))
})
})
Describe("func MessageTypes()", func() {
It("returns the expected message types", func() {
Expect(cfg.MessageTypes()).To(Equal(
EntityMessages[message.Type]{
message.TypeOf(CommandA1): {
Kind: message.CommandKind,
IsProduced: true,
},
message.TypeOf(EventA1): {
Kind: message.EventKind,
IsConsumed: true,
},
message.TypeOf(EventB1): {
Kind: message.EventKind,
IsConsumed: true,
},
message.TypeOf(TimeoutA1): {
Kind: message.TimeoutKind,
IsProduced: true,
IsConsumed: true,
},
},
))
})
})
Describe("func TypeName()", func() {
It("returns the fully-qualified type name of the handler", func() {
Expect(cfg.TypeName()).To(Equal("*github.com/dogmatiq/enginekit/enginetest/stubs.ProcessMessageHandlerStub"))
})
})
Describe("func ReflectType()", func() {
It("returns the type of the handler", func() {
Expect(cfg.ReflectType()).To(Equal(reflect.TypeOf(handler)))
})
})
Describe("func AcceptVisitor()", func() {
It("calls the appropriate method on the visitor", func() {
v := &visitorStub{
VisitProcessFunc: func(_ context.Context, c Process) error {
Expect(c).To(BeIdenticalTo(cfg))
return errors.New("<error>")
},
}
err := cfg.AcceptVisitor(context.Background(), v)
Expect(err).To(MatchError("<error>"))
})
})
Describe("func AcceptRichVisitor()", func() {
It("calls the appropriate method on the visitor", func() {
v := &richVisitorStub{
VisitRichProcessFunc: func(_ context.Context, c RichProcess) error {
Expect(c).To(BeIdenticalTo(cfg))
return errors.New("<error>")
},
}
err := cfg.AcceptRichVisitor(context.Background(), v)
Expect(err).To(MatchError("<error>"))
})
})
Describe("func HandlerType()", func() {
It("returns ProcessHandlerType", func() {
Expect(cfg.HandlerType()).To(Equal(ProcessHandlerType))
})
})
Describe("func Handler()", func() {
It("returns the underlying handler", func() {
Expect(cfg.Handler()).To(BeIdenticalTo(handler))
})
})
When("the handler is disabled", func() {
BeforeEach(func() {
configure := handler.ConfigureFunc
handler.ConfigureFunc = func(c dogma.ProcessConfigurer) {
configure(c)
c.Disable()
}
})
Describe("func IsDisabled()", func() {
It("returns true", func() {
Expect(cfg.IsDisabled()).To(BeTrue())
})
})
})
})
DescribeTable(
"when the configuration is invalid",
func(
msg string,
fn func(dogma.ProcessConfigurer),
) {
handler.ConfigureFunc = fn
var err error
func() {
defer Recover(&err)
FromProcess(handler)
}()
Expect(err).Should(HaveOccurred())
if msg != "" {
Expect(err).To(MatchError(msg))
}
},
Entry(
"when the handler does not configure anything",
"", // any error
nil,
),
Entry(
"when the handler does not configure an identity",
`*stubs.ProcessMessageHandlerStub is configured without an identity, Identity() must be called exactly once within Configure()`,
func(c dogma.ProcessConfigurer) {
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
)
},
),
Entry(
"when the handler configures multiple identities",
`*stubs.ProcessMessageHandlerStub is configured with multiple identities (<name>/bea52cf4-e403-4b18-819d-88ade7836308 and <other>/bea52cf4-e403-4b18-819d-88ade7836308), Identity() must be called exactly once within Configure()`,
func(c dogma.ProcessConfigurer) {
c.Identity("<name>", processKey)
c.Identity("<other>", processKey)
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
)
},
),
Entry(
"when the handler configures an invalid name",
`*stubs.ProcessMessageHandlerStub is configured with an invalid identity, invalid name "\t \n", names must be non-empty, printable UTF-8 strings with no whitespace`,
func(c dogma.ProcessConfigurer) {
c.Identity("\t \n", processKey)
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
)
},
),
Entry(
"when the handler configures an invalid key",
`*stubs.ProcessMessageHandlerStub is configured with an invalid identity, invalid key "\t \n", keys must be RFC 4122 UUIDs`,
func(c dogma.ProcessConfigurer) {
c.Identity("<name>", "\t \n")
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
)
},
),
Entry(
"when the handler does not configure any event routes",
`*stubs.ProcessMessageHandlerStub (<name>) is not configured to handle any events, at least one HandlesEvent() route must be added within Configure()`,
func(c dogma.ProcessConfigurer) {
c.Identity("<name>", processKey)
c.Routes(
dogma.ExecutesCommand[CommandStub[TypeA]](),
)
},
),
Entry(
"when the handler configures multiple routes for the same event",
`*stubs.ProcessMessageHandlerStub (<name>) is configured with multiple HandlesEvent() routes for stubs.EventStub[TypeA], should these refer to different message types?`,
func(c dogma.ProcessConfigurer) {
c.Identity("<name>", processKey)
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
)
},
),
Entry(
"when the handler does not configure command routes",
`*stubs.ProcessMessageHandlerStub (<name>) is not configured to execute any commands, at least one ExecutesCommand() route must be added within Configure()`,
func(c dogma.ProcessConfigurer) {
c.Identity("<name>", processKey)
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
)
},
),
Entry(
"when the handler configures multiple routes for the same command",
`*stubs.ProcessMessageHandlerStub (<name>) is configured with multiple ExecutesCommand() routes for stubs.CommandStub[TypeA], should these refer to different message types?`,
func(c dogma.ProcessConfigurer) {
c.Identity("<name>", processKey)
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
)
},
),
Entry(
"when the handler configures multiple routes for the same timeout",
`*stubs.ProcessMessageHandlerStub (<name>) is configured with multiple SchedulesTimeout() routes for stubs.TimeoutStub[TypeA], should these refer to different message types?`,
func(c dogma.ProcessConfigurer) {
c.Identity("<name>", processKey)
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.ExecutesCommand[CommandStub[TypeA]](),
dogma.SchedulesTimeout[TimeoutStub[TypeA]](),
dogma.SchedulesTimeout[TimeoutStub[TypeA]](),
)
},
),
Entry(
"when an error occurs before the identity is configured it omits the handler name",
`*stubs.ProcessMessageHandlerStub is configured with multiple HandlesEvent() routes for stubs.EventStub[TypeA], should these refer to different message types?`,
func(c dogma.ProcessConfigurer) {
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
dogma.HandlesEvent[EventStub[TypeA]](),
)
},
),
)
})