|
41 | 41 | import static org.mockito.ArgumentMatchers.anyLong; |
42 | 42 | import static org.mockito.ArgumentMatchers.eq; |
43 | 43 | import static org.mockito.ArgumentMatchers.isNull; |
| 44 | +import static org.mockito.Mockito.atLeast; |
44 | 45 | import static org.mockito.Mockito.atLeastOnce; |
45 | 46 | import static org.mockito.Mockito.clearInvocations; |
46 | 47 | import static org.mockito.Mockito.mock; |
@@ -1779,4 +1780,80 @@ public void testRecoveryDisabled_probingSucceedsButChannelRemainsInFallback() { |
1779 | 1780 | sharedState.shutdown(); |
1780 | 1781 | } |
1781 | 1782 | } |
| 1783 | + |
| 1784 | + @Test |
| 1785 | + public void testPoolLevelRecovery_multipleFailoverCyclesResetProbeStatistics() { |
| 1786 | + GcpFallbackState sharedState = new GcpFallbackState(); |
| 1787 | + sharedState.getInFallbackMode().set(true); // Cycle 1: Fallback active |
| 1788 | + |
| 1789 | + GcpFallbackChannelOptions options1 = |
| 1790 | + getDefaultOptionsBuilder() |
| 1791 | + .setSharedState(sharedState) |
| 1792 | + .setEnableRecovery(true) |
| 1793 | + .setEnablePerChannelRecovery(false) |
| 1794 | + .setPrimaryProbingFunction(channel -> "OK") |
| 1795 | + .setMinPrimaryProbeSuccessCount(2) |
| 1796 | + .setMinPrimaryProbeSuccessDuration(Duration.ZERO) |
| 1797 | + .build(); |
| 1798 | + |
| 1799 | + GcpFallbackChannelOptions options2 = |
| 1800 | + getDefaultOptionsBuilder() |
| 1801 | + .setSharedState(sharedState) |
| 1802 | + .setEnableRecovery(true) |
| 1803 | + .setEnablePerChannelRecovery(false) |
| 1804 | + .setPrimaryProbingFunction(channel -> "OK") |
| 1805 | + .setMinPrimaryProbeSuccessCount(2) |
| 1806 | + .setMinPrimaryProbeSuccessDuration(Duration.ZERO) |
| 1807 | + .build(); |
| 1808 | + |
| 1809 | + ScheduledExecutorService mockExec = mock(ScheduledExecutorService.class); |
| 1810 | + ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class); |
| 1811 | + |
| 1812 | + GcpFallbackChannel channel1 = |
| 1813 | + new GcpFallbackChannel(options1, mockPrimaryBuilder, mockFallbackBuilder, mockExec); |
| 1814 | + GcpFallbackChannel channel2 = |
| 1815 | + new GcpFallbackChannel(options2, mockPrimaryBuilder, mockFallbackBuilder, mockExec); |
| 1816 | + |
| 1817 | + try { |
| 1818 | + verify(mockExec, atLeast(2)) |
| 1819 | + .scheduleAtFixedRate( |
| 1820 | + taskCaptor.capture(), |
| 1821 | + eq(options1.getPrimaryProbingInterval().toMillis()), |
| 1822 | + eq(options1.getPrimaryProbingInterval().toMillis()), |
| 1823 | + eq(MILLISECONDS)); |
| 1824 | + Runnable probeTask1 = taskCaptor.getAllValues().get(0); |
| 1825 | + Runnable probeTask2 = taskCaptor.getAllValues().get(1); |
| 1826 | + |
| 1827 | + // Both channels enter fallback |
| 1828 | + assertTrue(channel1.isInFallbackMode()); |
| 1829 | + assertTrue(channel2.isInFallbackMode()); |
| 1830 | + |
| 1831 | + // Channel 1 probes twice -> recovers pool |
| 1832 | + probeTask1.run(); |
| 1833 | + probeTask1.run(); |
| 1834 | + assertFalse(channel1.isInFallbackMode()); |
| 1835 | + assertFalse(channel2.isInFallbackMode()); |
| 1836 | + |
| 1837 | + // Now Cycle 2: Incident occurs again, pool enters fallback |
| 1838 | + sharedState.getInFallbackMode().set(true); |
| 1839 | + assertTrue(channel2.isInFallbackMode()); |
| 1840 | + |
| 1841 | + // Channel 2's localProbeSuccesses must be reset to 0 in new cycle |
| 1842 | + assertEquals(0, channel2.getLocalProbeSuccesses().get()); |
| 1843 | + |
| 1844 | + // Probe once: count is 1 (< 2 required), must still be in fallback |
| 1845 | + probeTask2.run(); |
| 1846 | + assertEquals(1, channel2.getLocalProbeSuccesses().get()); |
| 1847 | + assertTrue(channel2.isInFallbackMode()); |
| 1848 | + |
| 1849 | + // Probe second time: count is 2 (>= 2 required) -> recovers! |
| 1850 | + probeTask2.run(); |
| 1851 | + assertFalse(channel2.isInFallbackMode()); |
| 1852 | + assertFalse(sharedState.getInFallbackMode().get()); |
| 1853 | + } finally { |
| 1854 | + channel1.shutdownNow(); |
| 1855 | + channel2.shutdownNow(); |
| 1856 | + sharedState.shutdown(); |
| 1857 | + } |
| 1858 | + } |
1782 | 1859 | } |
0 commit comments