Skip to content

Commit

Permalink
fix: default write to transaction mode
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Nov 22, 2023
1 parent af7d739 commit f8ae276
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ public CompletableFuture<ClientWriteResponse> write(ClientWriteRequest request,
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();

if (options != null && !options.disableTransactions()) {
return writeTransactions(storeId, request, options);
if (options != null && (options.disableTransactions() || options.getTransactionChunkSize() == 1)) {
return writeNonTransaction(storeId, request, options);
}

return writeNonTransaction(storeId, request, options);
return writeTransactions(storeId, request, options);
}

private CompletableFuture<ClientWriteResponse> writeNonTransaction(
Expand All @@ -305,7 +305,7 @@ private CompletableFuture<ClientWriteResponse> writeNonTransaction(
private CompletableFuture<ClientWriteResponse> writeTransactions(
String storeId, ClientWriteRequest request, ClientWriteOptions options) {

int chunkSize = options.getTransactionChunkSize();
int chunkSize = options == null ? DEFAULT_MAX_METHOD_PARALLEL_REQS : options.getTransactionChunkSize();

var writeTransactions = chunksOf(chunkSize, request.getWrites()).map(ClientWriteRequest::ofWrites);
var deleteTransactions = chunksOf(chunkSize, request.getDeletes()).map(ClientWriteRequest::ofDeletes);
Expand Down
32 changes: 22 additions & 10 deletions src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1142,8 +1142,8 @@ public void writeTest_transactionsWithFailure() throws Exception {
String failedUser = "user:SECOND";
String skippedUser = "user:third";
Function<String, String> writeBody = user -> String.format(
"{\"writes\":{\"tuple_keys\":[{\"object\":\"%s\",\"relation\":\"%s\",\"user\":\"%s\"}]},\"deletes\":null,\"authorization_model_id\":\"%s\"}",
DEFAULT_OBJECT, DEFAULT_RELATION, user, DEFAULT_AUTH_MODEL_ID);
"{\"writes\":{\"tuple_keys\":[{\"object\":\"%s\",\"relation\":\"%s\",\"user\":\"%s\"},{\"object\":\"%s\",\"relation\":\"%s\",\"user\":\"%s\"}]},\"deletes\":null,\"authorization_model_id\":\"%s\"}",
DEFAULT_OBJECT, DEFAULT_RELATION, user, DEFAULT_OBJECT, DEFAULT_RELATION, user, DEFAULT_AUTH_MODEL_ID);
mockHttpClient
.onPost(postPath)
.withBody(isOneOf(writeBody.apply(firstUser), writeBody.apply(skippedUser)))
Expand All @@ -1153,14 +1153,14 @@ public void writeTest_transactionsWithFailure() throws Exception {
.withBody(is(writeBody.apply(failedUser)))
.doReturn(400, "{\"code\":\"validation_error\",\"message\":\"Generic validation error\"}");
ClientWriteRequest request = new ClientWriteRequest()
.writes(Stream.of(firstUser, failedUser, skippedUser)
.writes(Stream.of(firstUser, firstUser, failedUser, failedUser, skippedUser, skippedUser)
.map(user -> new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(user))
.collect(Collectors.toList()));
ClientWriteOptions options =
new ClientWriteOptions().disableTransactions(false).transactionChunkSize(1);
new ClientWriteOptions().disableTransactions(false).transactionChunkSize(2);

// When
var execException = assertThrows(
Expand Down Expand Up @@ -1249,11 +1249,15 @@ public void write_400() throws Exception {
mockHttpClient
.onPost(postUrl)
.doReturn(400, "{\"code\":\"validation_error\",\"message\":\"Generic validation error\"}");
ClientWriteRequest request = new ClientWriteRequest()
.writes(List.of(new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER)));

// When
ExecutionException execException =
assertThrows(ExecutionException.class, () -> fga.write(new ClientWriteRequest())
.get());
assertThrows(ExecutionException.class, () -> fga.write(request).get());

// Then
mockHttpClient.verify().post(postUrl).called(1);
Expand All @@ -1271,11 +1275,15 @@ public void write_404() throws Exception {
mockHttpClient
.onPost(postUrl)
.doReturn(404, "{\"code\":\"undefined_endpoint\",\"message\":\"Endpoint not enabled\"}");
ClientWriteRequest request = new ClientWriteRequest()
.writes(List.of(new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER)));

// When
ExecutionException execException =
assertThrows(ExecutionException.class, () -> fga.write(new ClientWriteRequest())
.get());
assertThrows(ExecutionException.class, () -> fga.write(request).get());

// Then
mockHttpClient.verify().post(postUrl).called(1);
Expand All @@ -1292,11 +1300,15 @@ public void write_500() throws Exception {
mockHttpClient
.onPost(postUrl)
.doReturn(500, "{\"code\":\"internal_error\",\"message\":\"Internal Server Error\"}");
ClientWriteRequest request = new ClientWriteRequest()
.writes(List.of(new ClientTupleKey()
._object(DEFAULT_OBJECT)
.relation(DEFAULT_RELATION)
.user(DEFAULT_USER)));

// When
ExecutionException execException =
assertThrows(ExecutionException.class, () -> fga.write(new ClientWriteRequest())
.get());
assertThrows(ExecutionException.class, () -> fga.write(request).get());

// Then
mockHttpClient.verify().post(postUrl).called(1 + DEFAULT_MAX_RETRIES);
Expand Down

0 comments on commit f8ae276

Please sign in to comment.