Skip to content

Commit a7d9dfc

Browse files
committed
Rename assertion methods in RetryStateTest
1 parent 6f85ee4 commit a7d9dfc

1 file changed

Lines changed: 46 additions & 40 deletions

File tree

driver-core/src/test/unit/com/mongodb/internal/async/function/RetryStateTest.java

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.mongodb.internal.TimeoutSettings;
2222
import com.mongodb.internal.async.function.LoopState.AttachmentKey;
2323
import com.mongodb.internal.operation.retry.AttachmentKeys;
24-
import com.mongodb.lang.Nullable;
2524
import org.junit.jupiter.api.Assertions;
2625
import org.junit.jupiter.api.DisplayName;
2726
import org.junit.jupiter.api.Test;
@@ -93,7 +92,7 @@ void unlimitedAttemptsAndAdvance(final TimeoutContext timeoutContext) {
9392
assertAll(
9493
() -> assertFalse(retryState.isFirstAttempt()),
9594
() -> assertEquals(1, retryState.attempt()),
96-
() -> assertAdvanceOrThrow(attemptException, retryState, attemptException)
95+
() -> assertAdvanceOrThrowThrows(attemptException, retryState, attemptException)
9796
);
9897
}
9998

@@ -104,7 +103,7 @@ void limitedAttemptsAndAdvance() {
104103
assertAll(
105104
() -> assertTrue(retryState.isFirstAttempt()),
106105
() -> assertEquals(0, retryState.attempt()),
107-
() -> assertAdvanceOrThrow(attemptException, retryState, attemptException),
106+
() -> assertAdvanceOrThrowThrows(attemptException, retryState, attemptException),
108107
// when there is only one attempt, it is both the first and the last one
109108
() -> assertTrue(retryState.isFirstAttempt()),
110109
() -> assertEquals(0, retryState.attempt())
@@ -117,7 +116,7 @@ void markAsLastAttemptAdvanceWithRuntimeException(final TimeoutContext timeoutCo
117116
RetryState retryState = new RetryState(timeoutContext);
118117
retryState.markAsLastAttempt();
119118
RuntimeException attemptException = new RuntimeException();
120-
assertAdvanceOrThrow(attemptException, retryState, attemptException, (rs, e) -> fail());
119+
assertAdvanceOrThrowThrows(attemptException, retryState, attemptException, (rs, e) -> fail());
121120
}
122121

123122
@ParameterizedTest(name = "should advance with non-retryable error when marked as last attempt and : ''{0}''")
@@ -126,15 +125,15 @@ void markAsLastAttemptAdvanceWithError(final TimeoutContext timeoutContext) {
126125
RetryState retryState = new RetryState(timeoutContext);
127126
retryState.markAsLastAttempt();
128127
Error attemptException = new Error();
129-
assertAdvanceOrThrow(attemptException, retryState, attemptException, (rs, e) -> fail());
128+
assertAdvanceOrThrowThrows(attemptException, retryState, attemptException, (rs, e) -> fail());
130129
}
131130

132131
@ParameterizedTest
133132
@MethodSource({"infiniteTimeout", "noTimeout"})
134133
void breakAndThrowIfRetryAndFirstAttempt(final TimeoutContext timeoutContext) {
135134
RetryState retryState = new RetryState(timeoutContext);
136135
retryState.breakAndThrowIfRetryAnd(Assertions::fail);
137-
assertAdvanceOrThrow(null, retryState, new RuntimeException());
136+
assertAdvanceOrThrowDoesNotThrow(retryState, new RuntimeException());
138137
}
139138

140139
@ParameterizedTest
@@ -143,7 +142,7 @@ void breakAndThrowIfRetryAndFalse(final TimeoutContext timeoutContext) {
143142
RetryState retryState = new RetryState(timeoutContext);
144143
advance(retryState);
145144
retryState.breakAndThrowIfRetryAnd(() -> false);
146-
assertAdvanceOrThrow(null, retryState, new RuntimeException());
145+
assertAdvanceOrThrowDoesNotThrow(retryState, new RuntimeException());
147146
}
148147

149148
@ParameterizedTest
@@ -153,7 +152,7 @@ void breakAndThrowIfRetryAndTrue(final TimeoutContext timeoutContext) {
153152
advance(retryState);
154153
assertThrows(RuntimeException.class, () -> retryState.breakAndThrowIfRetryAnd(() -> true));
155154
RuntimeException attemptException = new RuntimeException();
156-
assertAdvanceOrThrow(attemptException, retryState, attemptException);
155+
assertAdvanceOrThrowThrows(attemptException, retryState, attemptException);
157156
}
158157

159158
@Test
@@ -163,7 +162,7 @@ void breakAndThrowIfRetryAndTrueWithExpiredTimeout() {
163162
advance(retryState);
164163
assertThrows(RuntimeException.class, () -> retryState.breakAndThrowIfRetryAnd(() -> true));
165164
RuntimeException attemptException = new RuntimeException();
166-
assertAdvanceOrThrow(attemptException, retryState, attemptException);
165+
assertAdvanceOrThrowThrows(attemptException, retryState, attemptException);
167166
}
168167

169168
@ParameterizedTest
@@ -177,7 +176,7 @@ void breakAndThrowIfRetryIfPredicateThrows(final TimeoutContext timeoutContext)
177176
assertThrows(exception.getClass(), () -> retryState.breakAndThrowIfRetryAnd(() -> {
178177
throw exception;
179178
})));
180-
assertAdvanceOrThrow(null, retryState, exception);
179+
assertAdvanceOrThrowDoesNotThrow(retryState, exception);
181180
}
182181

183182
@ParameterizedTest
@@ -187,7 +186,7 @@ void breakAndCompleteIfRetryAndFirstAttempt(final TimeoutContext timeoutContext)
187186
SupplyingCallback<?> callback = new SupplyingCallback<>();
188187
assertFalse(retryState.breakAndCompleteIfRetryAnd(Assertions::fail, callback));
189188
assertFalse(callback.completed());
190-
assertAdvanceOrThrow(null, retryState, new RuntimeException());
189+
assertAdvanceOrThrowDoesNotThrow(retryState, new RuntimeException());
191190
}
192191

193192
@ParameterizedTest
@@ -198,7 +197,7 @@ void breakAndCompleteIfRetryAndFalse(final TimeoutContext timeoutContext) {
198197
SupplyingCallback<?> callback = new SupplyingCallback<>();
199198
assertFalse(retryState.breakAndCompleteIfRetryAnd(() -> false, callback));
200199
assertFalse(callback.completed());
201-
assertAdvanceOrThrow(null, retryState, new RuntimeException());
200+
assertAdvanceOrThrowDoesNotThrow(retryState, new RuntimeException());
202201
}
203202

204203
@ParameterizedTest
@@ -210,7 +209,7 @@ void breakAndCompleteIfRetryAndTrue(final TimeoutContext timeoutContext) {
210209
assertTrue(retryState.breakAndCompleteIfRetryAnd(() -> true, callback));
211210
assertThrows(RuntimeException.class, callback::get);
212211
RuntimeException attemptException = new RuntimeException();
213-
assertAdvanceOrThrow(attemptException, retryState, attemptException);
212+
assertAdvanceOrThrowThrows(attemptException, retryState, attemptException);
214213
}
215214

216215
@ParameterizedTest
@@ -226,15 +225,15 @@ void breakAndCompleteIfRetryAndPredicateThrows(final TimeoutContext timeoutConte
226225
assertEquals(
227226
exception,
228227
assertThrows(exception.getClass(), callback::get));
229-
assertAdvanceOrThrow(null, retryState, exception);
228+
assertAdvanceOrThrowDoesNotThrow(retryState, exception);
230229
}
231230

232231
@ParameterizedTest
233232
@MethodSource({"infiniteTimeout", "noTimeout"})
234233
void advanceOrThrowPredicateFalse(final TimeoutContext timeoutContext) {
235234
RetryState retryState = new RetryState(timeoutContext);
236235
RuntimeException attemptException = new RuntimeException();
237-
assertAdvanceOrThrow(attemptException, retryState, attemptException, (rs, e) -> false);
236+
assertAdvanceOrThrowThrows(attemptException, retryState, attemptException, (rs, e) -> false);
238237
}
239238

240239
@ParameterizedTest
@@ -244,7 +243,7 @@ void advanceReThrowDetectedTimeoutExceptionEvenIfTimeoutInRetryStateIsNotExpired
244243
RetryState retryState = new RetryState(timeoutContext);
245244

246245
MongoOperationTimeoutException expectedTimeoutException = TimeoutContext.createMongoTimeoutException("Server selection failed");
247-
assertAdvanceOrThrow(expectedTimeoutException, retryState, expectedTimeoutException,
246+
assertAdvanceOrThrowThrows(expectedTimeoutException, retryState, expectedTimeoutException,
248247
(e1, e2) -> expectedTimeoutException,
249248
(rs, e) -> false);
250249
}
@@ -284,7 +283,7 @@ void advanceThrowOriginalTimeoutExceptionWhenTransformerReturnsOriginalTimeoutEx
284283
(e1, e2) -> previousAttemptException,
285284
(rs, e) -> true);
286285

287-
assertAdvanceOrThrow(expectedTimeoutException, retryState, expectedTimeoutException,
286+
assertAdvanceOrThrowThrows(expectedTimeoutException, retryState, expectedTimeoutException,
288287
(e1, e2) -> expectedTimeoutException,
289288
(rs, e) -> false);
290289
}
@@ -293,7 +292,7 @@ void advanceThrowOriginalTimeoutExceptionWhenTransformerReturnsOriginalTimeoutEx
293292
void advanceOrThrowPredicateTrueAndLastAttempt() {
294293
RetryState retryState = RetryState.withNonRetryableState();
295294
Error attemptException = new Error();
296-
assertAdvanceOrThrow(attemptException, retryState, attemptException);
295+
assertAdvanceOrThrowThrows(attemptException, retryState, attemptException);
297296
}
298297

299298
@ParameterizedTest
@@ -302,7 +301,7 @@ void advanceOrThrowPredicateThrowsAfterFirstAttempt(final TimeoutContext timeout
302301
RetryState retryState = new RetryState(timeoutContext);
303302
RuntimeException predicateException = new RuntimeException();
304303
RuntimeException attemptException = new RuntimeException();
305-
assertAdvanceOrThrow(predicateException, retryState, attemptException,
304+
assertAdvanceOrThrowThrows(predicateException, retryState, attemptException,
306305
(e1, e2) -> e2,
307306
(rs, e) -> {
308307
assertTrue(rs.isFirstAttempt());
@@ -335,7 +334,7 @@ void advanceOrThrowPredicateThrows(final TimeoutContext timeoutContext) {
335334
retryState.advanceOrThrow(firstAttemptException, (e1, e2) -> e2, (rs, e) -> true);
336335
RuntimeException secondAttemptException = new RuntimeException();
337336
RuntimeException predicateException = new RuntimeException();
338-
assertAdvanceOrThrow(predicateException, retryState, secondAttemptException,
337+
assertAdvanceOrThrowThrows(predicateException, retryState, secondAttemptException,
339338
(e1, e2) -> e2,
340339
(rs, e) -> {
341340
assertEquals(1, rs.attempt());
@@ -349,7 +348,7 @@ void advanceOrThrowPredicateThrows(final TimeoutContext timeoutContext) {
349348
void advanceOrThrowTransformerThrowsAfterFirstAttempt(final TimeoutContext timeoutContext) {
350349
RetryState retryState = new RetryState(timeoutContext);
351350
RuntimeException transformerException = new RuntimeException();
352-
assertAdvanceOrThrow(transformerException, retryState, new AssertionError(),
351+
assertAdvanceOrThrowThrows(transformerException, retryState, new AssertionError(),
353352
(e1, e2) -> {
354353
throw transformerException;
355354
},
@@ -363,7 +362,7 @@ void advanceOrThrowTransformerThrows(final TimeoutContext timeoutContext) throws
363362
Error firstAttemptException = new Error();
364363
retryState.advanceOrThrow(firstAttemptException, (e1, e2) -> e2, (rs, e) -> true);
365364
RuntimeException transformerException = new RuntimeException();
366-
assertAdvanceOrThrow(transformerException, retryState, new AssertionError(),
365+
assertAdvanceOrThrowThrows(transformerException, retryState, new AssertionError(),
367366
(e1, e2) -> {
368367
throw transformerException;
369368
},
@@ -376,7 +375,7 @@ void advanceOrThrowTransformAfterFirstAttempt(final TimeoutContext timeoutContex
376375
RetryState retryState = new RetryState(timeoutContext);
377376
RuntimeException attemptException = new RuntimeException();
378377
RuntimeException transformerResult = new RuntimeException();
379-
assertAdvanceOrThrow(transformerResult, retryState, attemptException,
378+
assertAdvanceOrThrowThrows(transformerResult, retryState, attemptException,
380379
(e1, e2) -> {
381380
assertNull(e1);
382381
assertEquals(attemptException, e2);
@@ -419,7 +418,7 @@ void advanceOrThrowTransform(final TimeoutContext timeoutContext) {
419418
retryState.advanceOrThrow(firstAttemptException, (e1, e2) -> e2, (rs, e) -> true);
420419
RuntimeException secondAttemptException = new RuntimeException();
421420
RuntimeException transformerResult = new RuntimeException();
422-
assertAdvanceOrThrow(transformerResult, retryState, secondAttemptException,
421+
assertAdvanceOrThrowThrows(transformerResult, retryState, secondAttemptException,
423422
(e1, e2) -> {
424423
assertEquals(firstAttemptException, e1);
425424
assertEquals(secondAttemptException, e2);
@@ -452,34 +451,41 @@ private static void advance(final RetryState retryState) {
452451
retryState.advanceOrThrow(new RuntimeException(), (e1, e2) -> e2, (rs, e) -> true);
453452
}
454453

455-
private static void assertAdvanceOrThrow(
456-
@Nullable final Throwable expectedException,
454+
private static void assertAdvanceOrThrowDoesNotThrow(
457455
final RetryState retryState,
458456
final Throwable attemptException) {
459-
assertAdvanceOrThrow(expectedException, retryState, attemptException, (rs, e) -> true);
457+
assertDoesNotThrow(() -> retryState.advanceOrThrow(attemptException, (e1, e2) -> e2, (rs, e) -> true));
460458
}
461459

462-
private static void assertAdvanceOrThrow(
463-
@Nullable final Throwable expectedException,
460+
private static void assertAdvanceOrThrowThrows(
461+
final Throwable expectedException,
462+
final RetryState retryState,
463+
final Throwable attemptException) {
464+
assertAdvanceOrThrowThrows(
465+
com.mongodb.assertions.Assertions.assertNotNull(expectedException),
466+
retryState, attemptException, (rs, e) -> true);
467+
}
468+
469+
private static void assertAdvanceOrThrowThrows(
470+
final Throwable expectedException,
464471
final RetryState retryState,
465472
final Throwable attemptException,
466473
final BiPredicate<RetryState, Throwable> retryPredicate) {
467-
assertAdvanceOrThrow(expectedException, retryState, attemptException, (e1, e2) -> e2, retryPredicate);
474+
assertAdvanceOrThrowThrows(
475+
com.mongodb.assertions.Assertions.assertNotNull(expectedException),
476+
retryState, attemptException, (e1, e2) -> e2, retryPredicate);
468477
}
469478

470-
private static void assertAdvanceOrThrow(
471-
@Nullable final Throwable expectedException,
479+
private static void assertAdvanceOrThrowThrows(
480+
final Throwable expectedException,
472481
final RetryState retryState,
473482
final Throwable attemptException,
474483
final BinaryOperator<Throwable> onAttemptFailureOperator,
475484
final BiPredicate<RetryState, Throwable> retryPredicate) {
476-
if (expectedException == null) {
477-
assertDoesNotThrow(() -> retryState.advanceOrThrow(attemptException, onAttemptFailureOperator, retryPredicate));
478-
} else {
479-
assertEquals(
480-
expectedException,
481-
assertThrows(expectedException.getClass(), () ->
482-
retryState.advanceOrThrow(attemptException, onAttemptFailureOperator, retryPredicate)));
483-
}
485+
com.mongodb.assertions.Assertions.assertNotNull(expectedException);
486+
assertEquals(
487+
expectedException,
488+
assertThrows(expectedException.getClass(), () ->
489+
retryState.advanceOrThrow(attemptException, onAttemptFailureOperator, retryPredicate)));
484490
}
485491
}

0 commit comments

Comments
 (0)