-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsink_test.go
347 lines (310 loc) · 9.35 KB
/
sink_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Tideland Go Cells - Unit Tests
//
// Copyright (C) 2010-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package cells_test
//--------------------
// IMPORTS
//--------------------
import (
stderr "errors"
"sort"
"testing"
"time"
"github.com/tideland/golib/audit"
"github.com/tideland/gocells/cells"
)
//--------------------
// TESTS
//--------------------
// TestEventSink tests the simple event sink.
func TestEventSink(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
checkTopic := func(event cells.Event) {
assert.Contents(event.Topic(), topics)
}
// Empty sink.
sink := cells.NewEventSink(0)
first, ok := sink.PeekFirst()
assert.Nil(first)
assert.False(ok)
last, ok := sink.PeekLast()
assert.Nil(last)
assert.False(ok)
at, ok := sink.PeekAt(-1)
assert.Nil(at)
assert.False(ok)
at, ok = sink.PeekAt(4711)
assert.Nil(at)
assert.False(ok)
// Limited number of events.
sink = cells.NewEventSink(5)
addEvents(assert, 10, sink)
assert.Length(sink, 5)
// Unlimited number of events.
sink = cells.NewEventSink(0)
addEvents(assert, 10, sink)
assert.Length(sink, 10)
first, ok = sink.PeekFirst()
assert.True(ok)
checkTopic(first)
last, ok = sink.PeekLast()
assert.True(ok)
checkTopic(last)
for i := 0; i < sink.Len(); i++ {
at, ok = sink.PeekAt(i)
assert.True(ok)
checkTopic(at)
}
}
// TestEventSinkIteration tests the event sink iteration.
func TestEventSinkIteration(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
sink := cells.NewEventSink(0)
addEvents(assert, 10, sink)
assert.Length(sink, 10)
err := sink.Do(func(index int, event cells.Event) error {
assert.Contents(event.Topic(), topics)
var payload int
err := event.Payload().Unmarshal(&payload)
assert.Nil(err)
assert.Range(payload, 1, 9)
return nil
})
assert.Nil(err)
ok, err := cells.NewEventSinkAnalyzer(sink).Match(func(index int, event cells.Event) (bool, error) {
topicOK := event.Topic() >= "a" && event.Topic() <= "j"
var payload int
err := event.Payload().Unmarshal(&payload)
assert.Nil(err)
payloadOK := payload >= 1 && payload <= 9
return topicOK && payloadOK, nil
})
assert.Nil(err)
assert.True(ok)
}
// TestEventSinkIterationError tests the event sink iteration error.
func TestEventSinkIterationError(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
sink := cells.NewEventSink(0)
addEvents(assert, 10, sink)
err := sink.Do(func(index int, event cells.Event) error {
return stderr.New("ouch")
})
assert.ErrorMatch(err, "ouch")
ok, err := cells.NewEventSinkAnalyzer(sink).Match(func(index int, event cells.Event) (bool, error) {
// The bool true won't be passed to outside.
return true, stderr.New("ouch")
})
assert.False(ok)
assert.ErrorMatch(err, "ouch")
}
// TestCheckedEventSink tests the checking of new events.
func TestCheckedEventSink(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
payloadc := audit.MakeSigChan()
donec := audit.MakeSigChan()
count := 0
wanted := []string{"f", "c", "c"}
checker := func(events cells.EventSinkAccessor) (cells.Payload, error) {
count++
defer func() {
if count == 100 {
donec <- struct{}{}
}
}()
if events.Len() < len(wanted) {
return nil, nil
}
ok, err := cells.NewEventSinkAnalyzer(events).Match(func(index int, event cells.Event) (bool, error) {
return event.Topic() == wanted[index], nil
})
if err != nil {
return nil, err
}
if ok {
first, _ := events.PeekFirst()
last, _ := events.PeekLast()
payload := last.Timestamp().Sub(first.Timestamp())
payloadc <- payload
}
return nil, nil
}
sink := cells.NewCheckedEventSink(3, checker)
go addEvents(assert, 100, sink)
for {
select {
case payload := <-payloadc:
d, ok := payload.(time.Duration)
assert.True(ok)
assert.True(d > 0)
case <-donec:
return
case <-time.After(5 * time.Second):
assert.Fail()
}
}
}
// TestEventSinkAnalyzer tests analyzing an event sink.
func TestEventSinkAnalyzer(t *testing.T) {
assert := audit.NewTestingAssertion(t, true)
sink := cells.NewEventSink(0)
analyzer := cells.NewEventSinkAnalyzer(sink)
addEvents(assert, 100, sink)
// Check filtering.
fchecker := func(index int, event cells.Event) (bool, error) {
if event.Topic() == "f" {
return true, nil
}
return false, nil
}
ferrchecker := func(index int, event cells.Event) (bool, error) {
return false, stderr.New("ouch")
}
fs, err := analyzer.Filter(fchecker)
assert.Nil(err)
assert.True(fs.Len() < sink.Len(), "less events with topic f than total number")
fs, err = analyzer.Filter(ferrchecker)
assert.ErrorMatch(err, "ouch", "error is returned correctly")
// Check matching.
fs, err = analyzer.Filter(fchecker)
assert.Nil(err)
ok, err := cells.NewEventSinkAnalyzer(fs).Match(fchecker)
assert.Nil(err)
assert.True(ok, "all events in fs do have topic f")
ok, err = cells.NewEventSinkAnalyzer(fs).Match(ferrchecker)
assert.ErrorMatch(err, "ouch", "error is returned correctly")
// Check folding.
count := 0
ffolder := func(index int, acc interface{}, event cells.Event) (interface{}, error) {
if event.Topic() == "f" {
count++
fs, ok := acc.(int)
if !ok {
return nil, stderr.New("ouch")
}
return fs + 1, nil
}
return acc, nil
}
ferrfolder := func(index int, acc interface{}, event cells.Event) (interface{}, error) {
return nil, stderr.New("ouch")
}
fcount, err := analyzer.Fold(0, ffolder)
assert.Nil(err)
assert.Equal(fcount, count, "accumulator has been updated correctly")
fcount, err = analyzer.Fold(0, ferrfolder)
assert.ErrorMatch(err, "ouch", "error is returned correctly")
count = 0
fpfolder := func(index int, acc cells.Payload, event cells.Event) (cells.Payload, error) {
if event.Topic() == "f" {
count++
payload, err := cells.NewPayload(acc.String() + event.Topic())
if err != nil {
return nil, err
}
return payload, nil
}
return acc, nil
}
fperrfolder := func(index int, acc cells.Payload, event cells.Event) (cells.Payload, error) {
return nil, stderr.New("ouch")
}
initial, err := cells.NewPayload("")
assert.Nil(err)
fpcount, err := analyzer.FoldPayload(initial, fpfolder)
assert.Nil(err)
assert.Length(fpcount, count, "payload accumulator has been updated correctly")
initial, err = cells.NewPayload("")
assert.Nil(err)
fpcount, err = analyzer.FoldPayload(initial, fperrfolder)
assert.ErrorMatch(err, "ouch", "error is returned correctly")
// Check total duration.
dsink := cells.NewEventSink(0)
danalyzer := cells.NewEventSinkAnalyzer(dsink)
duration := danalyzer.TotalDuration()
assert.Equal(duration, 0*time.Nanosecond, "empty sink has no duration")
addEvents(assert, 1, dsink)
duration = danalyzer.TotalDuration()
assert.Equal(duration, 0*time.Nanosecond, "sink containing one event has no duration")
addEvents(assert, 1, dsink)
first, ok := dsink.PeekFirst()
assert.True(ok)
last, ok := dsink.PeekLast()
assert.True(ok)
duration = danalyzer.TotalDuration()
assert.Equal(duration, last.Timestamp().Sub(first.Timestamp()), "total duration calculated correctly")
// Check minimum/maximum duration.
durations := []time.Duration{}
timestamp := time.Time{}
sink.Do(func(index int, event cells.Event) error {
if index == 0 {
timestamp = event.Timestamp()
return nil
}
duration = event.Timestamp().Sub(timestamp)
durations = append(durations, duration)
timestamp = event.Timestamp()
return nil
})
sort.Slice(durations, func(i, j int) bool {
return durations[i] < durations[j]
})
dmin := durations[0]
dmax := durations[len(durations)-1]
min, max := analyzer.MinMaxDuration()
assert.Equal(min, dmin, "minimum duration is correct")
assert.Equal(max, dmax, "maximum duration is correct")
// Check topic quantities.
quantities := analyzer.TopicQuantities()
assert.Length(quantities, len(topics))
for topic, quantity := range quantities {
assert.Contents(topic, topics, "topic is one of the topics")
assert.Range(quantity, 1, 100, "quantity is in range")
}
tfolder := func(index int, acc interface{}, event cells.Event) (interface{}, error) {
if acc == nil {
return 0, nil
}
count, ok := acc.(int)
if !ok {
return nil, stderr.New("ouch")
}
return count + 1, nil
}
terrfolder := func(index int, acc interface{}, event cells.Event) (interface{}, error) {
return nil, stderr.New("ouch")
}
counts, err := analyzer.TopicFolds(tfolder)
assert.Nil(err)
assert.Length(quantities, len(topics))
for topic, count := range counts {
assert.Contents(topic, topics, "topic is one of the topics")
assert.Range(count, 1, 100, "quantity is in range")
}
counts, err = analyzer.TopicFolds(terrfolder)
assert.ErrorMatch(err, "ouch", "error is returned correctly")
}
//--------------------
// HELPER
//--------------------
// topics contains the test topics.
var topics = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
// addEvents adds a number of events to a sink.
func addEvents(assert audit.Assertion, count int, sink cells.EventSink) {
generator := audit.NewGenerator(audit.FixedRand())
for i := 0; i < count; i++ {
topic := generator.OneStringOf(topics...)
payload := generator.OneIntOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
sleep := generator.Duration(2*time.Millisecond, 4*time.Millisecond)
event, err := cells.NewEvent(topic, payload)
assert.Nil(err)
n, err := sink.Push(event)
assert.Nil(err)
assert.True(n > 0)
time.Sleep(sleep)
}
}
// EOF