Skip to content

Commit d245a2f

Browse files
author
Mike Davis
committed
Larger test refactor to mitigate race.
1 parent c4c5aae commit d245a2f

File tree

1 file changed

+40
-75
lines changed

1 file changed

+40
-75
lines changed

pkg/event/processor_test.go

Lines changed: 40 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ func TestDefaultEventProcessor_LogEventNotification(t *testing.T) {
139139

140140
func TestDefaultEventProcessor_DefaultConfig(t *testing.T) {
141141
eg := newExecutionContext()
142+
dispatcher := NewMockDispatcher(100, false)
142143
processor := NewBatchEventProcessor(
143-
WithEventDispatcher(NewMockDispatcher(100, false)),
144+
WithEventDispatcher(dispatcher),
144145
// here we are setting the timing interval so that we don't have to wait the default 30 seconds
145146
WithFlushInterval(500*time.Millisecond))
146147
eg.Go(processor.Start)
@@ -158,27 +159,21 @@ func TestDefaultEventProcessor_DefaultConfig(t *testing.T) {
158159
// sleep for 1 second here. to allow event processor to run.
159160
time.Sleep(1 * time.Second)
160161

161-
assert.NotNil(t, processor.Ticker)
162-
163162
assert.Equal(t, 0, processor.eventsCount())
164-
165-
result, ok := (processor.EventDispatcher).(*MockDispatcher)
166-
167-
if ok {
168-
assert.Equal(t, 1, result.Events.Size())
169-
evs := result.Events.Get(1)
170-
logEvent, _ := evs[0].(LogEvent)
171-
assert.Equal(t, 4, len(logEvent.Event.Visitors))
172-
}
163+
assert.Equal(t, 1, dispatcher.Events.Size())
164+
evs := dispatcher.Events.Get(1)
165+
logEvent, _ := evs[0].(LogEvent)
166+
assert.Equal(t, 4, len(logEvent.Event.Visitors))
173167
}
174168

175169
func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
176170
eg := newExecutionContext()
171+
dispatcher := NewMockDispatcher(100, false)
177172
processor := NewBatchEventProcessor(
178173
WithFlushInterval(1*time.Second),
179174
WithQueueSize(100),
180175
WithQueue(NewInMemoryQueue(100)),
181-
WithEventDispatcher(NewMockDispatcher(100, false)))
176+
WithEventDispatcher(dispatcher))
182177
eg.Go(processor.Start)
183178

184179
impression := BuildTestImpressionEvent()
@@ -193,27 +188,21 @@ func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
193188

194189
time.Sleep(1500 * time.Millisecond)
195190

196-
assert.NotNil(t, processor.Ticker)
197-
198191
assert.Equal(t, 0, processor.eventsCount())
199-
200-
result, ok := (processor.EventDispatcher).(*MockDispatcher)
201-
202-
if ok {
203-
assert.Equal(t, 1, result.Events.Size())
204-
evs := result.Events.Get(1)
205-
logEvent, _ := evs[0].(LogEvent)
206-
assert.Equal(t, 4, len(logEvent.Event.Visitors))
207-
}
192+
assert.Equal(t, 1, dispatcher.Events.Size())
193+
evs := dispatcher.Events.Get(1)
194+
logEvent, _ := evs[0].(LogEvent)
195+
assert.Equal(t, 4, len(logEvent.Event.Visitors))
208196
}
209197

210198
func TestDefaultEventProcessor_BatchSizeMet(t *testing.T) {
211199
eg := newExecutionContext()
200+
dispatcher := NewMockDispatcher(100, false)
212201
processor := NewBatchEventProcessor(
213202
WithBatchSize(2),
214203
WithFlushInterval(1000*time.Millisecond),
215204
WithQueue(NewInMemoryQueue(2)),
216-
WithEventDispatcher(NewMockDispatcher(100, false)))
205+
WithEventDispatcher(dispatcher))
217206
eg.Go(processor.Start)
218207

219208
impression := BuildTestImpressionEvent()
@@ -226,15 +215,10 @@ func TestDefaultEventProcessor_BatchSizeMet(t *testing.T) {
226215

227216
time.Sleep(100 * time.Millisecond)
228217

229-
result, ok := (processor.EventDispatcher).(*MockDispatcher)
230-
231-
if ok {
232-
assert.Equal(t, 1, result.Events.Size())
233-
evs := result.Events.Get(1)
234-
logEvent, _ := evs[0].(LogEvent)
235-
assert.Equal(t, 2, len(logEvent.Event.Visitors))
236-
237-
}
218+
assert.Equal(t, 1, dispatcher.Events.Size())
219+
evs := dispatcher.Events.Get(1)
220+
logEvent, _ := evs[0].(LogEvent)
221+
assert.Equal(t, 2, len(logEvent.Event.Visitors))
238222

239223
processor.ProcessEvent(conversion)
240224
processor.ProcessEvent(conversion)
@@ -246,10 +230,7 @@ func TestDefaultEventProcessor_BatchSizeMet(t *testing.T) {
246230
assert.NotNil(t, processor.Ticker)
247231

248232
assert.Equal(t, 0, processor.eventsCount())
249-
250-
if ok {
251-
assert.Equal(t, 2, result.Events.Size())
252-
}
233+
assert.Equal(t, 2, dispatcher.Events.Size())
253234
}
254235

255236
func TestDefaultEventProcessor_BatchSizeLessThanQSize(t *testing.T) {
@@ -290,11 +271,12 @@ func TestDefaultEventProcessor_QSizeExceeded(t *testing.T) {
290271

291272
func TestDefaultEventProcessor_FailedDispatch(t *testing.T) {
292273
eg := newExecutionContext()
274+
dispatcher := &MockDispatcher{ShouldFail: true, Events: NewInMemoryQueue(100)}
293275
processor := NewBatchEventProcessor(
294276
WithQueueSize(100),
295277
WithFlushInterval(100),
296278
WithQueue(NewInMemoryQueue(100)),
297-
WithEventDispatcher(&MockDispatcher{ShouldFail: true, Events: NewInMemoryQueue(100)}))
279+
WithEventDispatcher(dispatcher))
298280
eg.Go(processor.Start)
299281

300282
impression := BuildTestImpressionEvent()
@@ -312,12 +294,7 @@ func TestDefaultEventProcessor_FailedDispatch(t *testing.T) {
312294
assert.NotNil(t, processor.Ticker)
313295

314296
assert.Equal(t, 4, processor.eventsCount())
315-
316-
result, ok := (processor.EventDispatcher).(*MockDispatcher)
317-
318-
if ok {
319-
assert.Equal(t, 0, result.Events.Size())
320-
}
297+
assert.Equal(t, 0, dispatcher.Events.Size())
321298
}
322299

323300
func TestBatchEventProcessor_FlushesOnClose(t *testing.T) {
@@ -346,10 +323,11 @@ func TestBatchEventProcessor_FlushesOnClose(t *testing.T) {
346323

347324
func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
348325
eg := newExecutionContext()
326+
dispatcher := NewMockDispatcher(100, false)
349327
processor := NewBatchEventProcessor(
350328
WithQueueSize(100),
351329
WithQueue(NewInMemoryQueue(100)),
352-
WithEventDispatcher(NewMockDispatcher(100, false)))
330+
WithEventDispatcher(dispatcher))
353331

354332
eg.Go(processor.Start)
355333

@@ -369,23 +347,19 @@ func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
369347
assert.NotNil(t, processor.Ticker)
370348

371349
assert.Equal(t, 0, processor.eventsCount())
372-
373-
result, ok := (processor.EventDispatcher).(*MockDispatcher)
374-
375-
if ok {
376-
assert.Equal(t, 3, result.Events.Size())
377-
evs := result.Events.Get(3)
378-
logEvent, _ := evs[len(evs)-1].(LogEvent)
379-
assert.Equal(t, 2, len(logEvent.Event.Visitors))
380-
}
350+
assert.Equal(t, 3, dispatcher.Events.Size())
351+
evs := dispatcher.Events.Get(3)
352+
logEvent, _ := evs[len(evs)-1].(LogEvent)
353+
assert.Equal(t, 2, len(logEvent.Event.Visitors))
381354
}
382355

383356
func TestDefaultEventProcessor_ProcessBatchProjectMismatch(t *testing.T) {
384357
eg := newExecutionContext()
358+
dispatcher := NewMockDispatcher(100, false)
385359
processor := NewBatchEventProcessor(
386360
WithQueueSize(100),
387361
WithQueue(NewInMemoryQueue(100)),
388-
WithEventDispatcher(NewMockDispatcher(100, false)))
362+
WithEventDispatcher(dispatcher))
389363

390364
eg.Go(processor.Start)
391365

@@ -405,15 +379,10 @@ func TestDefaultEventProcessor_ProcessBatchProjectMismatch(t *testing.T) {
405379
assert.NotNil(t, processor.Ticker)
406380

407381
assert.Equal(t, 0, processor.eventsCount())
408-
409-
result, ok := (processor.EventDispatcher).(*MockDispatcher)
410-
411-
if ok {
412-
assert.Equal(t, 3, result.Events.Size())
413-
evs := result.Events.Get(3)
414-
logEvent, _ := evs[len(evs)-1].(LogEvent)
415-
assert.Equal(t, 2, len(logEvent.Event.Visitors))
416-
}
382+
assert.Equal(t, 3, dispatcher.Events.Size())
383+
evs := dispatcher.Events.Get(3)
384+
logEvent, _ := evs[len(evs)-1].(LogEvent)
385+
assert.Equal(t, 2, len(logEvent.Event.Visitors))
417386
}
418387

419388
func TestChanQueueEventProcessor_ProcessImpression(t *testing.T) {
@@ -439,10 +408,11 @@ func TestChanQueueEventProcessor_ProcessImpression(t *testing.T) {
439408

440409
func TestChanQueueEventProcessor_ProcessBatch(t *testing.T) {
441410
eg := newExecutionContext()
411+
dispatcher := NewMockDispatcher(100, false)
442412
processor := NewBatchEventProcessor(
443413
WithQueueSize(100),
444414
WithQueue(NewInMemoryQueue(100)),
445-
WithEventDispatcher(NewMockDispatcher(100, false)))
415+
WithEventDispatcher(dispatcher))
446416
eg.Go(processor.Start)
447417

448418
impression := BuildTestImpressionEvent()
@@ -458,15 +428,10 @@ func TestChanQueueEventProcessor_ProcessBatch(t *testing.T) {
458428
assert.NotNil(t, processor.Ticker)
459429

460430
assert.Equal(t, 0, processor.eventsCount())
461-
462-
result, ok := (processor.EventDispatcher).(*MockDispatcher)
463-
464-
if ok {
465-
assert.Equal(t, 1, result.Events.Size())
466-
evs := result.Events.Get(1)
467-
logEvent, _ := evs[0].(LogEvent)
468-
assert.True(t, len(logEvent.Event.Visitors) >= 1)
469-
}
431+
assert.Equal(t, 1, dispatcher.Events.Size())
432+
evs := dispatcher.Events.Get(1)
433+
logEvent, _ := evs[0].(LogEvent)
434+
assert.True(t, len(logEvent.Event.Visitors) >= 1)
470435
}
471436

472437
// The NoOpLogger is used during benchmarking so that results are printed nicely.

0 commit comments

Comments
 (0)