@@ -32,12 +32,12 @@ type MockDispatcher struct {
3232 Events Queue
3333}
3434
35- func (f * MockDispatcher ) DispatchEvent (event LogEvent ) (bool , error ) {
36- if f .ShouldFail {
35+ func (m * MockDispatcher ) DispatchEvent (event LogEvent ) (bool , error ) {
36+ if m .ShouldFail {
3737 return false , errors .New ("Failed to dispatch" )
3838 }
3939
40- f .Events .Add (event )
40+ m .Events .Add (event )
4141 return true , nil
4242}
4343
@@ -116,10 +116,44 @@ func TestDefaultEventProcessor_LogEventNotification(t *testing.T) {
116116 assert .Nil (t , err )
117117}
118118
119+ func TestDefaultEventProcessor_DefaultConfig (t * testing.T ) {
120+ exeCtx := utils .NewCancelableExecutionCtx ()
121+ processor := NewBatchEventProcessor (WithEventDispatcher (NewMockDispatcher (100 , false )))
122+ processor .Start (exeCtx )
123+
124+ impression := BuildTestImpressionEvent ()
125+ conversion := BuildTestConversionEvent ()
126+
127+ processor .ProcessEvent (impression )
128+ processor .ProcessEvent (impression )
129+ processor .ProcessEvent (conversion )
130+ processor .ProcessEvent (conversion )
131+
132+ assert .Equal (t , 4 , processor .EventsCount ())
133+
134+ time .Sleep (31 * time .Second )
135+
136+ assert .NotNil (t , processor .Ticker )
137+
138+ assert .Equal (t , 0 , processor .EventsCount ())
139+
140+ result , ok := (processor .EventDispatcher ).(* MockDispatcher )
141+
142+ if ok {
143+ assert .Equal (t , 1 , result .Events .Size ())
144+ evs := result .Events .Get (1 )
145+ logEvent , _ := evs [0 ].(LogEvent )
146+ assert .Equal (t , 4 , len (logEvent .Event .Visitors ))
147+ }
148+ }
149+
119150func TestDefaultEventProcessor_ProcessBatch (t * testing.T ) {
120151 exeCtx := utils .NewCancelableExecutionCtx ()
121- processor := NewBatchEventProcessor (WithFlushInterval (100 ), WithQueueSize (100 ),
122- WithQueue (NewInMemoryQueue (100 )), WithEventDispatcher (NewMockDispatcher (100 , false )))
152+ processor := NewBatchEventProcessor (
153+ WithFlushInterval (1 * time .Second ),
154+ WithQueueSize (100 ),
155+ WithQueue (NewInMemoryQueue (100 )),
156+ WithEventDispatcher (NewMockDispatcher (100 , false )))
123157 processor .Start (exeCtx )
124158
125159 impression := BuildTestImpressionEvent ()
@@ -132,7 +166,7 @@ func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
132166
133167 assert .Equal (t , 4 , processor .EventsCount ())
134168
135- exeCtx . TerminateAndWait ( )
169+ time . Sleep ( 1500 * time . Millisecond )
136170
137171 assert .NotNil (t , processor .Ticker )
138172
@@ -148,10 +182,13 @@ func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
148182 }
149183}
150184
151- func TestDefaultEventProcessor_QSizeMet (t * testing.T ) {
185+ func TestDefaultEventProcessor_BatchSizeMet (t * testing.T ) {
152186 exeCtx := utils .NewCancelableExecutionCtx ()
153- processor := NewBatchEventProcessor (WithQueueSize (2 ), WithFlushInterval (1000 ),
154- WithQueue (NewInMemoryQueue (2 )), WithEventDispatcher (NewMockDispatcher (100 , false )))
187+ processor := NewBatchEventProcessor (
188+ WithBatchSize (2 ),
189+ WithFlushInterval (1000 * time .Millisecond ),
190+ WithQueue (NewInMemoryQueue (2 )),
191+ WithEventDispatcher (NewMockDispatcher (100 , false )))
155192 processor .Start (exeCtx )
156193
157194 impression := BuildTestImpressionEvent ()
@@ -190,10 +227,49 @@ func TestDefaultEventProcessor_QSizeMet(t *testing.T) {
190227 }
191228}
192229
230+ func TestDefaultEventProcessor_BatchSizeLessThanQSize (t * testing.T ) {
231+ processor := NewBatchEventProcessor (
232+ WithQueueSize (2 ),
233+ WithFlushInterval (1000 * time .Millisecond ),
234+ WithQueue (NewInMemoryQueue (100 )),
235+ WithEventDispatcher (NewMockDispatcher (100 , false )))
236+
237+ assert .Equal (t , 2 , processor .BatchSize )
238+ assert .Equal (t , 10 , processor .MaxQueueSize )
239+
240+ }
241+
242+ func TestDefaultEventProcessor_QSizeExceeded (t * testing.T ) {
243+ exeCtx := utils .NewCancelableExecutionCtx ()
244+ processor := NewBatchEventProcessor (
245+ WithQueueSize (2 ),
246+ WithBatchSize (2 ),
247+ WithFlushInterval (1000 * time .Millisecond ),
248+ WithQueue (NewInMemoryQueue (2 )),
249+ WithEventDispatcher (NewMockDispatcher (100 , true )))
250+ processor .Start (exeCtx )
251+
252+ impression := BuildTestImpressionEvent ()
253+
254+ processor .ProcessEvent (impression )
255+ processor .ProcessEvent (impression )
256+
257+ assert .Equal (t , 2 , processor .EventsCount ())
258+
259+ processor .ProcessEvent (impression )
260+ processor .ProcessEvent (impression )
261+
262+ assert .Equal (t , 2 , processor .EventsCount ())
263+
264+ }
265+
193266func TestDefaultEventProcessor_FailedDispatch (t * testing.T ) {
194267 exeCtx := utils .NewCancelableExecutionCtx ()
195- processor := NewBatchEventProcessor (WithQueueSize (100 ), WithFlushInterval (100 ),
196- WithQueue (NewInMemoryQueue (100 )), WithEventDispatcher (& MockDispatcher {ShouldFail : true , Events : NewInMemoryQueue (100 )}))
268+ processor := NewBatchEventProcessor (
269+ WithQueueSize (100 ),
270+ WithFlushInterval (100 ),
271+ WithQueue (NewInMemoryQueue (100 )),
272+ WithEventDispatcher (& MockDispatcher {ShouldFail : true , Events : NewInMemoryQueue (100 )}))
197273 processor .Start (exeCtx )
198274
199275 impression := BuildTestImpressionEvent ()
@@ -221,7 +297,9 @@ func TestDefaultEventProcessor_FailedDispatch(t *testing.T) {
221297
222298func TestBatchEventProcessor_FlushesOnClose (t * testing.T ) {
223299 exeCtx := utils .NewCancelableExecutionCtx ()
224- processor := NewBatchEventProcessor (WithQueueSize (100 ), WithQueue (NewInMemoryQueue (100 )),
300+ processor := NewBatchEventProcessor (
301+ WithQueueSize (100 ),
302+ WithQueue (NewInMemoryQueue (100 )),
225303 WithEventDispatcher (NewMockDispatcher (100 , false )))
226304 processor .Start (exeCtx )
227305
0 commit comments