Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 86681ad

Browse files
committedDec 5, 2024·
Assertion fixes
1 parent 52d5a17 commit 86681ad

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed
 

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

Lines changed: 6 additions & 6 deletions
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;
@@ -369,10 +369,9 @@ public void shouldPassAllOutcomes(
369369
}
370370
compareLogMessages(rootContext, definition, tweaks);
371371
}
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
372+
} catch (Throwable e) {
373+
if (!testDef.matchesThrowable(e)) {
374+
// if the throwable is not matched, test definitions were not intended to apply; rethrow it
376375
throw e;
377376
}
378377

@@ -381,7 +380,8 @@ public void shouldPassAllOutcomes(
381380
if (forceFlaky || lastAttempt) {
382381
throw e;
383382
} else {
384-
assumeFalse(completed.contains(testName), "Ignoring failure and retrying attempt " + attemptNumber);
383+
// not the last attempt
384+
abort("Ignoring failure and retrying attempt " + attemptNumber);
385385
}
386386
}
387387
}

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

Lines changed: 16 additions & 11 deletions
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)
Please sign in to comment.