|
26 | 26 | import java.time.temporal.ChronoUnit; |
27 | 27 | import java.util.concurrent.Executors; |
28 | 28 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 29 | +import java.util.concurrent.TimeoutException; |
29 | 30 | import java.util.concurrent.atomic.AtomicInteger; |
30 | 31 | import org.junit.After; |
31 | 32 | import org.junit.Before; |
@@ -514,6 +515,50 @@ public void testGetConnectionInfo_throwsTerminalException_refreshOperationNotSch |
514 | 515 | assertThat(refreshCount.get()).isEqualTo(1); |
515 | 516 | } |
516 | 517 |
|
| 518 | + @Test |
| 519 | + public void testGetConnectionInfo_throwsTerminalException_forceRefreshResumes() |
| 520 | + throws InterruptedException, TimeoutException { |
| 521 | + ExampleData data = new ExampleData(Instant.now().plus(1, ChronoUnit.HOURS)); |
| 522 | + AtomicInteger refreshCount = new AtomicInteger(); |
| 523 | + |
| 524 | + RefreshAheadStrategy r = |
| 525 | + new RefreshAheadStrategy( |
| 526 | + "RefresherTest.testGetConnectionInfo_throwsTerminalException_forceRefreshResumes", |
| 527 | + executorService, |
| 528 | + () -> { |
| 529 | + int c = refreshCount.get(); |
| 530 | + ExampleData refreshResult = data; |
| 531 | + if (c == 0) { // refresh 0 should throw an exception |
| 532 | + refreshCount.incrementAndGet(); |
| 533 | + throw new TerminalException("Not authorized"); |
| 534 | + } |
| 535 | + // refresh 2 and on should return data immediately |
| 536 | + refreshCount.incrementAndGet(); |
| 537 | + return Futures.immediateFuture(refreshResult); |
| 538 | + }, |
| 539 | + rateLimiter); |
| 540 | + |
| 541 | + // Raising TerminalException stops the refresher's executor from running the next task. |
| 542 | + assertThrows(TerminalException.class, () -> r.getConnectionInfo(TEST_TIMEOUT_MS)); |
| 543 | + assertThat(refreshCount.get()).isEqualTo(1); |
| 544 | + |
| 545 | + // Manually force a refresh |
| 546 | + r.forceRefresh(); |
| 547 | + |
| 548 | + // getConnectionInfo again, and assert the refresh operation completed. |
| 549 | + new PauseCondition() |
| 550 | + .waitForCondition( |
| 551 | + () -> { |
| 552 | + try { |
| 553 | + return r.getConnectionInfo(TEST_TIMEOUT_MS) == data; |
| 554 | + } catch (TerminalException e) { |
| 555 | + return false; |
| 556 | + } |
| 557 | + }, |
| 558 | + 1000L); |
| 559 | + assertThat(refreshCount.get()).isEqualTo(2); |
| 560 | + } |
| 561 | + |
517 | 562 | @Test |
518 | 563 | public void testGetConnectionInfo_throwsRuntimeException_refreshOperationScheduled() |
519 | 564 | throws Exception { |
|
0 commit comments