-
Notifications
You must be signed in to change notification settings - Fork 3
fix: replace insteance on panic, add tests #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
openfeature-provider-local/src/test/java/com/spotify/confidence/SwapWasmResolverApiTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package com.spotify.confidence; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.atLeastOnce; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.dylibso.chicory.wasm.ChicoryException; | ||
| import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsRequest; | ||
| import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsResponse; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class SwapWasmResolverApiTest { | ||
|
|
||
| @Test | ||
| public void testChicoryExceptionTriggersStateReset() throws Exception { | ||
| // Create a mock WasmFlagLogger | ||
| final WasmFlagLogger mockLogger = mock(WasmFlagLogger.class); | ||
|
|
||
| // Use valid test state bytes and accountId | ||
| final byte[] initialState = ResolveTest.exampleStateBytes; | ||
| final String accountId = "test-account-id"; | ||
|
|
||
| // Create a spy of SwapWasmResolverApi to verify method calls | ||
| final SwapWasmResolverApi swapApi = | ||
| spy( | ||
| new SwapWasmResolverApi( | ||
| mockLogger, initialState, accountId, mock(ResolverFallback.class))); | ||
|
|
||
| // Create a mock WasmResolveApi that throws ChicoryException | ||
| final WasmResolveApi mockWasmApi = mock(WasmResolveApi.class); | ||
| when(mockWasmApi.resolve(any(ResolveFlagsRequest.class))) | ||
| .thenThrow(new ChicoryException("WASM runtime error")); | ||
|
|
||
| // Replace the internal WasmResolveApi with our mock | ||
| final var field = SwapWasmResolverApi.class.getDeclaredField("wasmResolverApiRef"); | ||
| field.setAccessible(true); | ||
| @SuppressWarnings("unchecked") | ||
| final AtomicReference<WasmResolveApi> ref = | ||
| (AtomicReference<WasmResolveApi>) field.get(swapApi); | ||
| ref.set(mockWasmApi); | ||
|
|
||
| // Create a test resolve request | ||
| final ResolveFlagsRequest request = | ||
| ResolveFlagsRequest.newBuilder() | ||
| .addFlags("test-flag") | ||
| .setClientSecret("test-secret") | ||
| .build(); | ||
|
|
||
| // Execute the resolve and expect ChicoryException to be thrown | ||
| assertThrows(ChicoryException.class, () -> swapApi.resolve(request)); | ||
|
|
||
| // Verify that updateStateAndFlushLogs was called with null state and the accountId | ||
| verify(swapApi, atLeastOnce()).updateStateAndFlushLogs(null, accountId); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsClosedExceptionTriggersRetry() throws Exception { | ||
| // Create a mock WasmFlagLogger | ||
| final WasmFlagLogger mockLogger = mock(WasmFlagLogger.class); | ||
|
|
||
| // Use valid test state bytes and accountId | ||
| final byte[] initialState = ResolveTest.exampleStateBytes; | ||
| final String accountId = "test-account-id"; | ||
|
|
||
| // Create a spy of SwapWasmResolverApi to verify method calls | ||
| final SwapWasmResolverApi swapApi = | ||
| spy( | ||
| new SwapWasmResolverApi( | ||
| mockLogger, initialState, accountId, mock(ResolverFallback.class))); | ||
|
|
||
| // Create a mock WasmResolveApi that throws IsClosedException on first call, then succeeds | ||
| final WasmResolveApi mockWasmApi = mock(WasmResolveApi.class); | ||
| final ResolveFlagsResponse mockResponse = ResolveFlagsResponse.newBuilder().build(); | ||
| when(mockWasmApi.resolve(any(ResolveFlagsRequest.class))) | ||
| .thenThrow(new IsClosedException()) | ||
| .thenReturn(mockResponse); | ||
|
|
||
| // Replace the internal WasmResolveApi with our mock | ||
| final var field = SwapWasmResolverApi.class.getDeclaredField("wasmResolverApiRef"); | ||
| field.setAccessible(true); | ||
| @SuppressWarnings("unchecked") | ||
| final AtomicReference<WasmResolveApi> ref = | ||
| (AtomicReference<WasmResolveApi>) field.get(swapApi); | ||
| ref.set(mockWasmApi); | ||
|
|
||
| // Create a test resolve request | ||
| final ResolveFlagsRequest request = | ||
| ResolveFlagsRequest.newBuilder() | ||
| .addFlags("flags/flag-1") | ||
| .setClientSecret(TestBase.secret.getSecret()) | ||
| .build(); | ||
|
|
||
| // Call resolve - it should retry when IsClosedException is thrown and succeed on second attempt | ||
| final ResolveFlagsResponse response = swapApi.resolve(request); | ||
|
|
||
| // Verify response is not null | ||
| assertNotNull(response); | ||
|
|
||
| // Verify that the mock WasmResolveApi.resolve was called twice (first threw exception, second | ||
| // succeeded) | ||
| verify(mockWasmApi, times(2)).resolve(request); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.