Skip to content

Commit 1228981

Browse files
author
Mike Davis
authored
refact(test): update test to fix flaky assertion. (#276)
1 parent ebb4026 commit 1228981

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

pkg/event/dispatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ func (ed *QueueEventDispatcher) flushEvents() {
144144
} else {
145145
ed.logger.Warning("dispatch event failed")
146146
// we failed. Sleep some seconds and try again.
147-
time.Sleep(sleepTime)
148147
// increase retryCount. We exit if we have retried x times.
149148
// we will retry again next event that is added.
150149
retryCount++
151150
ed.retryFlushCounter.Add(1)
151+
time.Sleep(sleepTime)
152152
}
153153
} else {
154154
ed.logger.Error("Error dispatching ", err)

pkg/event/dispatcher_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,13 @@ func TestQueueEventDispatcher_FailDispath(t *testing.T) {
185185

186186
assert.Equal(t, 1, q.eventQueue.Size())
187187

188-
// give the queue a chance to run
189-
q.flushEvents()
190-
time.Sleep(1 * time.Second)
188+
// give the queue a chance to run the queue is drained asynchronously
189+
retryCount, _ := metricsRegistry.GetCounter(metrics.DispatcherRetryFlush).(*MetricsCounter)
190+
assert.Eventually(t, func() bool { return retryCount.Get() > 1 }, 5*time.Second, 1*time.Second)
191191

192-
// check the queue. bad event type should be removed. but, not sent.
192+
// check the queue. the event should still be in the queue
193193
assert.Equal(t, 1, q.eventQueue.Size())
194194

195195
assert.Equal(t, float64(1), metricsRegistry.GetGauge(metrics.DispatcherQueueSize).(*MetricsGauge).Get())
196196
assert.Equal(t, float64(0), metricsRegistry.GetCounter(metrics.DispatcherSuccessFlush).(*MetricsCounter).Get())
197-
assert.True(t, metricsRegistry.GetCounter(metrics.DispatcherRetryFlush).(*MetricsCounter).Get() > 1)
198-
199-
q.flushEvents()
200-
201-
// check the queue. bad event type should be removed. but, not sent.
202-
assert.Equal(t, 1, q.eventQueue.Size())
203197
}

0 commit comments

Comments
 (0)