@@ -9,8 +9,12 @@ import (
99 "github.com/stretchr/testify/assert"
1010)
1111
12+ func close (wg * sync.WaitGroup , cancelFn context.CancelFunc ) {
13+ cancelFn ()
14+ wg .Wait ()
15+ }
1216func TestDefaultEventProcessor_ProcessImpression (t * testing.T ) {
13- ctx := context .Background ()
17+ ctx , cancelFn := context .WithCancel ( context . Background () )
1418 var wg sync.WaitGroup
1519 processor := NewEventProcessor (ctx , 10 , 100 , 100 , & wg )
1620
@@ -20,7 +24,7 @@ func TestDefaultEventProcessor_ProcessImpression(t *testing.T) {
2024
2125 assert .Equal (t , 1 , processor .EventsCount ())
2226
23- time . Sleep ( 2000 * time . Millisecond )
27+ close ( & wg , cancelFn )
2428
2529 assert .NotNil (t , processor .Ticker )
2630
@@ -38,6 +42,7 @@ func (f *MockDispatcher) DispatchEvent(event LogEvent) (bool, error) {
3842
3943func TestDefaultEventProcessor_ProcessBatch (t * testing.T ) {
4044 var wg sync.WaitGroup
45+ ctx , cancelFn := context .WithCancel (context .Background ())
4146 processor := & QueueingEventProcessor {
4247 MaxQueueSize : 100 ,
4348 FlushInterval : 100 ,
@@ -46,7 +51,7 @@ func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
4651 wg : & wg ,
4752 }
4853 processor .BatchSize = 10
49- processor .StartTicker (context . TODO () )
54+ processor .StartTicker (ctx )
5055
5156 impression := BuildTestImpressionEvent ()
5257 conversion := BuildTestConversionEvent ()
@@ -58,7 +63,7 @@ func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
5863
5964 assert .Equal (t , 4 , processor .EventsCount ())
6065
61- time . Sleep ( 200 * time . Millisecond )
66+ close ( & wg , cancelFn )
6267
6368 assert .NotNil (t , processor .Ticker )
6469
@@ -96,18 +101,15 @@ func TestBatchEventProcessor_FlushesOnClose(t *testing.T) {
96101
97102 assert .Equal (t , 4 , processor .EventsCount ())
98103
99- time .Sleep (500 * time .Millisecond )
100-
101104 // Triggers the flush in the processor
102- cancelFn ()
103-
104- time .Sleep (500 * time .Millisecond )
105+ close (& wg , cancelFn )
105106
106107 assert .Equal (t , 0 , processor .EventsCount ())
107108}
108109
109110func TestDefaultEventProcessor_ProcessBatchRevisionMismatch (t * testing.T ) {
110111 var wg sync.WaitGroup
112+ ctx , cancelFn := context .WithCancel (context .Background ())
111113 processor := & QueueingEventProcessor {
112114 MaxQueueSize : 100 ,
113115 FlushInterval : 100 ,
@@ -116,7 +118,7 @@ func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
116118 wg : & wg ,
117119 }
118120 processor .BatchSize = 10
119- processor .StartTicker (context . TODO () )
121+ processor .StartTicker (ctx )
120122
121123 impression := BuildTestImpressionEvent ()
122124 conversion := BuildTestConversionEvent ()
@@ -129,7 +131,7 @@ func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
129131
130132 assert .Equal (t , 4 , processor .EventsCount ())
131133
132- time . Sleep ( 200 * time . Millisecond )
134+ close ( & wg , cancelFn )
133135
134136 assert .NotNil (t , processor .Ticker )
135137
@@ -146,6 +148,7 @@ func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
146148
147149func TestDefaultEventProcessor_ProcessBatchProjectMismatch (t * testing.T ) {
148150 var wg sync.WaitGroup
151+ ctx , cancelFn := context .WithCancel (context .Background ())
149152 processor := & QueueingEventProcessor {
150153 MaxQueueSize : 100 ,
151154 FlushInterval : 100 ,
@@ -154,7 +157,7 @@ func TestDefaultEventProcessor_ProcessBatchProjectMismatch(t *testing.T) {
154157 wg : & wg ,
155158 }
156159 processor .BatchSize = 10
157- processor .StartTicker (context . TODO () )
160+ processor .StartTicker (ctx )
158161
159162 impression := BuildTestImpressionEvent ()
160163 conversion := BuildTestConversionEvent ()
@@ -167,7 +170,7 @@ func TestDefaultEventProcessor_ProcessBatchProjectMismatch(t *testing.T) {
167170
168171 assert .Equal (t , 4 , processor .EventsCount ())
169172
170- time . Sleep ( 200 * time . Millisecond )
173+ close ( & wg , cancelFn )
171174
172175 assert .NotNil (t , processor .Ticker )
173176
@@ -184,6 +187,7 @@ func TestDefaultEventProcessor_ProcessBatchProjectMismatch(t *testing.T) {
184187
185188func TestChanQueueEventProcessor_ProcessImpression (t * testing.T ) {
186189 var wg sync.WaitGroup
190+ ctx , cancelFn := context .WithCancel (context .Background ())
187191 processor := & QueueingEventProcessor {
188192 MaxQueueSize : 100 ,
189193 FlushInterval : 100 ,
@@ -192,15 +196,15 @@ func TestChanQueueEventProcessor_ProcessImpression(t *testing.T) {
192196 wg : & wg ,
193197 }
194198 processor .BatchSize = 10
195- processor .StartTicker (context . TODO () )
199+ processor .StartTicker (ctx )
196200
197201 impression := BuildTestImpressionEvent ()
198202
199203 processor .ProcessEvent (impression )
200204 processor .ProcessEvent (impression )
201205 processor .ProcessEvent (impression )
202206
203- time . Sleep ( 3000 * time . Millisecond )
207+ close ( & wg , cancelFn )
204208
205209 assert .NotNil (t , processor .Ticker )
206210
@@ -209,9 +213,10 @@ func TestChanQueueEventProcessor_ProcessImpression(t *testing.T) {
209213
210214func TestChanQueueEventProcessor_ProcessBatch (t * testing.T ) {
211215 var wg sync.WaitGroup
216+ ctx , cancelFn := context .WithCancel (context .Background ())
212217 processor := & QueueingEventProcessor {MaxQueueSize : 100 , FlushInterval : 100 , Q : NewChanQueue (100 ), EventDispatcher : & MockDispatcher {}, wg : & wg }
213218 processor .BatchSize = 10
214- processor .StartTicker (context . TODO () )
219+ processor .StartTicker (ctx )
215220
216221 impression := BuildTestImpressionEvent ()
217222 conversion := BuildTestConversionEvent ()
@@ -221,14 +226,12 @@ func TestChanQueueEventProcessor_ProcessBatch(t *testing.T) {
221226 processor .ProcessEvent (conversion )
222227 processor .ProcessEvent (conversion )
223228
224- time . Sleep ( 3000 * time . Millisecond )
229+ close ( & wg , cancelFn )
225230
226231 assert .NotNil (t , processor .Ticker )
227232
228233 assert .Equal (t , 0 , processor .EventsCount ())
229234
230- time .Sleep (3000 * time .Millisecond )
231-
232235 result , ok := (processor .EventDispatcher ).(* MockDispatcher )
233236
234237 if ok {
0 commit comments