Skip to content

Commit

Permalink
Merge pull request #556 from commercetools/fix-retryhandler
Browse files Browse the repository at this point in the history
fix RetryHandler class cast exception
  • Loading branch information
jenschude authored Jan 22, 2024
2 parents 635439a + fa84a6e commit edcc4e9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,39 @@ public void concurrentMod() {
});
}

@Test
public void concurrentModWithoutRetry() {
ServiceRegion region = System.getenv("CTP_REGION") == null ? ServiceRegion.GCP_EUROPE_WEST1
: ServiceRegion.valueOf(System.getenv("CTP_REGION"));
String authURL = System.getenv("CTP_AUTH_URL") == null ? region.getOAuthTokenUrl()
: System.getenv("CTP_AUTH_URL");
String apiUrl = System.getenv("CTP_API_URL") == null ? region.getApiUrl() : System.getenv("CTP_API_URL");

final ProjectApiRoot projectApiRoot = ApiRootBuilder.of()
.defaultClient(ClientCredentials.of()
.withClientId(CommercetoolsTestUtils.getClientId())
.withClientSecret(CommercetoolsTestUtils.getClientSecret())
.build(),
authURL, apiUrl)
.withErrorMiddleware(ErrorMiddleware.ExceptionMode.UNWRAP_COMPLETION_EXCEPTION)
.build(CommercetoolsTestUtils.getProjectKey());

CartsFixtures.withUpdateableCart(cart -> {

final Cart modCart = RetryHandler
.concurrentModification(projectApiRoot.carts()
.withId(cart.getId())
.post(CartUpdateBuilder.of()
.version(cart.getVersion())
.actions(CartSetCountryActionBuilder.of().country("DE").build())
.build()),
CartUpdate::builder, CartUpdateBuilder::version)
.executeBlocking()
.getBody();
return modCart;
});
}

@Test
public void concurrentModDelete() {
ServiceRegion region = System.getenv("CTP_REGION") == null ? ServiceRegion.GCP_EUROPE_WEST1
Expand Down Expand Up @@ -112,6 +145,60 @@ public void concurrentModDelete() {
});
}

@Test
public void concurrentModDeleteWithoutRetry() {
ServiceRegion region = System.getenv("CTP_REGION") == null ? ServiceRegion.GCP_EUROPE_WEST1
: ServiceRegion.valueOf(System.getenv("CTP_REGION"));
String authURL = System.getenv("CTP_AUTH_URL") == null ? region.getOAuthTokenUrl()
: System.getenv("CTP_AUTH_URL");
String apiUrl = System.getenv("CTP_API_URL") == null ? region.getApiUrl() : System.getenv("CTP_API_URL");

final ProjectApiRoot projectApiRoot = ApiRootBuilder.of()
.defaultClient(ClientCredentials.of()
.withClientId(CommercetoolsTestUtils.getClientId())
.withClientSecret(CommercetoolsTestUtils.getClientSecret())
.build(),
authURL, apiUrl)
.withErrorMiddleware(ErrorMiddleware.ExceptionMode.UNWRAP_COMPLETION_EXCEPTION)
.build(CommercetoolsTestUtils.getProjectKey());

ProductTypeFixtures.withUpdateableProductType(productType -> {

final ProductType deletedProductType = RetryHandler
.concurrentModification(projectApiRoot.productTypes().delete(productType))
.executeBlocking()
.getBody();
return deletedProductType;
});
}

@Test
public void concurrentModMiddlewareSuccess() {
String projectKey = CommercetoolsTestUtils.getProjectKey();

ProjectApiRoot projectApiRoot = ApiRootBuilder.of()
.defaultClient(ClientCredentials.of()
.withClientId(CommercetoolsTestUtils.getClientId())
.withClientSecret(CommercetoolsTestUtils.getClientSecret())
.build(),
ServiceRegion.GCP_EUROPE_WEST1)
.addConcurrentModificationMiddleware(3)
.build(projectKey);

CartsFixtures.withUpdateableCart(cart -> {

final Cart modCart = projectApiRoot.carts()
.withId(cart.getId())
.post(CartUpdateBuilder.of()
.version(cart.getVersion())
.actions(CartSetCountryActionBuilder.of().country("DE").build())
.build())
.executeBlocking()
.getBody();
return modCart;
});
}

@Test
public void concurrentModMiddleware() {
String projectKey = CommercetoolsTestUtils.getProjectKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Function;

import com.commercetools.api.client.error.ConcurrentModificationException;
import com.spotify.futures.CompletableFutures;

import io.vrap.rmf.base.client.*;

Expand Down Expand Up @@ -41,9 +42,7 @@ public CompletableFuture<ApiHttpResponse<TResult>> execute() {
return f;
};

return request.execute()
.handle((r, ex) -> (ex == null) ? this : fn.apply(ex))
.thenCompose(x -> (CompletableFuture<ApiHttpResponse<TResult>>) x);
return CompletableFutures.exceptionallyCompose(request.execute(), fn).toCompletableFuture();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.commercetools.api.client.error.ConcurrentModificationException;
import com.commercetools.api.models.ResourceUpdate;
import com.spotify.futures.CompletableFutures;

import io.vrap.rmf.base.client.*;

Expand Down Expand Up @@ -45,9 +46,7 @@ public CompletableFuture<ApiHttpResponse<TResult>> execute() {
return f;
};

return request.execute()
.handle((r, ex) -> (ex == null) ? this : fn.apply(ex))
.thenCompose(x -> (CompletableFuture<ApiHttpResponse<TResult>>) x);
return CompletableFutures.exceptionallyCompose(request.execute(), fn).toCompletableFuture();
}

@Override
Expand Down

0 comments on commit edcc4e9

Please sign in to comment.