Skip to content

Commit 3b2c915

Browse files
committed
Assertion fixes, clarity
1 parent 52d5a17 commit 3b2c915

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

driver-sync/src/test/functional/com/mongodb/client/unified/UnifiedTest.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import org.junit.jupiter.params.ParameterizedTest;
5858
import org.junit.jupiter.params.provider.Arguments;
5959
import org.junit.jupiter.params.provider.MethodSource;
60-
import org.opentest4j.AssertionFailedError;
6160
import org.opentest4j.TestAbortedException;
6261

6362
import java.io.File;
@@ -96,6 +95,7 @@
9695
import static org.junit.jupiter.api.Assertions.assertNull;
9796
import static org.junit.jupiter.api.Assertions.assertTrue;
9897
import static org.junit.jupiter.api.Assertions.fail;
98+
import static org.junit.jupiter.api.Assumptions.abort;
9999
import static org.junit.jupiter.api.Assumptions.assumeFalse;
100100
import static org.junit.jupiter.api.Assumptions.assumeTrue;
101101
import static util.JsonPoweredTestHelper.getTestDocument;
@@ -107,7 +107,7 @@ public abstract class UnifiedTest {
107107
"wait queue timeout errors include details about checked out connections");
108108

109109
public static final int ATTEMPTS = 3;
110-
private static Set<String> completed = new HashSet<>();
110+
private static Set<String> ignoreRemaining = new HashSet<>();
111111

112112
@Nullable
113113
private String fileDescription;
@@ -341,8 +341,9 @@ public void shouldPassAllOutcomes(
341341
final BsonDocument definition) {
342342
boolean forceFlaky = totalAttempts < 0;
343343
if (!forceFlaky) {
344-
assumeFalse(completed.contains(testName), "Skipping retryable test that succeeded");
345-
completed.add(testName);
344+
boolean ignoreThisTest = ignoreRemaining.contains(testName);
345+
assumeFalse(ignoreThisTest, "Skipping a retryable test that already succeeded");
346+
ignoreRemaining.add(testName);
346347
}
347348
try {
348349
BsonArray operations = definition.getArray("operations");
@@ -369,20 +370,21 @@ public void shouldPassAllOutcomes(
369370
}
370371
compareLogMessages(rootContext, definition, tweaks);
371372
}
372-
} catch (AssertionFailedError e) {
373-
assertTrue(testDef.wasAssignedModifier(Modifier.RETRY));
374-
if (!testDef.matchesError(e)) {
375-
// if the error is not matched, test definitions were not intended to apply; throw it
373+
} catch (Throwable e) {
374+
if (forceFlaky) {
376375
throw e;
377376
}
378-
379-
completed.remove(testName);
380-
boolean lastAttempt = attemptNumber == Math.abs(totalAttempts);
381-
if (forceFlaky || lastAttempt) {
377+
if (!testDef.matchesThrowable(e)) {
378+
// if the throwable is not matched, test definitions were not intended to apply; rethrow it
379+
throw e;
380+
}
381+
boolean isLastAttempt = attemptNumber == Math.abs(totalAttempts);
382+
if (isLastAttempt) {
382383
throw e;
383-
} else {
384-
assumeFalse(completed.contains(testName), "Ignoring failure and retrying attempt " + attemptNumber);
385384
}
385+
386+
ignoreRemaining.remove(testName);
387+
abort("Ignoring failure and retrying attempt " + attemptNumber);
386388
}
387389
}
388390

driver-sync/src/test/functional/com/mongodb/client/unified/UnifiedTestModifications.java

+16-11
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public static void applyCustomizations(final TestDef def) {
4444
// TODO reasons for retry
4545
// Exception in encryption library: ChangeCipherSpec message sequence violation
4646
def.retry("TODO reason")
47-
.whenExceptionContains("ChangeCipherSpec message sequence violation")
47+
.whenFailureContains("ChangeCipherSpec message sequence violation")
4848
.test("client-side-encryption", "namedKMS-createDataKey", "create datakey with named KMIP KMS provider");
4949

5050
def.retry("TODO reason")
51-
.whenExceptionContains("Number of checked out connections must match expected")
51+
.whenFailureContains("Number of checked out connections must match expected")
5252
.test("load-balancers", "cursors are correctly pinned to connections for load-balanced clusters", "pinned connections are returned after a network error during a killCursors request");
5353

5454
def.retry("TODO reason")
@@ -295,7 +295,7 @@ public static final class TestDef {
295295
private final boolean reactive;
296296

297297
private final List<Modifier> modifiers = new ArrayList<>();
298-
private Function<AssertionFailedError, Boolean> matchesError;
298+
private Function<Throwable, Boolean> matchesThrowable;
299299

300300
private TestDef(final String dir, final String file, final String test, final boolean reactive) {
301301
this.dir = dir;
@@ -379,9 +379,9 @@ public boolean wasAssignedModifier(final Modifier modifier) {
379379
return this.modifiers.contains(modifier);
380380
}
381381

382-
public boolean matchesError(final AssertionFailedError e) {
383-
if (matchesError != null) {
384-
return matchesError.apply(e);
382+
public boolean matchesThrowable(final Throwable e) {
383+
if (matchesThrowable != null) {
384+
return matchesThrowable.apply(e);
385385
}
386386
return false;
387387
}
@@ -396,7 +396,7 @@ public static final class TestApplicator {
396396
private boolean matchWasPerformed = false;
397397

398398
private final List<Modifier> modifiersToApply;
399-
private Function<AssertionFailedError, Boolean> matchesError;
399+
private Function<Throwable, Boolean> matchesThrowable;
400400

401401
private TestApplicator(
402402
final TestDef testDef,
@@ -416,7 +416,7 @@ private TestApplicator onMatch(final boolean match) {
416416
}
417417
if (match) {
418418
this.testDef.modifiers.addAll(this.modifiersToApply);
419-
this.testDef.matchesError = this.matchesError;
419+
this.testDef.matchesThrowable = this.matchesThrowable;
420420
}
421421
return this;
422422
}
@@ -509,9 +509,14 @@ public TestApplicator when(final Supplier<Boolean> precondition) {
509509
return this;
510510
}
511511

512-
public TestApplicator whenExceptionContains(final String fragment) {
513-
this.matchesError = (final AssertionFailedError e) -> {
514-
return e.getCause().getMessage().contains(fragment);
512+
public TestApplicator whenFailureContains(final String messageFragment) {
513+
this.matchesThrowable = (final Throwable e) -> {
514+
// inspect the cause for failed assertions with a cause
515+
if (e instanceof AssertionFailedError && e.getCause() != null) {
516+
return e.getCause().getMessage().contains(messageFragment);
517+
} else {
518+
return e.getMessage().contains(messageFragment);
519+
}
515520
};
516521
return this;
517522
}

0 commit comments

Comments
 (0)