|
| 1 | +package com.spotify.confidence; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.atLeastOnce; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.spy; |
| 9 | +import static org.mockito.Mockito.times; |
| 10 | +import static org.mockito.Mockito.verify; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | +import com.dylibso.chicory.wasm.ChicoryException; |
| 14 | +import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsRequest; |
| 15 | +import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsResponse; |
| 16 | +import java.util.concurrent.atomic.AtomicReference; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +public class SwapWasmResolverApiTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testChicoryExceptionTriggersStateReset() throws Exception { |
| 23 | + // Create a mock WasmFlagLogger |
| 24 | + final WasmFlagLogger mockLogger = mock(WasmFlagLogger.class); |
| 25 | + |
| 26 | + // Use valid test state bytes and accountId |
| 27 | + final byte[] initialState = ResolveTest.exampleStateBytes; |
| 28 | + final String accountId = "test-account-id"; |
| 29 | + |
| 30 | + // Create a spy of SwapWasmResolverApi to verify method calls |
| 31 | + final SwapWasmResolverApi swapApi = |
| 32 | + spy( |
| 33 | + new SwapWasmResolverApi( |
| 34 | + mockLogger, initialState, accountId, mock(ResolverFallback.class))); |
| 35 | + |
| 36 | + // Create a mock WasmResolveApi that throws ChicoryException |
| 37 | + final WasmResolveApi mockWasmApi = mock(WasmResolveApi.class); |
| 38 | + when(mockWasmApi.resolve(any(ResolveFlagsRequest.class))) |
| 39 | + .thenThrow(new ChicoryException("WASM runtime error")); |
| 40 | + |
| 41 | + // Replace the internal WasmResolveApi with our mock |
| 42 | + final var field = SwapWasmResolverApi.class.getDeclaredField("wasmResolverApiRef"); |
| 43 | + field.setAccessible(true); |
| 44 | + @SuppressWarnings("unchecked") |
| 45 | + final AtomicReference<WasmResolveApi> ref = |
| 46 | + (AtomicReference<WasmResolveApi>) field.get(swapApi); |
| 47 | + ref.set(mockWasmApi); |
| 48 | + |
| 49 | + // Create a test resolve request |
| 50 | + final ResolveFlagsRequest request = |
| 51 | + ResolveFlagsRequest.newBuilder() |
| 52 | + .addFlags("test-flag") |
| 53 | + .setClientSecret("test-secret") |
| 54 | + .build(); |
| 55 | + |
| 56 | + // Execute the resolve and expect ChicoryException to be thrown |
| 57 | + assertThrows(ChicoryException.class, () -> swapApi.resolve(request)); |
| 58 | + |
| 59 | + // Verify that updateStateAndFlushLogs was called with null state and the accountId |
| 60 | + verify(swapApi, atLeastOnce()).updateStateAndFlushLogs(null, accountId); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testIsClosedExceptionTriggersRetry() throws Exception { |
| 65 | + // Create a mock WasmFlagLogger |
| 66 | + final WasmFlagLogger mockLogger = mock(WasmFlagLogger.class); |
| 67 | + |
| 68 | + // Use valid test state bytes and accountId |
| 69 | + final byte[] initialState = ResolveTest.exampleStateBytes; |
| 70 | + final String accountId = "test-account-id"; |
| 71 | + |
| 72 | + // Create a spy of SwapWasmResolverApi to verify method calls |
| 73 | + final SwapWasmResolverApi swapApi = |
| 74 | + spy( |
| 75 | + new SwapWasmResolverApi( |
| 76 | + mockLogger, initialState, accountId, mock(ResolverFallback.class))); |
| 77 | + |
| 78 | + // Create a mock WasmResolveApi that throws IsClosedException on first call, then succeeds |
| 79 | + final WasmResolveApi mockWasmApi = mock(WasmResolveApi.class); |
| 80 | + final ResolveFlagsResponse mockResponse = ResolveFlagsResponse.newBuilder().build(); |
| 81 | + when(mockWasmApi.resolve(any(ResolveFlagsRequest.class))) |
| 82 | + .thenThrow(new IsClosedException()) |
| 83 | + .thenReturn(mockResponse); |
| 84 | + |
| 85 | + // Replace the internal WasmResolveApi with our mock |
| 86 | + final var field = SwapWasmResolverApi.class.getDeclaredField("wasmResolverApiRef"); |
| 87 | + field.setAccessible(true); |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + final AtomicReference<WasmResolveApi> ref = |
| 90 | + (AtomicReference<WasmResolveApi>) field.get(swapApi); |
| 91 | + ref.set(mockWasmApi); |
| 92 | + |
| 93 | + // Create a test resolve request |
| 94 | + final ResolveFlagsRequest request = |
| 95 | + ResolveFlagsRequest.newBuilder() |
| 96 | + .addFlags("flags/flag-1") |
| 97 | + .setClientSecret(TestBase.secret.getSecret()) |
| 98 | + .build(); |
| 99 | + |
| 100 | + // Call resolve - it should retry when IsClosedException is thrown and succeed on second attempt |
| 101 | + final ResolveFlagsResponse response = swapApi.resolve(request); |
| 102 | + |
| 103 | + // Verify response is not null |
| 104 | + assertNotNull(response); |
| 105 | + |
| 106 | + // Verify that the mock WasmResolveApi.resolve was called twice (first threw exception, second |
| 107 | + // succeeded) |
| 108 | + verify(mockWasmApi, times(2)).resolve(request); |
| 109 | + } |
| 110 | +} |
0 commit comments