78
78
import java .util .concurrent .Future ;
79
79
import java .util .concurrent .TimeUnit ;
80
80
import java .util .concurrent .atomic .AtomicLong ;
81
+ import java .util .stream .Collectors ;
81
82
82
83
import static com .mongodb .assertions .Assertions .assertFalse ;
83
84
import static com .mongodb .internal .thread .InterruptionUtil .interruptAndCreateMongoInterruptedException ;
84
85
import static java .lang .String .format ;
86
+ import static java .util .Arrays .asList ;
85
87
import static org .junit .Assert .assertEquals ;
88
+ import static org .junit .Assert .assertTrue ;
86
89
import static org .junit .Assert .fail ;
87
90
import static org .junit .Assume .assumeFalse ;
88
91
import static org .junit .Assume .assumeNotNull ;
@@ -285,40 +288,59 @@ public void shouldPassAllOutcomes() throws Exception {
285
288
BsonDocument expectedEvent = cur .asDocument ();
286
289
String type = expectedEvent .getString ("type" ).getValue ();
287
290
if (type .equals ("ConnectionPoolCreated" )) {
291
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" , "options" );
288
292
ConnectionPoolCreatedEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionPoolCreatedEvent .class );
289
293
assertAddressMatch (expectedEvent , actualEvent .getServerId ().getAddress ());
290
294
assertEquals (settings , actualEvent .getSettings ());
291
295
} else if (type .equals ("ConnectionPoolCleared" )) {
296
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" );
292
297
ConnectionPoolClearedEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionPoolClearedEvent .class );
293
298
assertAddressMatch (expectedEvent , actualEvent .getServerId ().getAddress ());
294
299
} else if (type .equals ("ConnectionPoolReady" )) {
300
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" );
295
301
ConnectionPoolReadyEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionPoolReadyEvent .class );
296
302
assertAddressMatch (expectedEvent , actualEvent .getServerId ().getAddress ());
297
303
} else if (type .equals ("ConnectionPoolClosed" )) {
304
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" );
298
305
ConnectionPoolClosedEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionPoolClosedEvent .class );
299
306
assertAddressMatch (expectedEvent , actualEvent .getServerId ().getAddress ());
300
307
} else if (type .equals ("ConnectionCreated" )) {
308
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" , "connectionId" );
301
309
ConnectionCreatedEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionCreatedEvent .class );
310
+ assertAddressMatch (expectedEvent , actualEvent .getConnectionId ().getServerId ().getAddress ());
302
311
assertConnectionIdMatch (expectedEvent , actualEvent .getConnectionId ());
303
312
} else if (type .equals ("ConnectionReady" )) {
313
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" , "connectionId" , "duration" );
304
314
ConnectionReadyEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionReadyEvent .class );
305
315
assertAddressMatch (expectedEvent , actualEvent .getConnectionId ().getServerId ().getAddress ());
316
+ assertConnectionIdMatch (expectedEvent , actualEvent .getConnectionId ());
317
+ assertDurationMatch (expectedEvent , actualEvent );
306
318
} else if (type .equals ("ConnectionClosed" )) {
319
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" , "connectionId" , "reason" );
307
320
ConnectionClosedEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionClosedEvent .class );
321
+ assertAddressMatch (expectedEvent , actualEvent .getConnectionId ().getServerId ().getAddress ());
308
322
assertConnectionIdMatch (expectedEvent , actualEvent .getConnectionId ());
309
323
assertReasonMatch (expectedEvent , actualEvent );
310
324
} else if (type .equals ("ConnectionCheckOutStarted" )) {
325
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" );
311
326
ConnectionCheckOutStartedEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionCheckOutStartedEvent .class );
312
327
assertAddressMatch (expectedEvent , actualEvent .getServerId ().getAddress ());
313
328
} else if (type .equals ("ConnectionCheckOutFailed" )) {
329
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" , "reason" , "duration" );
314
330
ConnectionCheckOutFailedEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionCheckOutFailedEvent .class );
315
331
assertAddressMatch (expectedEvent , actualEvent .getServerId ().getAddress ());
316
332
assertReasonMatch (expectedEvent , actualEvent );
333
+ assertDurationMatch (expectedEvent , actualEvent );
317
334
} else if (type .equals ("ConnectionCheckedOut" )) {
335
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" , "connectionId" , "duration" );
318
336
ConnectionCheckedOutEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionCheckedOutEvent .class );
337
+ assertAddressMatch (expectedEvent , actualEvent .getConnectionId ().getServerId ().getAddress ());
319
338
assertConnectionIdMatch (expectedEvent , actualEvent .getConnectionId ());
339
+ assertDurationMatch (expectedEvent , actualEvent );
320
340
} else if (type .equals ("ConnectionCheckedIn" )) {
341
+ assertHasOnlySupportedKeys (expectedEvent , "type" , "address" , "connectionId" );
321
342
ConnectionCheckedInEvent actualEvent = getNextEvent (actualEventsIterator , ConnectionCheckedInEvent .class );
343
+ assertAddressMatch (expectedEvent , actualEvent .getConnectionId ().getServerId ().getAddress ());
322
344
assertConnectionIdMatch (expectedEvent , actualEvent .getConnectionId ());
323
345
} else {
324
346
throw new UnsupportedOperationException ("Unsupported event type " + type );
@@ -327,6 +349,16 @@ public void shouldPassAllOutcomes() throws Exception {
327
349
}
328
350
}
329
351
352
+ private static void assertHasOnlySupportedKeys (final BsonDocument document , final String ... supportedKeys ) {
353
+ List <String > supportedKeysList = asList (supportedKeys );
354
+ List <String > unsupportedKeys = document .keySet ().stream ()
355
+ .filter (key -> !supportedKeysList .contains (key ))
356
+ .collect (Collectors .toList ());
357
+ if (!unsupportedKeys .isEmpty ()) {
358
+ fail (format ("The runner encountered not yet supported keys %s in %s" , unsupportedKeys , document ));
359
+ }
360
+ }
361
+
330
362
private void assertReasonMatch (final BsonDocument expectedEvent , final ConnectionClosedEvent connectionClosedEvent ) {
331
363
if (!expectedEvent .containsKey ("reason" )) {
332
364
return ;
@@ -400,6 +432,32 @@ private void assertConnectionIdMatch(final BsonDocument expectedEvent, final Con
400
432
}
401
433
}
402
434
435
+ private static void assertDurationMatch (final BsonDocument expectedEvent , final ConnectionReadyEvent actualEvent ) {
436
+ assertDurationMatch (expectedEvent , actualEvent .getElapsedTime (TimeUnit .MILLISECONDS ));
437
+ }
438
+
439
+ private static void assertDurationMatch (final BsonDocument expectedEvent , final ConnectionCheckOutFailedEvent actualEvent ) {
440
+ assertDurationMatch (expectedEvent , actualEvent .getElapsedTime (TimeUnit .MILLISECONDS ));
441
+ }
442
+
443
+ private static void assertDurationMatch (final BsonDocument expectedEvent , final ConnectionCheckedOutEvent actualEvent ) {
444
+ assertDurationMatch (expectedEvent , actualEvent .getElapsedTime (TimeUnit .MILLISECONDS ));
445
+ }
446
+
447
+ private static void assertDurationMatch (final BsonDocument expectedEvent , final long actualDurationMillis ) {
448
+ String durationKey = "duration" ;
449
+ if (expectedEvent .isNumber (durationKey )) {
450
+ assertTrue ("actualDurationMillis must not be negative" , actualDurationMillis >= 0 );
451
+ long expectedDurationMillis = expectedEvent .getNumber (durationKey ).longValue ();
452
+ if (expectedDurationMillis != ANY_INT ) {
453
+ fail (format ("Unsupported duration value %d. Pay attention to the expected value unit when supporting the value" ,
454
+ expectedDurationMillis ));
455
+ }
456
+ } else if (expectedEvent .containsKey (durationKey )) {
457
+ fail (format ("Unsupported value %s" , expectedEvent .get (durationKey )));
458
+ }
459
+ }
460
+
403
461
private long adjustedConnectionIdLocalValue (final long connectionIdLocalValue ) {
404
462
if (pool instanceof ConnectionIdAdjustingConnectionPool ) {
405
463
return ((ConnectionIdAdjustingConnectionPool ) pool ).adjustedConnectionIdLocalValue (connectionIdLocalValue );
0 commit comments