Skip to content

Commit 6906845

Browse files
committed
Update RetryHttpHandlerTest.cs
1 parent 72a5fd1 commit 6906845

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

csharp/test/Drivers/Apache/Spark/RetryHttpHandlerTest.cs

+36-36
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ public async Task RetryAfterHandlerProcesses503Response()
4848

4949
// Create the RetryHttpHandler with retry enabled and a 5-second timeout
5050
var retryHandler = new RetryHttpHandler(mockHandler, true, 5);
51-
51+
5252
// Create an HttpClient with our handler
5353
var httpClient = new HttpClient(retryHandler);
54-
54+
5555
// Set the mock handler to return a success response after the first retry
5656
mockHandler.SetResponseAfterRetryCount(1, new HttpResponseMessage(HttpStatusCode.OK)
5757
{
5858
Content = new StringContent("Success")
5959
});
60-
60+
6161
// Send a request
6262
var response = await httpClient.GetAsync("http://test.com");
63-
63+
6464
// Verify the response is OK
6565
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
6666
Assert.Equal("Success", await response.Content.ReadAsStringAsync());
6767
Assert.Equal(2, mockHandler.RequestCount); // Initial request + 1 retry
6868
}
69-
69+
7070
/// <summary>
7171
/// Tests that the RetryHttpHandler throws an exception when the retry timeout is exceeded.
7272
/// </summary>
@@ -83,22 +83,22 @@ public async Task RetryAfterHandlerThrowsWhenTimeoutExceeded()
8383

8484
// Create the RetryHttpHandler with retry enabled and a 1-second timeout
8585
var retryHandler = new RetryHttpHandler(mockHandler, true, 1);
86-
86+
8787
// Create an HttpClient with our handler
8888
var httpClient = new HttpClient(retryHandler);
89-
89+
9090
// Send a request and expect an AdbcException
91-
var exception = await Assert.ThrowsAsync<AdbcException>(async () =>
91+
var exception = await Assert.ThrowsAsync<AdbcException>(async () =>
9292
await httpClient.GetAsync("http://test.com"));
93-
93+
9494
// Verify the exception has the correct SQL state in the message
9595
Assert.Contains("[SQLState: 08001]", exception.Message);
9696
Assert.Equal(AdbcStatusCode.IOError, exception.Status);
97-
97+
9898
// Verify we only tried once (since the Retry-After value of 2 exceeds our timeout of 1)
9999
Assert.Equal(1, mockHandler.RequestCount);
100100
}
101-
101+
102102
/// <summary>
103103
/// Tests that the RetryHttpHandler doesn't retry when retry is disabled.
104104
/// </summary>
@@ -115,19 +115,19 @@ public async Task RetryAfterHandlerDoesNotRetryWhenDisabled()
115115

116116
// Create the RetryHttpHandler with retry disabled
117117
var retryHandler = new RetryHttpHandler(mockHandler, false, 5);
118-
118+
119119
// Create an HttpClient with our handler
120120
var httpClient = new HttpClient(retryHandler);
121-
121+
122122
// Send a request
123123
var response = await httpClient.GetAsync("http://test.com");
124-
124+
125125
// Verify the response is 503
126126
Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
127127
Assert.Equal("Service Unavailable", await response.Content.ReadAsStringAsync());
128128
Assert.Equal(1, mockHandler.RequestCount); // Only the initial request, no retries
129129
}
130-
130+
131131
/// <summary>
132132
/// Tests that the RetryHttpHandler handles non-503 responses correctly.
133133
/// </summary>
@@ -143,19 +143,19 @@ public async Task RetryAfterHandlerHandlesNon503Response()
143143

144144
// Create the RetryHttpHandler with retry enabled
145145
var retryHandler = new RetryHttpHandler(mockHandler, true, 5);
146-
146+
147147
// Create an HttpClient with our handler
148148
var httpClient = new HttpClient(retryHandler);
149-
149+
150150
// Send a request
151151
var response = await httpClient.GetAsync("http://test.com");
152-
152+
153153
// Verify the response is 404
154154
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
155155
Assert.Equal("Not Found", await response.Content.ReadAsStringAsync());
156156
Assert.Equal(1, mockHandler.RequestCount); // Only the initial request, no retries
157157
}
158-
158+
159159
/// <summary>
160160
/// Tests that the RetryHttpHandler handles 503 responses without Retry-After headers correctly.
161161
/// </summary>
@@ -171,19 +171,19 @@ public async Task RetryAfterHandlerHandles503WithoutRetryAfterHeader()
171171

172172
// Create the RetryHttpHandler with retry enabled
173173
var retryHandler = new RetryHttpHandler(mockHandler, true, 5);
174-
174+
175175
// Create an HttpClient with our handler
176176
var httpClient = new HttpClient(retryHandler);
177-
177+
178178
// Send a request
179179
var response = await httpClient.GetAsync("http://test.com");
180-
180+
181181
// Verify the response is 503
182182
Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
183183
Assert.Equal("Service Unavailable", await response.Content.ReadAsStringAsync());
184184
Assert.Equal(1, mockHandler.RequestCount); // Only the initial request, no retries
185185
}
186-
186+
187187
/// <summary>
188188
/// Tests that the RetryHttpHandler handles invalid Retry-After headers correctly.
189189
/// </summary>
@@ -207,19 +207,19 @@ public async Task RetryAfterHandlerHandlesInvalidRetryAfterHeader()
207207

208208
// Create the RetryHttpHandler with retry enabled
209209
var retryHandler = new RetryHttpHandler(mockHandler, true, 5);
210-
210+
211211
// Create an HttpClient with our handler
212212
var httpClient = new HttpClient(retryHandler);
213-
213+
214214
// Send a request
215215
response = await httpClient.GetAsync("http://test.com");
216-
216+
217217
// Verify the response is 503
218218
Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
219219
Assert.Equal("Service Unavailable", await response.Content.ReadAsStringAsync());
220220
Assert.Equal(1, mockHandler.RequestCount); // Only the initial request, no retries
221221
}
222-
222+
223223
/// <summary>
224224
/// Mock HttpMessageHandler for testing the RetryHttpHandler.
225225
/// </summary>
@@ -228,38 +228,38 @@ private class MockHttpMessageHandler : HttpMessageHandler
228228
private readonly HttpResponseMessage _defaultResponse;
229229
private HttpResponseMessage? _responseAfterRetryCount;
230230
private int _retryCountForResponse;
231-
231+
232232
public int RequestCount { get; private set; }
233-
233+
234234
public MockHttpMessageHandler(HttpResponseMessage defaultResponse)
235235
{
236236
_defaultResponse = defaultResponse;
237237
}
238-
238+
239239
public void SetResponseAfterRetryCount(int retryCount, HttpResponseMessage response)
240240
{
241241
_retryCountForResponse = retryCount;
242242
_responseAfterRetryCount = response;
243243
}
244-
244+
245245
protected override Task<HttpResponseMessage> SendAsync(
246246
HttpRequestMessage request,
247247
CancellationToken cancellationToken)
248248
{
249249
RequestCount++;
250-
250+
251251
if (_responseAfterRetryCount != null && RequestCount > _retryCountForResponse)
252252
{
253253
return Task.FromResult(_responseAfterRetryCount);
254254
}
255-
255+
256256
// Create a new response instance to avoid modifying the original
257257
var response = new HttpResponseMessage
258258
{
259259
StatusCode = _defaultResponse.StatusCode,
260260
Content = _defaultResponse.Content
261261
};
262-
262+
263263
// Copy headers only if they exist
264264
if (_defaultResponse.Headers.Contains("Retry-After"))
265265
{
@@ -268,9 +268,9 @@ protected override Task<HttpResponseMessage> SendAsync(
268268
response.Headers.Add("Retry-After", value);
269269
}
270270
}
271-
271+
272272
return Task.FromResult(response);
273273
}
274274
}
275275
}
276-
}
276+
}

0 commit comments

Comments
 (0)