Skip to content

Commit f9edd86

Browse files
authored
Test that durations are included on relevant pool events (#1370)
JAVA-5426
1 parent bec3def commit f9edd86

File tree

9 files changed

+92
-16
lines changed

9 files changed

+92
-16
lines changed

driver-core/src/test/resources/connection-monitoring-and-pooling/cmap-format/pool-checkin-make-available.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
{
2323
"type": "ConnectionCheckedOut",
2424
"connectionId": 1,
25-
"address": 42
25+
"address": 42,
26+
"duration": 42
2627
},
2728
{
2829
"type": "ConnectionCheckedIn",
@@ -32,7 +33,8 @@
3233
{
3334
"type": "ConnectionCheckedOut",
3435
"connectionId": 1,
35-
"address": 42
36+
"address": 42,
37+
"duration": 42
3638
}
3739
],
3840
"ignore": [

driver-core/src/test/resources/connection-monitoring-and-pooling/cmap-format/pool-checkout-connection.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
{
2424
"type": "ConnectionReady",
2525
"connectionId": 1,
26-
"address": 42
26+
"address": 42,
27+
"duration": 42
2728
},
2829
{
2930
"type": "ConnectionCheckedOut",
3031
"connectionId": 1,
31-
"address": 42
32+
"address": 42,
33+
"duration": 42
3234
}
3335
],
3436
"ignore": [

driver-core/src/test/resources/connection-monitoring-and-pooling/cmap-format/pool-checkout-error-closed.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
{
3939
"type": "ConnectionCheckedOut",
4040
"address": 42,
41-
"connectionId": 42
41+
"connectionId": 42,
42+
"duration": 42
4243
},
4344
{
4445
"type": "ConnectionCheckedIn",
@@ -56,6 +57,7 @@
5657
{
5758
"type": "ConnectionCheckOutFailed",
5859
"address": 42,
60+
"duration": 42,
5961
"reason": "poolClosed"
6062
}
6163
],

driver-core/src/test/resources/connection-monitoring-and-pooling/cmap-format/pool-checkout-maxConnecting-timeout.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
{
9090
"type": "ConnectionCheckOutFailed",
9191
"reason": "timeout",
92-
"address": 42
92+
"address": 42,
93+
"duration": 42
9394
}
9495
],
9596
"ignore": [

driver-core/src/test/resources/connection-monitoring-and-pooling/cmap-format/pool-clear-clears-waitqueue.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
},
6060
{
6161
"type": "ConnectionCheckedOut",
62-
"address": 42
62+
"address": 42,
63+
"duration": 42
6364
},
6465
{
6566
"type": "ConnectionCheckOutStarted",
@@ -76,17 +77,20 @@
7677
{
7778
"type": "ConnectionCheckOutFailed",
7879
"reason": "connectionError",
79-
"address": 42
80+
"address": 42,
81+
"duration": 42
8082
},
8183
{
8284
"type": "ConnectionCheckOutFailed",
8385
"reason": "connectionError",
84-
"address": 42
86+
"address": 42,
87+
"duration": 42
8588
},
8689
{
8790
"type": "ConnectionCheckOutFailed",
8891
"reason": "connectionError",
89-
"address": 42
92+
"address": 42,
93+
"duration": 42
9094
}
9195
],
9296
"ignore": [

driver-core/src/test/resources/connection-monitoring-and-pooling/cmap-format/pool-clear-ready.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
{
4141
"type": "ConnectionCheckedOut",
4242
"address": 42,
43-
"connectionId": 42
43+
"connectionId": 42,
44+
"duration": 42
4445
},
4546
{
4647
"type": "ConnectionPoolCleared",
@@ -49,6 +50,7 @@
4950
{
5051
"type": "ConnectionCheckOutFailed",
5152
"address": 42,
53+
"duration": 42,
5254
"reason": "connectionError"
5355
},
5456
{
@@ -57,7 +59,8 @@
5759
},
5860
{
5961
"type": "ConnectionCheckedOut",
60-
"address": 42
62+
"address": 42,
63+
"duration": 42
6164
}
6265
],
6366
"ignore": [

driver-core/src/test/resources/connection-monitoring-and-pooling/cmap-format/pool-ready.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
{
3232
"type": "ConnectionCheckOutFailed",
3333
"reason": "connectionError",
34-
"address": 42
34+
"address": 42,
35+
"duration": 42
3536
},
3637
{
3738
"type": "ConnectionPoolReady",
@@ -47,7 +48,8 @@
4748
},
4849
{
4950
"type": "ConnectionCheckedOut",
50-
"address": 42
51+
"address": 42,
52+
"duration": 42
5153
}
5254
],
5355
"ignore": [

driver-core/src/test/resources/connection-monitoring-and-pooling/cmap-format/wait-queue-timeout.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
{
4949
"type": "ConnectionCheckedOut",
5050
"connectionId": 42,
51-
"address": 42
51+
"address": 42,
52+
"duration": 42
5253
},
5354
{
5455
"type": "ConnectionCheckOutStarted",
@@ -57,7 +58,8 @@
5758
{
5859
"type": "ConnectionCheckOutFailed",
5960
"reason": "timeout",
60-
"address": 42
61+
"address": 42,
62+
"duration": 42
6163
},
6264
{
6365
"type": "ConnectionCheckedIn",

driver-core/src/test/unit/com/mongodb/internal/connection/AbstractConnectionPoolTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@
7878
import java.util.concurrent.Future;
7979
import java.util.concurrent.TimeUnit;
8080
import java.util.concurrent.atomic.AtomicLong;
81+
import java.util.stream.Collectors;
8182

8283
import static com.mongodb.assertions.Assertions.assertFalse;
8384
import static com.mongodb.internal.thread.InterruptionUtil.interruptAndCreateMongoInterruptedException;
8485
import static java.lang.String.format;
86+
import static java.util.Arrays.asList;
8587
import static org.junit.Assert.assertEquals;
88+
import static org.junit.Assert.assertTrue;
8689
import static org.junit.Assert.fail;
8790
import static org.junit.Assume.assumeFalse;
8891
import static org.junit.Assume.assumeNotNull;
@@ -285,40 +288,59 @@ public void shouldPassAllOutcomes() throws Exception {
285288
BsonDocument expectedEvent = cur.asDocument();
286289
String type = expectedEvent.getString("type").getValue();
287290
if (type.equals("ConnectionPoolCreated")) {
291+
assertHasOnlySupportedKeys(expectedEvent, "type", "address", "options");
288292
ConnectionPoolCreatedEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionPoolCreatedEvent.class);
289293
assertAddressMatch(expectedEvent, actualEvent.getServerId().getAddress());
290294
assertEquals(settings, actualEvent.getSettings());
291295
} else if (type.equals("ConnectionPoolCleared")) {
296+
assertHasOnlySupportedKeys(expectedEvent, "type", "address");
292297
ConnectionPoolClearedEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionPoolClearedEvent.class);
293298
assertAddressMatch(expectedEvent, actualEvent.getServerId().getAddress());
294299
} else if (type.equals("ConnectionPoolReady")) {
300+
assertHasOnlySupportedKeys(expectedEvent, "type", "address");
295301
ConnectionPoolReadyEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionPoolReadyEvent.class);
296302
assertAddressMatch(expectedEvent, actualEvent.getServerId().getAddress());
297303
} else if (type.equals("ConnectionPoolClosed")) {
304+
assertHasOnlySupportedKeys(expectedEvent, "type", "address");
298305
ConnectionPoolClosedEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionPoolClosedEvent.class);
299306
assertAddressMatch(expectedEvent, actualEvent.getServerId().getAddress());
300307
} else if (type.equals("ConnectionCreated")) {
308+
assertHasOnlySupportedKeys(expectedEvent, "type", "address", "connectionId");
301309
ConnectionCreatedEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionCreatedEvent.class);
310+
assertAddressMatch(expectedEvent, actualEvent.getConnectionId().getServerId().getAddress());
302311
assertConnectionIdMatch(expectedEvent, actualEvent.getConnectionId());
303312
} else if (type.equals("ConnectionReady")) {
313+
assertHasOnlySupportedKeys(expectedEvent, "type", "address", "connectionId", "duration");
304314
ConnectionReadyEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionReadyEvent.class);
305315
assertAddressMatch(expectedEvent, actualEvent.getConnectionId().getServerId().getAddress());
316+
assertConnectionIdMatch(expectedEvent, actualEvent.getConnectionId());
317+
assertDurationMatch(expectedEvent, actualEvent);
306318
} else if (type.equals("ConnectionClosed")) {
319+
assertHasOnlySupportedKeys(expectedEvent, "type", "address", "connectionId", "reason");
307320
ConnectionClosedEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionClosedEvent.class);
321+
assertAddressMatch(expectedEvent, actualEvent.getConnectionId().getServerId().getAddress());
308322
assertConnectionIdMatch(expectedEvent, actualEvent.getConnectionId());
309323
assertReasonMatch(expectedEvent, actualEvent);
310324
} else if (type.equals("ConnectionCheckOutStarted")) {
325+
assertHasOnlySupportedKeys(expectedEvent, "type", "address");
311326
ConnectionCheckOutStartedEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionCheckOutStartedEvent.class);
312327
assertAddressMatch(expectedEvent, actualEvent.getServerId().getAddress());
313328
} else if (type.equals("ConnectionCheckOutFailed")) {
329+
assertHasOnlySupportedKeys(expectedEvent, "type", "address", "reason", "duration");
314330
ConnectionCheckOutFailedEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionCheckOutFailedEvent.class);
315331
assertAddressMatch(expectedEvent, actualEvent.getServerId().getAddress());
316332
assertReasonMatch(expectedEvent, actualEvent);
333+
assertDurationMatch(expectedEvent, actualEvent);
317334
} else if (type.equals("ConnectionCheckedOut")) {
335+
assertHasOnlySupportedKeys(expectedEvent, "type", "address", "connectionId", "duration");
318336
ConnectionCheckedOutEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionCheckedOutEvent.class);
337+
assertAddressMatch(expectedEvent, actualEvent.getConnectionId().getServerId().getAddress());
319338
assertConnectionIdMatch(expectedEvent, actualEvent.getConnectionId());
339+
assertDurationMatch(expectedEvent, actualEvent);
320340
} else if (type.equals("ConnectionCheckedIn")) {
341+
assertHasOnlySupportedKeys(expectedEvent, "type", "address", "connectionId");
321342
ConnectionCheckedInEvent actualEvent = getNextEvent(actualEventsIterator, ConnectionCheckedInEvent.class);
343+
assertAddressMatch(expectedEvent, actualEvent.getConnectionId().getServerId().getAddress());
322344
assertConnectionIdMatch(expectedEvent, actualEvent.getConnectionId());
323345
} else {
324346
throw new UnsupportedOperationException("Unsupported event type " + type);
@@ -327,6 +349,16 @@ public void shouldPassAllOutcomes() throws Exception {
327349
}
328350
}
329351

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+
330362
private void assertReasonMatch(final BsonDocument expectedEvent, final ConnectionClosedEvent connectionClosedEvent) {
331363
if (!expectedEvent.containsKey("reason")) {
332364
return;
@@ -400,6 +432,32 @@ private void assertConnectionIdMatch(final BsonDocument expectedEvent, final Con
400432
}
401433
}
402434

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+
403461
private long adjustedConnectionIdLocalValue(final long connectionIdLocalValue) {
404462
if (pool instanceof ConnectionIdAdjustingConnectionPool) {
405463
return ((ConnectionIdAdjustingConnectionPool) pool).adjustedConnectionIdLocalValue(connectionIdLocalValue);

0 commit comments

Comments
 (0)