|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | + */ |
| 4 | +package com.marklogic.client.impl.okhttp; |
| 5 | + |
| 6 | +import com.marklogic.client.DatabaseClient; |
| 7 | +import com.marklogic.client.DatabaseClientFactory; |
| 8 | +import com.marklogic.client.FailedRequestException; |
| 9 | +import com.marklogic.client.extra.okhttpclient.OkHttpClientConfigurator; |
| 10 | +import com.marklogic.client.test.Common; |
| 11 | +import okhttp3.Interceptor; |
| 12 | +import okhttp3.Protocol; |
| 13 | +import okhttp3.Request; |
| 14 | +import okhttp3.Response; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.concurrent.atomic.AtomicInteger; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tests the retry logic in OkHttpServices using a custom interceptor that simulates server errors. This is not a |
| 26 | + * well-documented feature but users are currently able to discover it via the codebase. |
| 27 | + */ |
| 28 | +class RetryOn50XResponseTest { |
| 29 | + |
| 30 | + /** |
| 31 | + * Custom interceptor that returns 502 Bad Gateway responses and counts how many times it's invoked. |
| 32 | + */ |
| 33 | + private static class BadGatewayInterceptor implements Interceptor { |
| 34 | + |
| 35 | + private final AtomicInteger invocationCount = new AtomicInteger(0); |
| 36 | + private final int failureCount; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param failureCount Number of times to return 502 before allowing the request through |
| 40 | + */ |
| 41 | + public BadGatewayInterceptor(int failureCount) { |
| 42 | + this.failureCount = failureCount; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Response intercept(Chain chain) throws IOException { |
| 47 | + int count = invocationCount.incrementAndGet(); |
| 48 | + Request request = chain.request(); |
| 49 | + |
| 50 | + // Fail the first N requests |
| 51 | + if (count <= failureCount) { |
| 52 | + return new Response.Builder() |
| 53 | + .request(request) |
| 54 | + .protocol(Protocol.HTTP_1_1) |
| 55 | + .code(502) |
| 56 | + .message("Bad Gateway") |
| 57 | + .body(okhttp3.ResponseBody.create("Simulated 502 error", null)) |
| 58 | + .build(); |
| 59 | + } |
| 60 | + |
| 61 | + // After N failures, let the request through |
| 62 | + return chain.proceed(request); |
| 63 | + } |
| 64 | + |
| 65 | + public int getInvocationCount() { |
| 66 | + return invocationCount.get(); |
| 67 | + } |
| 68 | + |
| 69 | + public void reset() { |
| 70 | + invocationCount.set(0); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @BeforeEach |
| 75 | + void setUp() { |
| 76 | + // Configure very short retry delays for testing |
| 77 | + System.setProperty("com.marklogic.client.retry.maxDelay", "3"); |
| 78 | + System.setProperty("com.marklogic.client.retry.minRetry", "2"); |
| 79 | + |
| 80 | + // "Touch" the Common class to trigger the static block that removes existing configurators on |
| 81 | + // DatabaseClientFactory. |
| 82 | + Common.newServerPayload(); |
| 83 | + } |
| 84 | + |
| 85 | + @AfterEach |
| 86 | + void tearDown() { |
| 87 | + DatabaseClientFactory.removeConfigurators(); |
| 88 | + System.clearProperty("com.marklogic.client.retry.maxDelay"); |
| 89 | + System.clearProperty("com.marklogic.client.retry.minRetry"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testRetryWith502Responses() { |
| 94 | + // Create an interceptor that will fail 2 times, then succeed |
| 95 | + BadGatewayInterceptor interceptor = new BadGatewayInterceptor(2); |
| 96 | + |
| 97 | + DatabaseClientFactory.addConfigurator((OkHttpClientConfigurator) builder -> |
| 98 | + builder.addInterceptor(interceptor)); |
| 99 | + |
| 100 | + try (DatabaseClient client = Common.newClient()) { |
| 101 | + client.checkConnection(); |
| 102 | + assertEquals(3, interceptor.getInvocationCount(), |
| 103 | + "Expected 3 invocations: 2 failures followed by 1 success"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testRetryExceedsMaxAttempts() { |
| 109 | + // Create an interceptor that will fail 10 times (more than minRetry) |
| 110 | + BadGatewayInterceptor interceptor = new BadGatewayInterceptor(10); |
| 111 | + |
| 112 | + DatabaseClientFactory.addConfigurator((OkHttpClientConfigurator) builder -> |
| 113 | + builder.addInterceptor(interceptor)); |
| 114 | + |
| 115 | + try (DatabaseClient client = Common.newClient()) { |
| 116 | + assertThrows(FailedRequestException.class, () -> { |
| 117 | + client.checkConnection(); |
| 118 | + }, "Expected FailedRequestException after exhausting retries"); |
| 119 | + |
| 120 | + assertTrue(interceptor.getInvocationCount() >= 3, |
| 121 | + "Expected at least 3 retry attempts, but got " + interceptor.getInvocationCount()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testRetryCountIncreases() { |
| 127 | + // Test that retry attempts increase as expected |
| 128 | + BadGatewayInterceptor interceptor = new BadGatewayInterceptor(1); |
| 129 | + |
| 130 | + DatabaseClientFactory.addConfigurator((OkHttpClientConfigurator) builder -> |
| 131 | + builder.addInterceptor(interceptor)); |
| 132 | + |
| 133 | + try (DatabaseClient client = Common.newClient()) { |
| 134 | + // First request: fails once, then succeeds |
| 135 | + client.checkConnection(); |
| 136 | + assertEquals(2, interceptor.getInvocationCount(), "Expected 2 invocations for first request"); |
| 137 | + |
| 138 | + // Reset and try again |
| 139 | + interceptor.reset(); |
| 140 | + client.checkConnection(); |
| 141 | + assertEquals(2, interceptor.getInvocationCount(), "Expected 2 invocations for second request"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void testNoRetryOnSuccessfulRequest() { |
| 147 | + // Interceptor that never fails |
| 148 | + BadGatewayInterceptor interceptor = new BadGatewayInterceptor(0); |
| 149 | + |
| 150 | + DatabaseClientFactory.addConfigurator((OkHttpClientConfigurator) builder -> |
| 151 | + builder.addInterceptor(interceptor)); |
| 152 | + |
| 153 | + try (DatabaseClient client = Common.newClient()) { |
| 154 | + client.checkConnection(); |
| 155 | + assertEquals(1, interceptor.getInvocationCount(), |
| 156 | + "Expected exactly 1 invocation when request succeeds immediately"); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments