This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
atomizer_mock_test.go
327 lines (260 loc) · 6.43 KB
/
atomizer_mock_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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package engine
import (
"context"
"encoding/json"
"fmt"
"sync"
"testing"
"github.com/Pallinder/go-randomdata"
"github.com/google/uuid"
"github.com/pkg/errors"
"go.devnw.com/alog"
"go.devnw.com/event"
"go.devnw.com/validator"
)
type tresult struct {
result string
electron *Electron
// err bool
// panic bool
}
var noopelectron = &Electron{
SenderID: "empty",
ID: "empty",
AtomID: "empty",
}
var noopinvalidelectron = &Electron{}
type invalidconductor struct{}
type noopconductor struct{}
func (*noopconductor) Receive(ctx context.Context) <-chan *Electron {
return nil
}
func (*noopconductor) Send(
ctx context.Context,
electron *Electron,
) (<-chan *Properties, error) {
return nil, nil
}
func (*noopconductor) Close() {}
func (*noopconductor) Complete(
ctx context.Context,
properties *Properties,
) error {
return nil
}
type noopatom struct{}
func (*noopatom) Process(
ctx context.Context,
conductor Conductor,
electron *Electron,
) ([]byte, error) {
return nil, nil
}
type panicatom struct{}
func (*panicatom) Process(
ctx context.Context,
conductor Conductor,
electron *Electron,
) ([]byte, error) {
panic("test panic")
}
type invalidatom struct{}
func (*invalidatom) Process(
ctx context.Context,
conductor Conductor,
electron *Electron,
) ([]byte, error) {
return nil, nil
}
func (*invalidatom) Validate() bool {
return false
}
type validconductor struct {
echan chan *Electron
valid bool
}
func (cond *validconductor) Receive(ctx context.Context) <-chan *Electron {
return cond.echan
}
func (cond *validconductor) Send(
ctx context.Context,
electron *Electron,
) (response <-chan *Properties, err error) {
return response, err
}
func (cond *validconductor) Validate() (valid bool) {
return cond.valid && cond.echan != nil
}
func (cond *validconductor) Complete(
ctx context.Context,
properties *Properties,
) (err error) {
return err
}
func (cond *validconductor) Close() {}
// TODO: Move passthrough as a conductor implementation for in-node processing
type passthrough struct {
input chan *Electron
results sync.Map
}
func (pt *passthrough) Receive(ctx context.Context) <-chan *Electron {
return pt.input
}
func (pt *passthrough) Validate() bool { return pt.input != nil }
func (pt *passthrough) Complete(ctx context.Context, p *Properties) error {
if !validator.Valid(p) {
return errors.Errorf(
"invalid properties returned for electron [%s]",
p.ElectronID,
)
}
// for rabbit mq drop properties onto the /basepath/electronid message path
value, ok := pt.results.Load(p.ElectronID)
if !ok {
return errors.Errorf(
"unable to load properties channel from sync map for electron [%s]",
p.ElectronID,
)
}
if value == nil {
return errors.Errorf(
"nil properties channel returned for electron [%s]",
p.ElectronID,
)
}
resultChan, ok := value.(chan *Properties)
if !ok {
return errors.New("unable to type assert electron properties channel")
}
defer close(resultChan)
// Push the properties of the instance onto the channel
select {
case <-ctx.Done():
return nil
case resultChan <- p:
}
return nil
}
func (pt *passthrough) Send(
ctx context.Context,
electron *Electron,
) (<-chan *Properties, error) {
var err error
result := make(chan *Properties)
go func(result chan *Properties) {
// Only kick off the electron for processing if there isn't already an
// instance loaded in the system
if _, loaded := pt.results.LoadOrStore(electron.ID, result); !loaded {
// Push the electron onto the input channel
select {
case <-ctx.Done():
return
case pt.input <- electron:
// setup a monitoring thread for /basepath/electronid
}
} else {
defer close(result)
p := &Properties{}
alog.Errorf(nil, "duplicate electron registration for EID [%s]", electron.ID)
result <- p
}
}(result)
return result, err
}
func (pt *passthrough) Close() {}
type printer struct{ t *testing.T }
type state struct{ ID string }
func (s *state) Process(ctx context.Context, conductor Conductor, electron *Electron) (result []byte, err error) {
return []byte(s.ID), nil
}
func (p *printer) Process(ctx context.Context, conductor Conductor, electron *Electron) (result []byte, err error) {
if validator.Valid(electron) {
var payload printerdata
if err = json.Unmarshal(electron.Payload, &payload); err == nil {
p.t.Logf("message from electron [%s] is: %s\n", electron.ID, payload.Message)
}
}
return result, err
}
type returner struct{}
func (b *returner) Process(ctx context.Context, conductor Conductor, electron *Electron) (result []byte, err error) {
if !validator.Valid(electron) {
return nil, errors.New("invalid electron")
}
var payload = &printerdata{}
err = json.Unmarshal(electron.Payload, payload)
if err != nil {
return nil, err
}
result = []byte(payload.Message)
alog.Println("returning payload")
return result, err
}
func spawnReturner(size int) (tests []*tresult) {
tests = make([]*tresult, 0, size)
for i := 0; i < size; i++ {
msg := randomdata.SillyName()
e := newElectron(
ID(returner{}),
[]byte(fmt.Sprintf("{\"message\":%q}", msg)),
)
tests = append(tests, &tresult{
result: msg,
electron: e,
})
}
return tests
}
type printerdata struct {
Message string `json:"message"`
}
func newElectron(atomID string, payload []byte) *Electron {
return &Electron{
SenderID: uuid.New().String(),
ID: uuid.New().String(),
AtomID: atomID,
Payload: payload,
}
}
// harness creates a valid atomizer that uses the passthrough conductor
func harness(
ctx context.Context,
buffer int,
atoms ...Atom,
) (Conductor, event.EventStream, error) {
pass := &passthrough{
input: make(chan *Electron, 1),
}
// Register the conductor so it's picked up
// when the atomizer is initialized
if err := Register(pass); err != nil {
return nil, nil, err
}
// Test Atom registrations
if err := Register(&printer{}); err != nil {
return nil, nil, err
}
if err := Register(&noopatom{}); err != nil {
return nil, nil, err
}
if err := Register(&returner{}); err != nil {
return nil, nil, err
}
for _, a := range atoms {
if err := Register(a); err != nil {
return nil, nil, err
}
}
// Initialize the atomizer
mizer, err := Atomize(ctx)
if err != nil {
return nil, nil, fmt.Errorf("error creating atomizer | %s", err)
}
a, _ := mizer.(*atomizer)
var events event.EventStream
if buffer >= 0 {
events = a.Events(buffer)
}
// Start the execution threads
return pass, events, a.Exec()
}