30
30
31
31
import java .util .Collections ;
32
32
import java .util .concurrent .*;
33
+ import java .util .concurrent .atomic .AtomicInteger ;
33
34
34
35
import static org .junit .Assert .*;
35
36
import static org .mockito .Mockito .*;
@@ -93,11 +94,12 @@ public void testFlushOnMaxTimeout() throws Exception {
93
94
eventHandlerRule .expectConversion (EVENT_NAME , USER_ID );
94
95
95
96
if (!countDownLatch .await (MAX_DURATION_MS * 3 , TimeUnit .MILLISECONDS )) {
96
- fail ("Exceeded timeout waiting for notification ." );
97
+ fail ("Exceeded timeout waiting for events to flush ." );
97
98
}
98
99
99
100
eventProcessor .close ();
100
101
assertEquals (0 , eventQueue .size ());
102
+ eventHandlerRule .expectCalls (1 );
101
103
}
102
104
103
105
@ Test
@@ -116,17 +118,17 @@ public void testFlushMaxBatchSize() throws Exception {
116
118
eventHandlerRule .expectConversion (eventName , USER_ID );
117
119
}
118
120
119
- countDownLatch .await ();
121
+ if (!countDownLatch .await (MAX_DURATION_MS * 3 , TimeUnit .MILLISECONDS )) {
122
+ fail ("Exceeded timeout waiting for events to flush." );
123
+ }
124
+
120
125
assertEquals (0 , eventQueue .size ());
126
+ eventHandlerRule .expectCalls (1 );
121
127
}
122
128
123
129
@ Test
124
130
public void testFlush () throws Exception {
125
- CountDownLatch countDownLatch = new CountDownLatch (2 );
126
- setEventProcessor (logEvent -> {
127
- eventHandlerRule .dispatchEvent (logEvent );
128
- countDownLatch .countDown ();
129
- });
131
+ setEventProcessor (logEvent -> eventHandlerRule .dispatchEvent (logEvent ));
130
132
131
133
UserEvent userEvent = buildConversionEvent (EVENT_NAME );
132
134
eventProcessor .process (userEvent );
@@ -137,18 +139,12 @@ public void testFlush() throws Exception {
137
139
eventProcessor .flush ();
138
140
eventHandlerRule .expectConversion (EVENT_NAME , USER_ID );
139
141
140
- if (!countDownLatch .await (MAX_DURATION_MS / 2 , TimeUnit .MILLISECONDS )) {
141
- fail ("Exceeded timeout waiting for notification." );
142
- }
142
+ eventHandlerRule .expectCalls (2 );
143
143
}
144
144
145
145
@ Test
146
146
public void testFlushOnMismatchRevision () throws Exception {
147
- CountDownLatch countDownLatch = new CountDownLatch (2 );
148
- setEventProcessor (logEvent -> {
149
- eventHandlerRule .dispatchEvent (logEvent );
150
- countDownLatch .countDown ();
151
- });
147
+ setEventProcessor (logEvent -> eventHandlerRule .dispatchEvent (logEvent ));
152
148
153
149
ProjectConfig projectConfig1 = mock (ProjectConfig .class );
154
150
when (projectConfig1 .getRevision ()).thenReturn ("1" );
@@ -165,18 +161,12 @@ public void testFlushOnMismatchRevision() throws Exception {
165
161
eventHandlerRule .expectConversion (EVENT_NAME , USER_ID );
166
162
167
163
eventProcessor .close ();
168
- if (!countDownLatch .await (MAX_DURATION_MS * 3 , TimeUnit .MILLISECONDS )) {
169
- fail ("Exceeded timeout waiting for notification." );
170
- }
164
+ eventHandlerRule .expectCalls (2 );
171
165
}
172
166
173
167
@ Test
174
168
public void testFlushOnMismatchProjectId () throws Exception {
175
- CountDownLatch countDownLatch = new CountDownLatch (2 );
176
- setEventProcessor (logEvent -> {
177
- eventHandlerRule .dispatchEvent (logEvent );
178
- countDownLatch .countDown ();
179
- });
169
+ setEventProcessor (logEvent -> eventHandlerRule .dispatchEvent (logEvent ));
180
170
181
171
ProjectConfig projectConfig1 = mock (ProjectConfig .class );
182
172
when (projectConfig1 .getRevision ()).thenReturn ("1" );
@@ -193,18 +183,12 @@ public void testFlushOnMismatchProjectId() throws Exception {
193
183
eventHandlerRule .expectConversion (EVENT_NAME , USER_ID );
194
184
195
185
eventProcessor .close ();
196
- if (!countDownLatch .await (MAX_DURATION_MS * 3 , TimeUnit .MILLISECONDS )) {
197
- fail ("Exceeded timeout waiting for notification." );
198
- }
186
+ eventHandlerRule .expectCalls (2 );
199
187
}
200
188
201
189
@ Test
202
190
public void testStopAndStart () throws Exception {
203
- CountDownLatch countDownLatch = new CountDownLatch (2 );
204
- setEventProcessor (logEvent -> {
205
- eventHandlerRule .dispatchEvent (logEvent );
206
- countDownLatch .countDown ();
207
- });
191
+ setEventProcessor (logEvent -> eventHandlerRule .dispatchEvent (logEvent ));
208
192
209
193
UserEvent userEvent = buildConversionEvent (EVENT_NAME );
210
194
eventProcessor .process (userEvent );
@@ -218,31 +202,27 @@ public void testStopAndStart() throws Exception {
218
202
eventProcessor .start ();
219
203
220
204
eventProcessor .close ();
221
- if (!countDownLatch .await (MAX_DURATION_MS * 3 , TimeUnit .MILLISECONDS )) {
222
- fail ("Exceeded timeout waiting for notification." );
223
- }
205
+ eventHandlerRule .expectCalls (2 );
224
206
}
225
207
226
208
@ Test
227
209
public void testNotificationCenter () throws Exception {
228
- CountDownLatch countDownLatch = new CountDownLatch ( 1 );
229
- notificationCenter .addNotificationHandler (LogEvent .class , x -> countDownLatch . countDown ());
210
+ AtomicInteger counter = new AtomicInteger ( );
211
+ notificationCenter .addNotificationHandler (LogEvent .class , x -> counter . incrementAndGet ());
230
212
setEventProcessor (logEvent -> {});
231
213
232
214
UserEvent userEvent = buildConversionEvent (EVENT_NAME );
233
215
eventProcessor .process (userEvent );
234
216
eventProcessor .close ();
235
217
236
- if (!countDownLatch .await (MAX_DURATION_MS * 3 , TimeUnit .MILLISECONDS )) {
237
- fail ("Exceeded timeout waiting for notification." );
238
- }
218
+ assertEquals (1 , counter .intValue ());
239
219
}
240
220
241
221
@ Test
242
222
public void testCloseTimeout () throws Exception {
243
223
CountDownLatch countDownLatch = new CountDownLatch (1 );
244
224
setEventProcessor (logEvent -> {
245
- if (!countDownLatch .await (TIMEOUT_MS * 2 , TimeUnit .SECONDS )) {
225
+ if (!countDownLatch .await (TIMEOUT_MS * 2 , TimeUnit .MILLISECONDS )) {
246
226
fail ("Exceeded timeout waiting for close." );
247
227
}
248
228
});
@@ -266,6 +246,48 @@ public void testCloseEventHandler() throws Exception {
266
246
verify ((AutoCloseable ) mockEventHandler ).close ();
267
247
}
268
248
249
+ @ Test
250
+ public void testInvalidBatchSizeUsesDefault () {
251
+ eventProcessor = BatchEventProcessor .builder ()
252
+ .withEventQueue (eventQueue )
253
+ .withBatchSize (-1 )
254
+ .withFlushInterval (MAX_DURATION_MS )
255
+ .withEventHandler (new NoopEventHandler ())
256
+ .withNotificationCenter (notificationCenter )
257
+ .withTimeout (TIMEOUT_MS , TimeUnit .MILLISECONDS )
258
+ .build ();
259
+
260
+ assertEquals (eventProcessor .batchSize , BatchEventProcessor .DEFAULT_BATCH_SIZE );
261
+ }
262
+
263
+ @ Test
264
+ public void testInvalidFlushIntervalUsesDefault () {
265
+ eventProcessor = BatchEventProcessor .builder ()
266
+ .withEventQueue (eventQueue )
267
+ .withBatchSize (MAX_BATCH_SIZE )
268
+ .withFlushInterval (-1L )
269
+ .withEventHandler (new NoopEventHandler ())
270
+ .withNotificationCenter (notificationCenter )
271
+ .withTimeout (TIMEOUT_MS , TimeUnit .MILLISECONDS )
272
+ .build ();
273
+
274
+ assertEquals (eventProcessor .flushInterval , BatchEventProcessor .DEFAULT_BATCH_INTERVAL );
275
+ }
276
+
277
+ @ Test
278
+ public void testInvalidTimeoutUsesDefault () {
279
+ eventProcessor = BatchEventProcessor .builder ()
280
+ .withEventQueue (eventQueue )
281
+ .withBatchSize (MAX_BATCH_SIZE )
282
+ .withFlushInterval (MAX_DURATION_MS )
283
+ .withEventHandler (new NoopEventHandler ())
284
+ .withNotificationCenter (notificationCenter )
285
+ .withTimeout (-1L , TimeUnit .MILLISECONDS )
286
+ .build ();
287
+
288
+ assertEquals (eventProcessor .timeoutMillis , BatchEventProcessor .DEFAULT_TIMEOUT_INTERVAL );
289
+ }
290
+
269
291
private void setEventProcessor (EventHandler eventHandler ) {
270
292
eventProcessor = BatchEventProcessor .builder ()
271
293
.withEventQueue (eventQueue )
0 commit comments