Skip to content

Commit

Permalink
fix: Ensure executor is shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjames committed Dec 18, 2024
1 parent 5c39f01 commit 972cc80
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ public CompletableFuture<List<ClientBatchCheckResponse>> batchCheck(
return CompletableFuture.completedFuture(new ArrayList<>(responses));
} catch (Exception e) {
return CompletableFuture.failedFuture(e);
} finally {
executor.shutdown();
}
}

Expand Down
38 changes: 38 additions & 0 deletions src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand All @@ -41,6 +43,7 @@
import org.hamcrest.Matcher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

/**
* API tests for OpenFgaClient.
Expand Down Expand Up @@ -1703,6 +1706,41 @@ public void batchCheck() throws Exception {
assertEquals(Boolean.TRUE, response.get(0).getAllowed());
}

@Test
public void shouldShutdownExecutorAfterBatchCheck() throws Exception {
// Given
ScheduledExecutorService mockExecutor = mock(ScheduledExecutorService.class);

try (MockedStatic<Executors> mockedExecutors = mockStatic(Executors.class)) {
mockedExecutors
.when(() -> Executors.newScheduledThreadPool(anyInt()))
.thenReturn(mockExecutor);

// mockExecutor needs to handle tasks submitted to it so latch can count down
doAnswer(invocation -> {
Runnable task = invocation.getArgument(0);
task.run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));

ClientCheckRequest request = new ClientCheckRequest()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER);
ClientBatchCheckOptions options = new ClientBatchCheckOptions()
.authorizationModelId(DEFAULT_AUTH_MODEL_ID)
.consistency(ConsistencyPreference.MINIMIZE_LATENCY);

// When
fga.batchCheck(List.of(request), options).get();

// Then
verify(mockExecutor).shutdown();
}
}

@Test
public void batchCheck_twentyTimes() throws Exception {
// Given
Expand Down

0 comments on commit 972cc80

Please sign in to comment.