Skip to content

Commit 6402460

Browse files
committed
Assertion fixes, clarity
1 parent bb294cc commit 6402460

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

Lines changed: 16 additions & 14 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;
@@ -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;
@@ -348,8 +348,9 @@ public void shouldPassAllOutcomes(
348348
final BsonDocument definition) {
349349
boolean forceFlaky = totalAttempts < 0;
350350
if (!forceFlaky) {
351-
assumeFalse(completed.contains(testName), "Skipping retryable test that succeeded");
352-
completed.add(testName);
351+
boolean ignoreThisTest = ignoreRemaining.contains(testName);
352+
assumeFalse(ignoreThisTest, "Skipping a retryable test that already succeeded");
353+
ignoreRemaining.add(testName);
353354
}
354355
try {
355356
BsonArray operations = definition.getArray("operations");
@@ -376,20 +377,21 @@ public void shouldPassAllOutcomes(
376377
}
377378
compareLogMessages(rootContext, definition, tweaks);
378379
}
379-
} catch (AssertionFailedError e) {
380-
assertTrue(testDef.wasAssignedModifier(Modifier.RETRY));
381-
if (!testDef.matchesError(e)) {
382-
// if the error is not matched, test definitions were not intended to apply; throw it
380+
} catch (Throwable e) {
381+
if (forceFlaky) {
383382
throw e;
384383
}
385-
386-
completed.remove(testName);
387-
boolean lastAttempt = attemptNumber == Math.abs(totalAttempts);
388-
if (forceFlaky || lastAttempt) {
384+
if (!testDef.matchesThrowable(e)) {
385+
// if the throwable is not matched, test definitions were not intended to apply; rethrow it
386+
throw e;
387+
}
388+
boolean isLastAttempt = attemptNumber == Math.abs(totalAttempts);
389+
if (isLastAttempt) {
389390
throw e;
390-
} else {
391-
assumeFalse(completed.contains(testName), "Ignoring failure and retrying attempt " + attemptNumber);
392391
}
392+
393+
ignoreRemaining.remove(testName);
394+
abort("Ignoring failure and retrying attempt " + attemptNumber);
393395
}
394396
}
395397

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")
@@ -275,7 +275,7 @@ public static final class TestDef {
275275
private final boolean reactive;
276276

277277
private final List<Modifier> modifiers = new ArrayList<>();
278-
private Function<AssertionFailedError, Boolean> matchesError;
278+
private Function<Throwable, Boolean> matchesThrowable;
279279

280280
private TestDef(final String dir, final String file, final String test, final boolean reactive) {
281281
this.dir = assertNotNull(dir);
@@ -359,9 +359,9 @@ public boolean wasAssignedModifier(final Modifier modifier) {
359359
return this.modifiers.contains(modifier);
360360
}
361361

362-
public boolean matchesError(final AssertionFailedError e) {
363-
if (matchesError != null) {
364-
return matchesError.apply(e);
362+
public boolean matchesThrowable(final Throwable e) {
363+
if (matchesThrowable != null) {
364+
return matchesThrowable.apply(e);
365365
}
366366
return false;
367367
}
@@ -376,7 +376,7 @@ public static final class TestApplicator {
376376
private boolean matchWasPerformed = false;
377377

378378
private final List<Modifier> modifiersToApply;
379-
private Function<AssertionFailedError, Boolean> matchesError;
379+
private Function<Throwable, Boolean> matchesThrowable;
380380

381381
private TestApplicator(
382382
final TestDef testDef,
@@ -396,7 +396,7 @@ private TestApplicator onMatch(final boolean match) {
396396
}
397397
if (match) {
398398
this.testDef.modifiers.addAll(this.modifiersToApply);
399-
this.testDef.matchesError = this.matchesError;
399+
this.testDef.matchesThrowable = this.matchesThrowable;
400400
}
401401
return this;
402402
}
@@ -489,9 +489,14 @@ public TestApplicator when(final Supplier<Boolean> precondition) {
489489
return this;
490490
}
491491

492-
public TestApplicator whenExceptionContains(final String fragment) {
493-
this.matchesError = (final AssertionFailedError e) -> {
494-
return e.getCause().getMessage().contains(fragment);
492+
public TestApplicator whenFailureContains(final String messageFragment) {
493+
this.matchesThrowable = (final Throwable e) -> {
494+
// inspect the cause for failed assertions with a cause
495+
if (e instanceof AssertionFailedError && e.getCause() != null) {
496+
return e.getCause().getMessage().contains(messageFragment);
497+
} else {
498+
return e.getMessage().contains(messageFragment);
499+
}
495500
};
496501
return this;
497502
}

0 commit comments

Comments
 (0)