From b3326537f0e43db2794fdb575e077d445d865bb3 Mon Sep 17 00:00:00 2001 From: Steve Chaloner Date: Thu, 19 Nov 2026 14:24:03 +0100 Subject: [PATCH] Idempotency keys on write requests Send an Idempotency-Key (v4 UUID) with every POST/PUT/DELETE so a request whose outcome is unknown can be retried safely. - SeatsioException exposes idempotencyKey and outcomeIsUnknown() (connection failure, 5xx, or key still in use after retries) - SeatsioClient.withIdempotencyKey(key) returns a client that reuses a key; rejects non-v4-UUID keys - Retry 409 IDEMPOTENCY_KEY_IN_USE with backoff, like 429 - retryNonIdempotentRequests(true) opts in to retrying POST/PUT/DELETE on connection errors with the same key (off by default) - Stop retrying when the thread is interrupted - Error bodies without an error code no longer break parsing: errors and requestId are null, message contains status and body (ErrorResponse) - README: document idempotency, retries and the new exception fields --- README.md | 42 ++- src/main/java/seatsio/SeatsioClient.java | 25 +- src/main/java/seatsio/SeatsioException.java | 67 ++-- .../RateLimitExceededException.java | 4 + src/main/java/seatsio/util/ErrorResponse.java | 35 ++ .../java/seatsio/util/UnirestWrapper.java | 126 +++++-- src/test/java/seatsio/ErrorResponseTest.java | 44 +++ src/test/java/seatsio/FakeServer.java | 201 +++++++++++ src/test/java/seatsio/IdempotencyKeyTest.java | 159 +++++++++ .../java/seatsio/IdempotentRetryTest.java | 322 ++++++++++++++++++ src/test/java/seatsio/SeatsioClientTest.java | 13 + .../java/seatsio/events/IdempotencyTest.java | 109 ++++++ 12 files changed, 1090 insertions(+), 57 deletions(-) create mode 100644 src/main/java/seatsio/util/ErrorResponse.java create mode 100644 src/test/java/seatsio/ErrorResponseTest.java create mode 100644 src/test/java/seatsio/FakeServer.java create mode 100644 src/test/java/seatsio/IdempotencyKeyTest.java create mode 100644 src/test/java/seatsio/IdempotentRetryTest.java create mode 100644 src/test/java/seatsio/events/IdempotencyTest.java diff --git a/README.md b/README.md index c9c52036..3ddd2add 100644 --- a/README.md +++ b/README.md @@ -237,10 +237,50 @@ client.charts.updateCategory("the chart key", CategoryKey.of("the category key") When an API call results in a 4xx or 5xx error (e.g. when a chart could not be found), a SeatsioException is thrown. -This exception contains a message string describing what went wrong, and also two other properties: +This exception contains a message string describing what went wrong, and also these properties: - `errors`: a list of errors that the server returned. In most cases, this array will contain only one element, an instance of ApiError, containing an error code and a message. - `requestId`: the identifier of the request you made. Please mention this to us when you have questions, as it will make debugging easier. +- `idempotencyKey`: the `Idempotency-Key` sent with a `POST`, `PUT` or `DELETE` request (see [Idempotency](#idempotency)), or `null` for other requests. + +`outcomeIsUnknown()` tells you whether the request may or may not have been executed: it's `true` when the connection failed or the server answered with a 5xx, and `false` for a 4xx. + +If the server returns an error response without an error code (for example, an empty `errors` array), `errors` and `requestId` are `null`, and the message contains the HTTP status and the response body. + +## Idempotency + +**NOTE**: Idempotency is currently in a soft launch: it is enabled per company and not yet publicly available. Contact seats.io support to have it enabled for your company. + +Every `POST`, `PUT` and `DELETE` request is sent with an `Idempotency-Key` header (a v4 UUID, generated by the client). If idempotency is enabled for your company, the server executes a request with a given key only once, and replays the original response (with the header `Idempotent-Replayed: true`) when it receives the same request again. If it isn't enabled, the server ignores the key. + +If the connection fails after the server executed a request but before you received the response, you don't know whether the operation succeeded. The exception contains the key, so you can retry the operation with the same key: + +```java +try { + client.events.create(chartKey); +} catch (SeatsioException e) { + // only retry when the outcome is unknown (a connection error, a timeout or a 5xx); retrying a 4xx can never succeed + if (e.idempotencyKey != null && e.outcomeIsUnknown()) { + client.withIdempotencyKey(e.idempotencyKey).events.create(chartKey); + } +} +``` + +The retried request must be identical to the original one, otherwise the server returns an error `422`. Every `POST`, `PUT` and `DELETE` made with the client returned by `withIdempotencyKey()` sends that key, so use it for one operation only. You can also supply your own key; it must be a v4 UUID, otherwise `withIdempotencyKey()` throws an `IllegalArgumentException`. + +### Automatic retries + +If the server answers `409` because an earlier request with the same key is still being processed, the client waits and retries it with the same key, like it does for `429`. + +By default, the client doesn't retry `POST`, `PUT` and `DELETE` requests when the connection fails. To retry them (with the same key), enable it **only if idempotency is enabled for your company**. Otherwise, a retry could execute an operation that already succeeded a second time: + +```java +SeatsioClient client = new SeatsioClient(Region.EU, ).retryNonIdempotentRequests(true); +``` + +This setting also applies to the clients returned by `withIdempotencyKey()`, and the number of retries is limited by `maxRetries()`. + +The client stops retrying when its thread is interrupted (the interrupt flag stays set). This applies to all retries, including those after a `429`. ## Rate limiting - exponential backoff diff --git a/src/main/java/seatsio/SeatsioClient.java b/src/main/java/seatsio/SeatsioClient.java index 17b747f6..a8630b5f 100644 --- a/src/main/java/seatsio/SeatsioClient.java +++ b/src/main/java/seatsio/SeatsioClient.java @@ -14,6 +14,8 @@ public class SeatsioClient { + public static final String IDEMPOTENCY_KEY_HEADER = "Idempotency-Key"; + private final String baseUrl; private final UnirestWrapper unirest; @@ -29,8 +31,12 @@ public class SeatsioClient { public final TicketBuyers ticketBuyers; public SeatsioClient(String secretKey, String workspaceKey, String baseUrl) { + this(baseUrl, new UnirestWrapper(secretKey, workspaceKey)); + } + + private SeatsioClient(String baseUrl, UnirestWrapper unirest) { this.baseUrl = baseUrl; - this.unirest = new UnirestWrapper(secretKey, workspaceKey); + this.unirest = unirest; this.workspaces = new Workspaces(baseUrl, unirest); this.holdTokens = new HoldTokens(baseUrl, unirest); this.charts = new Charts(baseUrl, unirest); @@ -56,6 +62,22 @@ public SeatsioClient maxRetries(int maxRetries) { return this; } + public SeatsioClient retryNonIdempotentRequests(boolean retryNonIdempotentRequests) { + unirest.retryNonIdempotentRequests(retryNonIdempotentRequests); + return this; + } + + /** + * Returns a client that sends the given Idempotency-Key with every POST, PUT and DELETE request. + * Use it to retry an operation whose outcome is unknown: take the key from SeatsioException.idempotencyKey and + * make the same call again on the returned client. Use the returned client for that one operation only. + * + * @throws IllegalArgumentException if the key is not a v4 UUID + */ + public SeatsioClient withIdempotencyKey(String idempotencyKey) { + return new SeatsioClient(baseUrl, unirest.withIdempotencyKey(idempotencyKey)); + } + public String getBaseUrl() { return baseUrl; } @@ -63,5 +85,4 @@ public String getBaseUrl() { public void reinitializeHttpConnectionPool() { this.unirest.reinitializeHttpConnectionPool(); } - } diff --git a/src/main/java/seatsio/SeatsioException.java b/src/main/java/seatsio/SeatsioException.java index e328ff63..eeee4611 100644 --- a/src/main/java/seatsio/SeatsioException.java +++ b/src/main/java/seatsio/SeatsioException.java @@ -4,17 +4,20 @@ import kong.unirest.RawResponse; import seatsio.events.BestAvailableObjectsNotFoundException; import seatsio.exceptions.RateLimitExceededException; +import seatsio.util.ErrorResponse; +import seatsio.util.UnirestWrapper; import java.util.List; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.joining; -import static seatsio.json.SeatsioGson.gson; public class SeatsioException extends RuntimeException { public List errors; public String requestId; + public String idempotencyKey; + private boolean outcomeIsUnknown; public SeatsioException(String message) { super(message); @@ -22,6 +25,8 @@ public SeatsioException(String message) { public SeatsioException(Throwable t, HttpRequest request) { super("Error while executing " + request.getHttpMethod() + " " + request.getUrl(), t); + this.idempotencyKey = idempotencyKey(request); + this.outcomeIsUnknown = true; } protected SeatsioException(List errors, String requestId) { @@ -30,43 +35,55 @@ protected SeatsioException(List errors, String requestId) { this.requestId = requestId; } - private SeatsioException(HttpRequest request, RawResponse response) { - super(exceptionMessage(request, response)); + private SeatsioException(HttpRequest request, RawResponse response, byte[] responseBody) { + super(exceptionMessage(request, response, responseBody)); } public static SeatsioException from(HttpRequest request, RawResponse response, byte[] responseBody) { - if (response.getHeaders().getFirst("Content-Type").contains("application/json")) { - SeatsioExceptionTO parsedException = fromJson(responseBody); - if (response.getStatus() == 429) { - return new RateLimitExceededException(parsedException.errors, parsedException.requestId); - } else if (isBestAvailableObjectsNotFound(parsedException)) { - return new BestAvailableObjectsNotFoundException(parsedException.errors, parsedException.requestId); - } - return new SeatsioException(parsedException.errors, parsedException.requestId); - } - return new SeatsioException(request, response); + SeatsioException exception = create(request, response, responseBody); + exception.idempotencyKey = idempotencyKey(request); + exception.outcomeIsUnknown = response.getStatus() >= 500 || UnirestWrapper.isIdempotencyKeyInUse(response, responseBody); + return exception; } - private static boolean isBestAvailableObjectsNotFound(SeatsioExceptionTO parsedException) { - return parsedException.errors.stream().anyMatch(e -> "BEST_AVAILABLE_OBJECTS_NOT_FOUND".equals(e.getCode())); + /** + * True if the request may or may not have been executed: the connection failed, the server answered with a 5xx, or the key was still in use after all retries (the first request may still succeed). + * Only then can retrying the same operation (with the same idempotencyKey) give a different result. + */ + public boolean outcomeIsUnknown() { + return outcomeIsUnknown; } - private static String exceptionMessage(List errors) { - return errors.stream().map(ApiError::getMessage).collect(joining(", ")); + private static String idempotencyKey(HttpRequest request) { + String key = request.getHeaders().getFirst(SeatsioClient.IDEMPOTENCY_KEY_HEADER); + return key.isEmpty() ? null : key; } - private static String exceptionMessage(HttpRequest request, RawResponse response) { - return request.getHttpMethod() + " " + request.getUrl() + " resulted in a " + response.getStatus() + " " + response.getStatusText() + " response. Body: " + response.getContentAsString(); + private static SeatsioException create(HttpRequest request, RawResponse response, byte[] responseBody) { + ErrorResponse errorResponse = ErrorResponse.parseIfAny(response, responseBody); + if (errorResponse == null) { + if (response.getStatus() == 429) { + return new RateLimitExceededException(exceptionMessage(request, response, responseBody)); + } + return new SeatsioException(request, response, responseBody); + } + if (response.getStatus() == 429) { + return new RateLimitExceededException(errorResponse.errors, errorResponse.requestId); + } else if (isBestAvailableObjectsNotFound(errorResponse)) { + return new BestAvailableObjectsNotFoundException(errorResponse.errors, errorResponse.requestId); + } + return new SeatsioException(errorResponse.errors, errorResponse.requestId); } - private static SeatsioExceptionTO fromJson(byte[] responseBody) { - return gson().fromJson(new String(responseBody, UTF_8), SeatsioExceptionTO.class); + private static boolean isBestAvailableObjectsNotFound(ErrorResponse errorResponse) { + return errorResponse.errors.stream().anyMatch(e -> "BEST_AVAILABLE_OBJECTS_NOT_FOUND".equals(e.getCode())); } - private static class SeatsioExceptionTO { - - public List errors; - public String requestId; + private static String exceptionMessage(List errors) { + return errors.stream().map(ApiError::getMessage).collect(joining(", ")); + } + private static String exceptionMessage(HttpRequest request, RawResponse response, byte[] responseBody) { + return request.getHttpMethod() + " " + request.getUrl() + " resulted in a " + response.getStatus() + " " + response.getStatusText() + " response. Body: " + new String(responseBody, UTF_8); } } diff --git a/src/main/java/seatsio/exceptions/RateLimitExceededException.java b/src/main/java/seatsio/exceptions/RateLimitExceededException.java index 5fc0812d..fbdf050a 100644 --- a/src/main/java/seatsio/exceptions/RateLimitExceededException.java +++ b/src/main/java/seatsio/exceptions/RateLimitExceededException.java @@ -10,4 +10,8 @@ public class RateLimitExceededException extends SeatsioException { public RateLimitExceededException(List errors, String requestId) { super(errors, requestId); } + + public RateLimitExceededException(String message) { + super(message); + } } diff --git a/src/main/java/seatsio/util/ErrorResponse.java b/src/main/java/seatsio/util/ErrorResponse.java new file mode 100644 index 00000000..38cc146d --- /dev/null +++ b/src/main/java/seatsio/util/ErrorResponse.java @@ -0,0 +1,35 @@ +package seatsio.util; + +import com.google.gson.JsonParseException; +import kong.unirest.RawResponse; +import seatsio.ApiError; + +import java.util.List; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static seatsio.json.SeatsioGson.gson; + +public class ErrorResponse { + + public List errors; + public String requestId; + + public static ErrorResponse parseIfAny(RawResponse response, byte[] responseBody) { + if (!response.getHeaders().getFirst("Content-Type").contains("application/json")) { + return null; + } + try { + ErrorResponse errorResponse = gson().fromJson(new String(responseBody, UTF_8), ErrorResponse.class); + if (errorResponse == null || errorResponse.errors == null || errorResponse.errors.isEmpty() || errorResponse.errors.contains(null)) { + return null; + } + return errorResponse; + } catch (JsonParseException e) { + return null; + } + } + + public String firstErrorCode() { + return errors.get(0).getCode(); + } +} diff --git a/src/main/java/seatsio/util/UnirestWrapper.java b/src/main/java/seatsio/util/UnirestWrapper.java index 5cbff59d..992b7d0c 100644 --- a/src/main/java/seatsio/util/UnirestWrapper.java +++ b/src/main/java/seatsio/util/UnirestWrapper.java @@ -1,9 +1,15 @@ package seatsio.util; import kong.unirest.*; +import org.apache.http.client.ClientProtocolException; +import seatsio.SeatsioClient; import seatsio.SeatsioException; +import java.io.IOException; +import java.util.Set; +import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; +import java.util.regex.Pattern; import static java.nio.charset.StandardCharsets.UTF_8; @@ -13,42 +19,103 @@ public class UnirestWrapper { .socketTimeout(10000) .concurrency(200, 200)); - private int maxRetries = 5; + private static final Set SAFE_METHODS = Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS, HttpMethod.TRACE); + private static final Pattern V4_UUID = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}", Pattern.CASE_INSENSITIVE); + private final String secretKey; private final String workspaceKey; + private final String idempotencyKey; + private final RetrySettings retrySettings; public UnirestWrapper(String secretKey, String workspaceKey) { + this(secretKey, workspaceKey, null, new RetrySettings()); + } + + private UnirestWrapper(String secretKey, String workspaceKey, String idempotencyKey, RetrySettings retrySettings) { this.secretKey = secretKey; this.workspaceKey = workspaceKey; + this.idempotencyKey = idempotencyKey; + this.retrySettings = retrySettings; + } + + public UnirestWrapper withIdempotencyKey(String idempotencyKey) { + if (idempotencyKey == null || !V4_UUID.matcher(idempotencyKey).matches()) { + throw new IllegalArgumentException("Idempotency key " + idempotencyKey + " is not a v4 UUID"); + } + return new UnirestWrapper(secretKey, workspaceKey, idempotencyKey, retrySettings); } public String stringResponse(HttpRequest request) { - byte[] response = execute(request, 0); + byte[] response = binaryResponse(request); return new String(response, UTF_8); } public byte[] binaryResponse(HttpRequest request) { - return execute(request, 0); + addIdempotencyKey(request); + request.header("X-Client-Lib", "Java"); + authenticate(request, secretKey, workspaceKey); + return execute(request); } - private byte[] execute(HttpRequest request, int retryCount) { - try { - AtomicReference response = new AtomicReference<>(); - AtomicReference responseBody = new AtomicReference<>(); - request.header("X-Client-Lib", "Java"); - authenticate(request, secretKey, workspaceKey).thenConsume(r -> { - response.set((RawResponse) r); - responseBody.set(response.get().getContentAsBytes()); - }); - if (retryCount >= maxRetries || response.get().getStatus() != 429) { - return processResponse(request, response.get(), responseBody.get()); - } else { - long waitTime = (long) Math.pow(2, retryCount + 2) * 100; - sleep(waitTime); - return execute(request, ++retryCount); + private void addIdempotencyKey(HttpRequest request) { + if (!isSafe(request)) { + request.header(SeatsioClient.IDEMPOTENCY_KEY_HEADER, idempotencyKey != null ? idempotencyKey : UUID.randomUUID().toString()); + } + } + + private byte[] execute(HttpRequest request) { + for (int retryCount = 0; ; retryCount++) { + try { + AtomicReference response = new AtomicReference<>(); + AtomicReference responseBody = new AtomicReference<>(); + request.thenConsume(r -> { + response.set((RawResponse) r); + responseBody.set(response.get().getContentAsBytes()); + }); + if (!isRetryable(response.get(), responseBody.get()) || !waitBeforeRetry(retryCount)) { + return processResponse(request, response.get(), responseBody.get()); + } + } catch (UnirestException e) { + if (!isRetryable(request, e) || !waitBeforeRetry(retryCount)) { + throw new SeatsioException(e, request); + } } - } catch (UnirestException e) { - throw new SeatsioException(e, request); + } + } + + private static boolean isRetryable(RawResponse response, byte[] responseBody) { + return response.getStatus() == 429 || isIdempotencyKeyInUse(response, responseBody); + } + + public static boolean isIdempotencyKeyInUse(RawResponse response, byte[] responseBody) { + if (response.getStatus() != 409) { + return false; + } + ErrorResponse errorResponse = ErrorResponse.parseIfAny(response, responseBody); + return errorResponse != null && "IDEMPOTENCY_KEY_IN_USE".equals(errorResponse.firstErrorCode()); + } + + private boolean isRetryable(HttpRequest request, UnirestException e) { + return retrySettings.retryNonIdempotentRequests + && !isSafe(request) + && e.getCause() instanceof IOException + && !(e.getCause() instanceof ClientProtocolException); + } + + private static boolean isSafe(HttpRequest request) { + return SAFE_METHODS.contains(request.getHttpMethod()); + } + + private boolean waitBeforeRetry(int retryCount) { + if (retryCount >= retrySettings.maxRetries) { + return false; + } + try { + Thread.sleep((long) Math.pow(2, retryCount + 2) * 100); + return true; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return false; } } @@ -80,21 +147,22 @@ private T authenticate(T request, String secretKey, Stri return request; } - public static void sleep(long millis) { - try { - Thread.sleep(millis); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } + public UnirestWrapper maxRetries(int maxRetries) { + retrySettings.maxRetries = maxRetries; + return this; } - public UnirestWrapper maxRetries(int maxRetries) { - this.maxRetries = maxRetries; + public UnirestWrapper retryNonIdempotentRequests(boolean retryNonIdempotentRequests) { + retrySettings.retryNonIdempotentRequests = retryNonIdempotentRequests; return this; } + private static class RetrySettings { + volatile int maxRetries = 5; + volatile boolean retryNonIdempotentRequests = false; + } + public static void reinitializeHttpConnectionPool() { unirest.shutDown(false); } - } diff --git a/src/test/java/seatsio/ErrorResponseTest.java b/src/test/java/seatsio/ErrorResponseTest.java new file mode 100644 index 00000000..bb0766ea --- /dev/null +++ b/src/test/java/seatsio/ErrorResponseTest.java @@ -0,0 +1,44 @@ +package seatsio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import seatsio.FakeServer.Reply; +import seatsio.exceptions.RateLimitExceededException; +import seatsio.util.UnirestWrapper; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static seatsio.util.UnirestWrapper.get; + +public class ErrorResponseTest { + + @ParameterizedTest + @ValueSource(strings = {"{}", "{\"errors\": []}", "{\"errors\": null}", "null", "malformed", "[]", "{\"errors\": [null]}"}) + public void jsonErrorResponseWithoutAnErrorCodeGivesAGenericException(String body) throws Exception { + try (FakeServer server = FakeServer.replying(Reply.json(500, body))) { + SeatsioException e = assertThrows(SeatsioException.class, () -> new UnirestWrapper("secretKey", null).stringResponse(get(server.url("/")))); + + assertThat(e.getMessage()).isEqualTo("GET " + server.url("/") + " resulted in a 500 Fake response. Body: " + body); + assertThat(e.errors).isNull(); + } + } + + @Test + public void errorResponseWithoutContentTypeGivesAGenericException() throws Exception { + try (FakeServer server = FakeServer.replying(Reply.withoutContentType(500, "{}"))) { + SeatsioException e = assertThrows(SeatsioException.class, () -> new UnirestWrapper("secretKey", null).stringResponse(get(server.url("/")))); + + assertThat(e.getMessage()).endsWith("resulted in a 500 Fake response. Body: {}"); + } + } + + @Test + public void rateLimitWithoutAnErrorCodeStillGivesARateLimitExceededException() throws Exception { + try (FakeServer server = FakeServer.replying(Reply.json(429, "{}"))) { + SeatsioException e = assertThrows(RateLimitExceededException.class, () -> new UnirestWrapper("secretKey", null).maxRetries(0).stringResponse(get(server.url("/")))); + + assertThat(e.getMessage()).endsWith("resulted in a 429 Fake response. Body: {}"); + } + } +} diff --git a/src/test/java/seatsio/FakeServer.java b/src/test/java/seatsio/FakeServer.java new file mode 100644 index 00000000..a35a78ca --- /dev/null +++ b/src/test/java/seatsio/FakeServer.java @@ -0,0 +1,201 @@ +package seatsio; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.function.Consumer; +import java.util.function.Function; + +import static java.nio.charset.StandardCharsets.ISO_8859_1; +import static java.nio.charset.StandardCharsets.UTF_8; + +public class FakeServer implements AutoCloseable { + + private final ServerSocket serverSocket; + private final List replies; + private final List requests = Collections.synchronizedList(new ArrayList<>()); + private final ConcurrentLinkedQueue sockets = new ConcurrentLinkedQueue<>(); + + private FakeServer(List replies) throws IOException { + this.replies = replies; + this.serverSocket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress()); + Thread thread = new Thread(this::serve, "fake-server"); + thread.setDaemon(true); + thread.start(); + } + + public static FakeServer replying(Reply... replies) { + try { + return new FakeServer(List.of(replies)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public String url(String path) { + return "http://localhost:" + serverSocket.getLocalPort() + path; + } + + public List requests() { + return new ArrayList<>(requests); + } + + public List sentIdempotencyKeys() { + return requests().stream().map(r -> r.header(SeatsioClient.IDEMPOTENCY_KEY_HEADER)).toList(); + } + + @Override + public void close() throws IOException { + serverSocket.close(); + for (Socket socket : sockets) { + socket.close(); + } + } + + private void serve() { + while (!serverSocket.isClosed()) { + try (Socket socket = serverSocket.accept()) { + sockets.add(socket); + Request request = readRequest(socket.getInputStream()); + if (request == null) { + continue; + } + requests.add(request); + Reply reply = replies.get(Math.min(requests.size(), replies.size()) - 1); + reply.beforeReplying.accept(request); + if (reply.computedBy != null) { + reply = reply.computedBy.apply(request); + } + if (reply.status != null) { + write(socket.getOutputStream(), reply); + } + } catch (IOException e) { + // server closed, or client went away + } + } + } + + private static Request readRequest(InputStream in) throws IOException { + String requestLine = readLine(in); + if (requestLine == null || requestLine.isEmpty()) { + return null; + } + Map> headers = new HashMap<>(); + String line; + while ((line = readLine(in)) != null && !line.isEmpty()) { + int colon = line.indexOf(':'); + headers.computeIfAbsent(line.substring(0, colon).trim().toLowerCase(Locale.ROOT), k -> new ArrayList<>()).add(line.substring(colon + 1).trim()); + } + int contentLength = Integer.parseInt(headers.getOrDefault("content-length", List.of("0")).get(0)); + byte[] body = in.readNBytes(contentLength); + String[] parts = requestLine.split(" "); + return new Request(parts[0], parts[1], headers, new String(body, UTF_8)); + } + + private static String readLine(InputStream in) throws IOException { + ByteArrayOutputStream line = new ByteArrayOutputStream(); + int b; + while ((b = in.read()) != -1) { + if (b == '\n') { + return line.toString(ISO_8859_1).replaceAll("\r$", ""); + } + line.write(b); + } + return line.size() == 0 ? null : line.toString(ISO_8859_1); + } + + private static void write(OutputStream out, Reply reply) throws IOException { + byte[] body = reply.body.getBytes(UTF_8); + StringBuilder head = new StringBuilder("HTTP/1.1 " + reply.status + " Fake\r\n"); + if (reply.contentType != null) { + head.append("Content-Type: ").append(reply.contentType).append("\r\n"); + } + head.append("Content-Length: ").append(body.length).append("\r\n"); + head.append("Connection: close\r\n\r\n"); + out.write(head.toString().getBytes(ISO_8859_1)); + out.write(body); + out.flush(); + } + + public record Request(String method, String path, Map> headers, String body) { + + public String header(String name) { + List values = headers(name); + return values.isEmpty() ? null : values.get(0); + } + + public List headers(String name) { + return headers.getOrDefault(name.toLowerCase(Locale.ROOT), List.of()); + } + } + + public static class Reply { + + private final Integer status; + private final String contentType; + private final String body; + private final Consumer beforeReplying; + private final Function computedBy; + + private Reply(Integer status, String contentType, String body, Consumer beforeReplying) { + this(status, contentType, body, beforeReplying, null); + } + + private Reply(Integer status, String contentType, String body, Consumer beforeReplying, Function computedBy) { + this.status = status; + this.contentType = contentType; + this.body = body; + this.beforeReplying = beforeReplying; + this.computedBy = computedBy; + } + + public static Reply computedBy(Function computedBy) { + return new Reply(null, null, "", r -> { + }, computedBy); + } + + public static Reply of(int status, String contentType, String body) { + return new Reply(status, contentType == null || contentType.isEmpty() ? null : contentType, body, r -> { + }); + } + + public static Reply status(int status) { + return new Reply(status, "text/plain", "", r -> { + }); + } + + public static Reply json(int status, String body) { + return new Reply(status, "application/json", body, r -> { + }); + } + + public static Reply error(int status, String code) { + return json(status, "{\"errors\": [{\"code\": \"" + code + "\", \"message\": \"" + code + "\"}], \"requestId\": \"aRequestId\"}"); + } + + public static Reply withoutContentType(int status, String body) { + return new Reply(status, null, body, r -> { + }); + } + + public static Reply closeConnectionWithoutAnswering() { + return new Reply(null, null, "", r -> { + }); + } + + public Reply andBeforeReplying(Consumer action) { + return new Reply(status, contentType, body, action); + } + } +} diff --git a/src/test/java/seatsio/IdempotencyKeyTest.java b/src/test/java/seatsio/IdempotencyKeyTest.java new file mode 100644 index 00000000..4719fb74 --- /dev/null +++ b/src/test/java/seatsio/IdempotencyKeyTest.java @@ -0,0 +1,159 @@ +package seatsio; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import kong.unirest.Unirest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; +import seatsio.FakeServer.Reply; +import seatsio.util.UnirestWrapper; + +import java.util.UUID; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static seatsio.Httpbin.httpbinUrl; +import static seatsio.util.UnirestWrapper.delete; +import static seatsio.util.UnirestWrapper.get; +import static seatsio.util.UnirestWrapper.post; + +public class IdempotencyKeyTest { + + @Test + public void postsGetAnIdempotencyKey() { + String key = sentIdempotencyKey(new UnirestWrapper("secretKey", null).stringResponse(post(httpbinUrl("/anything")).body("{}"))); + + assertThat(UUID.fromString(key).version()).isEqualTo(4); + } + + @Test + public void deletesGetAnIdempotencyKey() { + String key = sentIdempotencyKey(new UnirestWrapper("secretKey", null).stringResponse(delete(httpbinUrl("/anything")))); + + assertThat(UUID.fromString(key).version()).isEqualTo(4); + } + + @Test + public void getsDoNotGetAnIdempotencyKey() { + String response = new UnirestWrapper("secretKey", null).stringResponse(get(httpbinUrl("/anything"))); + + assertThat(JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("headers").has("Idempotency-Key")).isFalse(); + } + + @Test + public void eachRequestGetsItsOwnKey() { + UnirestWrapper wrapper = new UnirestWrapper("secretKey", null); + + String first = sentIdempotencyKey(wrapper.stringResponse(post(httpbinUrl("/anything")).body("{}"))); + String second = sentIdempotencyKey(wrapper.stringResponse(post(httpbinUrl("/anything")).body("{}"))); + + assertThat(first).isNotEqualTo(second); + } + + @Test + public void failedRequestsExposeTheirIdempotencyKey() { + try { + new UnirestWrapper("secretKey", null).stringResponse(post(httpbinUrl("/status/500")).body("{}")); + fail("expected exception"); + } catch (SeatsioException e) { + assertThat(UUID.fromString(e.idempotencyKey).version()).isEqualTo(4); + } + } + + @Test + public void connectionFailuresExposeTheirIdempotencyKey() { + try { + new UnirestWrapper("secretKey", null).stringResponse(post("http://localhost:1/anything").body("{}")); + fail("expected exception"); + } catch (SeatsioException e) { + assertThat(UUID.fromString(e.idempotencyKey).version()).isEqualTo(4); + } + } + + @Test + public void aClientCanRetryWithTheSameIdempotencyKey() { + String key = UUID.randomUUID().toString(); + UnirestWrapper wrapper = new UnirestWrapper("secretKey", null).withIdempotencyKey(key); + + assertThat(sentIdempotencyKey(wrapper.stringResponse(post(httpbinUrl("/anything")).body("{}")))).isEqualTo(key); + assertThat(sentIdempotencyKey(wrapper.stringResponse(post(httpbinUrl("/anything")).body("{}")))).isEqualTo(key); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = {"", "notAUuid", "f81d4fae-7dec-11d0-a765-00a0c91e6bf6", "00000000-0000-0000-0000-000000000000", "1-1-1-1-1"}) + public void suppliedKeyMustBeAV4Uuid(String key) { + assertThrows(IllegalArgumentException.class, () -> new SeatsioClient("secretKey", null, "http://localhost:1").withIdempotencyKey(key)); + } + + @Test + public void suppliedKeyIsCaseInsensitive() { + String key = UUID.randomUUID().toString().toUpperCase(); + try (FakeServer server = FakeServer.replying(Reply.status(204))) { + new UnirestWrapper("secretKey", null).withIdempotencyKey(key).stringResponse(post(server.url("/")).body("{}")); + + assertThat(server.sentIdempotencyKeys()).containsExactly(key); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void putsAndPatchesGetAnIdempotencyKey() throws Exception { + try (FakeServer server = FakeServer.replying(Reply.status(204))) { + new UnirestWrapper("secretKey", null).stringResponse(Unirest.put(server.url("/")).body("{}")); + new UnirestWrapper("secretKey", null).stringResponse(Unirest.patch(server.url("/")).body("{}")); + + assertThat(server.sentIdempotencyKeys()).allSatisfy(key -> assertThat(UUID.fromString(key).version()).isEqualTo(4)); + } + } + + @ParameterizedTest + @MethodSource("errorResponses") + public void errorResponsesExposeTheKey(Reply reply) throws Exception { + try (FakeServer server = FakeServer.replying(reply)) { + SeatsioException e = assertThrows(SeatsioException.class, () -> new UnirestWrapper("secretKey", null).stringResponse(post(server.url("/")).body("{}"))); + + assertThat(e.idempotencyKey).isEqualTo(server.sentIdempotencyKeys().get(0)); + } + } + + @ParameterizedTest + @MethodSource("errorResponses") + public void errorResponsesOfGetsHaveNoKey(Reply reply) throws Exception { + try (FakeServer server = FakeServer.replying(reply)) { + SeatsioException e = assertThrows(SeatsioException.class, () -> new UnirestWrapper("secretKey", null).stringResponse(get(server.url("/")))); + + assertThat(e.idempotencyKey).isNull(); + } + } + + static Stream errorResponses() { + return Stream.of( + Reply.error(400, "EVENT_NOT_FOUND"), + Reply.json(500, "{}"), + Reply.json(500, "{\"errors\": []}"), + Reply.json(500, "malformed"), + Reply.status(500), + Reply.withoutContentType(500, "{}") + ); + } + + @Test + public void connectionFailuresOfGetsHaveNoKey() { + SeatsioException e = assertThrows(SeatsioException.class, () -> new UnirestWrapper("secretKey", null).stringResponse(get("http://localhost:1/anything"))); + + assertThat(e.idempotencyKey).isNull(); + } + + private static String sentIdempotencyKey(String httpbinResponse) { + JsonElement header = JsonParser.parseString(httpbinResponse).getAsJsonObject().getAsJsonObject("headers").get("Idempotency-Key"); + // httpbingo returns header values as arrays, httpbin as strings + return header.isJsonArray() ? header.getAsJsonArray().get(0).getAsString() : header.getAsString(); + } +} diff --git a/src/test/java/seatsio/IdempotentRetryTest.java b/src/test/java/seatsio/IdempotentRetryTest.java new file mode 100644 index 00000000..a353f011 --- /dev/null +++ b/src/test/java/seatsio/IdempotentRetryTest.java @@ -0,0 +1,322 @@ +package seatsio; + +import kong.unirest.HttpRequest; +import kong.unirest.Unirest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import seatsio.exceptions.RateLimitExceededException; +import seatsio.util.UnirestWrapper; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static seatsio.FakeServer.Reply.closeConnectionWithoutAnswering; +import static seatsio.FakeServer.Reply.error; +import static seatsio.FakeServer.Reply.json; +import static seatsio.FakeServer.Reply.status; + +public class IdempotentRetryTest { + + private static final int APACHE_DEFAULT_ATTEMPTS = 1 + 3; + + private FakeServer server; + + @AfterEach + public void stopServer() throws Exception { + Thread.interrupted(); + if (server != null) { + server.close(); + } + } + + @ParameterizedTest + @ValueSource(strings = {"POST", "PUT", "DELETE", "PATCH"}) + public void writesAreNotRetriedByDefaultWhenTheConnectionFails(String method) { + server = FakeServer.replying(closeConnectionWithoutAnswering(), status(200)); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().stringResponse(request(method))); + + assertThat(server.requests()).hasSize(1); + assertThat(e.idempotencyKey).isEqualTo(server.sentIdempotencyKeys().get(0)); + } + + @ParameterizedTest + @ValueSource(strings = {"POST", "PUT", "DELETE", "PATCH"}) + public void writesAreRetriedWithTheSameKeyWhenEnabled(String method) { + server = FakeServer.replying(closeConnectionWithoutAnswering(), status(200)); + + wrapper().retryNonIdempotentRequests(true).stringResponse(request(method)); + + assertThat(server.sentIdempotencyKeys()).hasSize(2).doesNotContainNull(); + assertThat(server.sentIdempotencyKeys().get(1)).isEqualTo(server.sentIdempotencyKeys().get(0)); + } + + @Test + public void writesAreNotRetriedAfterRetriesAreDisabledAgain() { + server = FakeServer.replying(closeConnectionWithoutAnswering(), status(200)); + + assertThrows(SeatsioException.class, () -> wrapper().retryNonIdempotentRequests(true).retryNonIdempotentRequests(false).stringResponse(request("POST"))); + + assertThat(server.requests()).hasSize(1); + } + + @Test + public void connectionFailureRetriesStopAfterMaxRetries() { + server = FakeServer.replying(closeConnectionWithoutAnswering()); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().maxRetries(2).retryNonIdempotentRequests(true).stringResponse(request("POST"))); + + assertThat(server.requests()).hasSize(3); + assertThat(server.sentIdempotencyKeys()).containsOnly(e.idempotencyKey); + } + + @Test + public void getsAreRetriedByTheHttpClientOnlyNotOnTopOfIt() { + server = FakeServer.replying(closeConnectionWithoutAnswering()); + + assertThrows(SeatsioException.class, () -> wrapper().retryNonIdempotentRequests(true).stringResponse(request("GET"))); + + assertThat(server.requests()).hasSize(APACHE_DEFAULT_ATTEMPTS); + } + + @Test + public void connectionRefusedIsRetriedWhenEnabled() { + server = FakeServer.replying(status(200)); + String url = server.url("/"); + closeServer(); + + long start = System.currentTimeMillis(); + assertThrows(SeatsioException.class, () -> wrapper().maxRetries(1).retryNonIdempotentRequests(true).stringResponse(Unirest.post(url).body("{}"))); + + assertThat(System.currentTimeMillis() - start).isGreaterThanOrEqualTo(400); + } + + @Test + public void requestConstructionErrorsAreNotRetried() { + long start = System.currentTimeMillis(); + + assertThrows(SeatsioException.class, () -> wrapper().retryNonIdempotentRequests(true).stringResponse(Unirest.post("unknownProtocol:///x").body("{}"))); + + assertThat(System.currentTimeMillis() - start).isLessThan(400); + } + + @Test + public void writesAreRetriedOn429WithTheSameKey() { + server = FakeServer.replying(error(429, "RATE_LIMIT_EXCEEDED"), status(200)); + + wrapper().stringResponse(request("POST")); + + assertThat(server.sentIdempotencyKeys()).hasSize(2); + assertThat(server.sentIdempotencyKeys().get(1)).isEqualTo(server.sentIdempotencyKeys().get(0)); + } + + @Test + public void writesAreRetriedOnKeyInUseWithTheSameKey() { + server = FakeServer.replying(error(409, "IDEMPOTENCY_KEY_IN_USE"), status(200)); + + wrapper().stringResponse(request("POST")); + + assertThat(server.sentIdempotencyKeys()).hasSize(2); + assertThat(server.sentIdempotencyKeys().get(1)).isEqualTo(server.sentIdempotencyKeys().get(0)); + } + + @Test + public void retriesOn429AndKeyInUseAlsoHappenWhenWriteRetriesAreEnabled() { + server = FakeServer.replying(error(429, "RATE_LIMIT_EXCEEDED"), error(409, "IDEMPOTENCY_KEY_IN_USE"), status(200)); + + wrapper().retryNonIdempotentRequests(true).stringResponse(request("POST")); + + assertThat(server.requests()).hasSize(3); + } + + @Test + public void callerKeySurvivesARetry() { + String key = UUID.randomUUID().toString(); + server = FakeServer.replying(error(409, "IDEMPOTENCY_KEY_IN_USE"), status(200)); + + wrapper().withIdempotencyKey(key).stringResponse(request("POST")); + + assertThat(server.sentIdempotencyKeys()).containsExactly(key, key); + } + + @Test + public void keyInUseErrorIsThrownWhenRetriesRunOut() { + server = FakeServer.replying(error(409, "IDEMPOTENCY_KEY_IN_USE")); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().maxRetries(2).stringResponse(request("POST"))); + + assertThat(server.requests()).hasSize(3); + assertThat(e.errors.get(0).getCode()).isEqualTo("IDEMPOTENCY_KEY_IN_USE"); + assertThat(server.sentIdempotencyKeys()).containsOnly(e.idempotencyKey); + } + + @ParameterizedTest + @ValueSource(strings = { + "{\"errors\": [{\"code\": \"SOME_OTHER_CONFLICT\", \"message\": \"conflict\"}]}", + "{\"errors\": []}", + "{}", + "not json", + "" + }) + public void otherConflictsAreNotRetried(String body) { + server = FakeServer.replying(json(409, body), status(200)); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().stringResponse(request("POST"))); + + assertThat(server.requests()).hasSize(1); + assertThat(e.idempotencyKey).isNotNull(); + } + + @Test + public void doesNotRetryWhenTheThreadIsInterrupted() { + Thread client = Thread.currentThread(); + server = FakeServer.replying(error(429, "RATE_LIMIT_EXCEEDED").andBeforeReplying(r -> client.interrupt()), status(200)); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().stringResponse(request("POST"))); + + assertThat(server.requests()).hasSize(1); + assertThat(e.idempotencyKey).isNotNull(); + assertThat(Thread.interrupted()).isTrue(); + } + + @Test + public void doesNotRetryAConnectionFailureWhenTheThreadIsInterrupted() { + Thread client = Thread.currentThread(); + server = FakeServer.replying(closeConnectionWithoutAnswering().andBeforeReplying(r -> client.interrupt()), status(200)); + + assertThrows(SeatsioException.class, () -> wrapper().retryNonIdempotentRequests(true).stringResponse(request("POST"))); + + assertThat(server.requests()).hasSize(1); + assertThat(Thread.interrupted()).isTrue(); + } + + @Test + public void retrySettingsAreSharedWithCopiesThatHaveAKey() { + server = FakeServer.replying(closeConnectionWithoutAnswering(), status(200)); + UnirestWrapper wrapper = wrapper(); + UnirestWrapper copy = wrapper.withIdempotencyKey(UUID.randomUUID().toString()); + + wrapper.retryNonIdempotentRequests(true); + copy.stringResponse(request("POST")); + + assertThat(server.requests()).hasSize(2); + } + + @Test + public void headersAreSentOncePerAttempt() { + server = FakeServer.replying(error(429, "RATE_LIMIT_EXCEEDED"), status(200)); + + wrapper().stringResponse(request("POST")); + + assertThat(server.requests()).hasSize(2); + for (FakeServer.Request request : server.requests()) { + assertThat(request.headers("X-Client-Lib")).hasSize(1); + assertThat(request.headers("Authorization")).hasSize(1); + assertThat(request.headers(SeatsioClient.IDEMPOTENCY_KEY_HEADER)).hasSize(1); + } + } + + @Test + public void maxRetriesIsSharedWithClientsThatHaveAKey() { + server = FakeServer.replying(error(429, "RATE_LIMIT_EXCEEDED")); + SeatsioClient client = new SeatsioClient("secretKey", null, server.url("")); + SeatsioClient copy = client.withIdempotencyKey(UUID.randomUUID().toString()); + + client.maxRetries(2); + assertThrows(RateLimitExceededException.class, () -> copy.events.update("anEvent", new seatsio.events.UpdateEventParams().withKey("anotherEvent"))); + + assertThat(server.requests()).hasSize(3); + } + + @Test + public void outcomeIsUnknownWhenTheConnectionFails() { + server = FakeServer.replying(closeConnectionWithoutAnswering()); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().stringResponse(request("POST"))); + + assertThat(e.outcomeIsUnknown()).isTrue(); + } + + @ParameterizedTest + @ValueSource(ints = {500, 502, 503}) + public void outcomeIsUnknownAfterA5xx(int status) { + server = FakeServer.replying(error(status, "SOME_ERROR")); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().stringResponse(request("POST"))); + + assertThat(e.outcomeIsUnknown()).isTrue(); + } + + @Test + public void outcomeIsUnknownWhenTheKeyIsStillInUseAfterAllRetries() { + server = FakeServer.replying(error(409, "IDEMPOTENCY_KEY_IN_USE")); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().maxRetries(0).stringResponse(request("POST"))); + + assertThat(e.outcomeIsUnknown()).isTrue(); + } + + @ParameterizedTest + @ValueSource(ints = {400, 404, 409, 422}) + public void outcomeIsKnownAfterA4xx(int status) { + server = FakeServer.replying(error(status, "SOME_ERROR")); + + SeatsioException e = assertThrows(SeatsioException.class, () -> wrapper().stringResponse(request("POST"))); + + assertThat(e.outcomeIsUnknown()).isFalse(); + } + + @Test + public void theLastScriptedReplyIsRepeatedOnceTheScriptRunsOut() { + server = FakeServer.replying(error(429, "RATE_LIMIT_EXCEEDED")); + + assertThrows(SeatsioException.class, () -> wrapper().maxRetries(2).stringResponse(request("POST"))); + + assertThat(server.requests()).hasSize(3); + } + + @Test + public void anOperationWithUnknownOutcomeCanBeRetriedWithTheKeyOfTheException() { + server = FakeServer.replying(error(500, "SOME_ERROR"), json(200, "{}")); + SeatsioClient client = new SeatsioClient("secretKey", null, server.url("")); + String key = assertThrows(SeatsioException.class, + () -> client.events.update("anEvent", new seatsio.events.UpdateEventParams().withKey("anotherEvent"))).idempotencyKey; + + client.withIdempotencyKey(key).events.update("anEvent", new seatsio.events.UpdateEventParams().withKey("anotherEvent")); + + assertThat(server.sentIdempotencyKeys()).containsExactly(key, key); + } + + @Test + public void seatsioClientCanEnableRetriesOfWrites() { + server = FakeServer.replying(closeConnectionWithoutAnswering(), json(200, "{}")); + SeatsioClient client = new SeatsioClient("secretKey", null, server.url("")).retryNonIdempotentRequests(true); + + client.withIdempotencyKey(UUID.randomUUID().toString()).events.update("anEvent", new seatsio.events.UpdateEventParams().withKey("anotherEvent")); + + assertThat(server.requests()).hasSize(2); + } + + private static UnirestWrapper wrapper() { + return new UnirestWrapper("secretKey", null); + } + + private HttpRequest request(String method) { + if (method.equals("GET")) { + return Unirest.get(server.url("/")); + } + return Unirest.request(method, server.url("/")).body("{}"); + } + + private void closeServer() { + try { + server.close(); + server = null; + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/test/java/seatsio/SeatsioClientTest.java b/src/test/java/seatsio/SeatsioClientTest.java index 48c1e3e5..b249c8a0 100644 --- a/src/test/java/seatsio/SeatsioClientTest.java +++ b/src/test/java/seatsio/SeatsioClientTest.java @@ -43,9 +43,22 @@ protected static String apiSecret() { return System.getenv().getOrDefault("CORE_V2_API_SECRET", "superSecretSystemApi"); } + protected void useTestCompanyWithIdempotencyEnabled() { + TestCompany testCompany = createTestCompany("{\"idempotencyEnabled\": true}"); + user = testCompany.admin; + workspace = testCompany.workspace; + client = seatsioClient(user.secretKey); + } + private TestCompany createTestCompany() throws UnirestException { + return createTestCompany("{}"); + } + + private TestCompany createTestCompany(String params) throws UnirestException { HttpResponse response = post(apiUrl() + "/system/private/create-test-company") .basicAuth(apiSecret(), "") + .header("Content-Type", "application/json") + .body(params) .asString(); if(response.getStatus() >= 400) { throw new SeatsioException("Creating test company failed wide code " + response.getStatus()); diff --git a/src/test/java/seatsio/events/IdempotencyTest.java b/src/test/java/seatsio/events/IdempotencyTest.java new file mode 100644 index 00000000..e7f04c93 --- /dev/null +++ b/src/test/java/seatsio/events/IdempotencyTest.java @@ -0,0 +1,109 @@ +package seatsio.events; + +import kong.unirest.HttpRequestWithBody; +import kong.unirest.HttpResponse; +import kong.unirest.Unirest; +import org.junit.jupiter.api.Test; +import seatsio.FakeServer; +import seatsio.FakeServer.Reply; +import seatsio.SeatsioClient; +import seatsio.SeatsioClientTest; +import seatsio.SeatsioException; + +import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class IdempotencyTest extends SeatsioClientTest { + + @Test + public void sameKeyReplaysTheResponse() { + useTestCompanyWithIdempotencyEnabled(); + String chartKey = createTestChart(); + SeatsioClient clientWithKey = client.withIdempotencyKey(UUID.randomUUID().toString()); + + Event first = clientWithKey.events.create(chartKey); + Event second = clientWithKey.events.create(chartKey); + + assertThat(second.id()).isEqualTo(first.id()); + assertThat(second.key()).isEqualTo(first.key()); + } + + @Test + public void sameKeyWithADifferentRequestIsRejected() { + useTestCompanyWithIdempotencyEnabled(); + String chartKey = createTestChart(); + SeatsioClient clientWithKey = client.withIdempotencyKey(UUID.randomUUID().toString()); + clientWithKey.events.create(chartKey); + + SeatsioException e = assertThrows(SeatsioException.class, () -> clientWithKey.events.create(chartKey, new CreateEventParams().withKey("anotherEvent"))); + + assertThat(e.errors.get(0).getCode()).isEqualTo("IDEMPOTENCY_KEY_REUSED"); + } + + @Test + public void keyIsIgnoredWhenIdempotencyIsNotEnabledForTheCompany() { + String chartKey = createTestChart(); + SeatsioClient clientWithKey = client.withIdempotencyKey(UUID.randomUUID().toString()); + + Event first = clientWithKey.events.create(chartKey); + Event second = clientWithKey.events.create(chartKey); + + assertThat(second.id()).isNotEqualTo(first.id()); + } + + @Test + public void suppliedKeyIsAvailableOnAFailedRequest() { + useTestCompanyWithIdempotencyEnabled(); + String key = UUID.randomUUID().toString(); + + SeatsioException e = assertThrows(SeatsioException.class, () -> client.withIdempotencyKey(key).events.create("unknownChart")); + + assertThat(e.idempotencyKey).isEqualTo(key); + } + + @Test + public void generatedKeyIsAvailableOnAFailedRequest() { + useTestCompanyWithIdempotencyEnabled(); + + SeatsioException e = assertThrows(SeatsioException.class, () -> client.events.create("unknownChart")); + + assertThat(UUID.fromString(e.idempotencyKey).version()).isEqualTo(4); + } + + @Test + public void retryAfterALostResponseCreatesOneEvent() throws Exception { + useTestCompanyWithIdempotencyEnabled(); + String chartKey = createTestChart(); + try (FakeServer proxy = proxyThatForwardsButDropsTheFirstResponse()) { + SeatsioClient clientViaProxy = new SeatsioClient(user.secretKey, null, proxy.url("")).retryNonIdempotentRequests(true); + + Event event = clientViaProxy.events.create(chartKey); + + assertThat(client.events.listAll()).extracting(Event::id).containsExactly(event.id()); + } + } + + private static FakeServer proxyThatForwardsButDropsTheFirstResponse() { + AtomicInteger requests = new AtomicInteger(); + return FakeServer.replying(Reply.computedBy(request -> { + HttpResponse response = forward(request); + if (requests.incrementAndGet() == 1) { + return Reply.closeConnectionWithoutAnswering(); + } + return Reply.of(response.getStatus(), response.getHeaders().getFirst("Content-Type"), response.getBody()); + })); + } + + private static HttpResponse forward(FakeServer.Request request) { + HttpRequestWithBody forwarded = Unirest.request(request.method(), apiUrl() + request.path()); + request.headers().forEach((name, values) -> { + if (!name.equals("host") && !name.equals("content-length")) { + values.forEach(value -> forwarded.header(name, value)); + } + }); + return forwarded.body(request.body()).asString(); + } +}