-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathRetryHandlerTests.cs
More file actions
445 lines (387 loc) · 18.9 KB
/
RetryHandlerTests.cs
File metadata and controls
445 lines (387 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using global::Azure;
using Microsoft.Azure.Cosmos.Handlers;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Documents;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
[TestClass]
public class RetryHandlerTests
{
private static readonly Uri TestUri = new Uri("https://dummy.documents.azure.com:443/dbs");
[TestMethod]
public async Task ValidateQueryPlanDoesNotThrowExceptionForOverlappingRanges()
{
await this.ValidateOverlappingRangesBehaviorAsync(
operationType: OperationType.QueryPlan,
shouldThrowGoneException: false);
}
[TestMethod]
public async Task ValidateQueryThrowsGoneExceptionForOverlappingRanges()
{
await this.ValidateOverlappingRangesBehaviorAsync(
operationType: OperationType.Query,
shouldThrowGoneException: true);
}
private async Task ValidateOverlappingRangesBehaviorAsync(
OperationType operationType,
bool shouldThrowGoneException)
{
// Create overlapping ranges for the test
List<PartitionKeyRange> overlappingRanges = new List<PartitionKeyRange>
{
new PartitionKeyRange { Id = "0", MinInclusive = "0D4DC2CD8F49C65A8E0C5306B61B4343", MaxExclusive = "0DCEB8CE51C6BFE84F4BD9409F69B9BB2164DEBD78C50C850E0C1E3E3F0579ED" },
new PartitionKeyRange { Id = "1", MinInclusive = "0DCEB8CE51C6BFE84F4BD9409F69B9BB2164DEBD78C50C850E0C1E3E3F0579ED", MaxExclusive = "1080F600C27CF98DC13F8639E94E7676" }
};
// Create a custom document client with our TestPartitionKeyRangeCache
var testPartitionKeyRangeCache = new TestPartitionKeyRangeCache(overlappingRanges);
var customDocClient = new CustomMockDocumentClient(testPartitionKeyRangeCache);
// Create CosmosClient with our custom document client
using CosmosClient client = new CosmosClient(
"https://localhost:8081",
MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey,
new CosmosClientOptions(),
customDocClient);
// Create mock container
Mock<ContainerInternal> containerMock = MockCosmosUtil.CreateMockContainer("testDb", "testColl");
// Setup container properties
ContainerProperties containerProps = new ContainerProperties("testColl", "/pk");
var resourceIdProperty = typeof(ContainerProperties).GetProperty(
"ResourceId",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
resourceIdProperty.SetValue(containerProps, "testCollRid");
// Set up additional mocks as needed
containerMock.Setup(c => c.GetCachedContainerPropertiesAsync(
It.IsAny<bool>(), It.IsAny<ITrace>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(containerProps);
Mock<Cosmos.Database> databaseMock = new Mock<Cosmos.Database>();
databaseMock.Setup(d => d.Id).Returns("testDb");
containerMock.Setup(c => c.Database).Returns(databaseMock.Object);
// FeedRangeEpk for the test - use a range that overlaps both partition key ranges
FeedRangeEpk feedRange = new FeedRangeEpk(new Documents.Routing.Range<string>(
"0DCEB8CE51C6BFE84F4BD9409F69B9BB",
"0DCEB8CE51C6BFE84F4BD9409F69B9BBFF",
true, false));
RequestInvokerHandler invoker = new RequestInvokerHandler(client, null, null, null)
{
InnerHandler = new TestHandler((request, token) => TestHandler.ReturnSuccess())
};
// Act
ResponseMessage response = await invoker.SendAsync(
"dbs/testDb/colls/testColl",
ResourceType.Document,
operationType,
null,
containerMock.Object,
feedRange,
null,
null,
NoOpTrace.Singleton,
CancellationToken.None);
// Assert
Assert.IsNotNull(response, "Response should not be null.");
if (shouldThrowGoneException)
{
Assert.IsFalse(response.IsSuccessStatusCode, "Expected a failure status code for Query operation.");
Assert.AreEqual(HttpStatusCode.Gone, response.StatusCode, "Expected a 410 Gone status code.");
Assert.AreEqual((int)SubStatusCodes.PartitionKeyRangeGone, (int)response.Headers.SubStatusCode, "Expected PartitionKeyRangeGone sub-status code.");
}
else
{
Assert.IsTrue(response.IsSuccessStatusCode, $"Expected a successful status code, but got {response.StatusCode}.");
}
}
// Custom MockDocumentClient that allows injecting our TestPartitionKeyRangeCache
private class CustomMockDocumentClient : MockDocumentClient
{
private readonly TestPartitionKeyRangeCache testPartitionKeyRangeCache;
public CustomMockDocumentClient(TestPartitionKeyRangeCache testPartitionKeyRangeCache)
: base(new ConnectionPolicy())
{
this.testPartitionKeyRangeCache = testPartitionKeyRangeCache;
}
internal override Task<PartitionKeyRangeCache> GetPartitionKeyRangeCacheAsync(ITrace trace)
{
return Task.FromResult<PartitionKeyRangeCache>(this.testPartitionKeyRangeCache);
}
}
private class TestPartitionKeyRangeCache : PartitionKeyRangeCache
{
private readonly IReadOnlyList<PartitionKeyRange> overlappingRanges;
public TestPartitionKeyRangeCache(IReadOnlyList<PartitionKeyRange> overlappingRanges)
: base(null, null, null, null) // Pass nulls or mocks as needed for base constructor
{
this.overlappingRanges = overlappingRanges;
}
public override Task<IReadOnlyList<PartitionKeyRange>> TryGetOverlappingRangesAsync(
string collectionRid,
Documents.Routing.Range<string> range,
ITrace trace,
bool forceRefresh)
{
return Task.FromResult(this.overlappingRanges);
}
}
[TestMethod]
public async Task RetryHandlerDoesNotRetryOnSuccess()
{
using CosmosClient client = MockCosmosUtil.CreateMockCosmosClient();
RetryHandler retryHandler = new RetryHandler(client);
int handlerCalls = 0;
int expectedHandlerCalls = 1;
TestHandler testHandler = new TestHandler((request, cancellationToken) =>
{
handlerCalls++;
return TestHandler.ReturnSuccess();
});
retryHandler.InnerHandler = testHandler;
RequestInvokerHandler invoker = new RequestInvokerHandler(
client,
requestedClientConsistencyLevel: null,
requestedClientPriorityLevel: null,
requestedClientThroughputBucket: null)
{
InnerHandler = retryHandler
};
RequestMessage requestMessage = new RequestMessage(HttpMethod.Delete, RetryHandlerTests.TestUri);
requestMessage.Headers.Add(HttpConstants.HttpHeaders.PartitionKey, "[]");
requestMessage.ResourceType = ResourceType.Document;
requestMessage.OperationType = OperationType.Read;
await invoker.SendAsync(requestMessage, new CancellationToken());
Assert.AreEqual(expectedHandlerCalls, handlerCalls);
}
[TestMethod]
public async Task RetryHandlerRetriesOn429()
{
using CosmosClient client = MockCosmosUtil.CreateMockCosmosClient();
RetryHandler retryHandler = new RetryHandler(client);
int handlerCalls = 0;
int expectedHandlerCalls = 2;
TestHandler testHandler = new TestHandler((request, cancellationToken) =>
{
if (handlerCalls == 0)
{
handlerCalls++;
return TestHandler.ReturnStatusCode((HttpStatusCode)StatusCodes.TooManyRequests);
}
handlerCalls++;
return TestHandler.ReturnSuccess();
});
retryHandler.InnerHandler = testHandler;
RequestInvokerHandler invoker = new RequestInvokerHandler(
client,
requestedClientConsistencyLevel: null,
requestedClientPriorityLevel: null,
requestedClientThroughputBucket: null)
{
InnerHandler = retryHandler
};
RequestMessage requestMessage = new RequestMessage(HttpMethod.Delete, RetryHandlerTests.TestUri);
requestMessage.Headers.Add(HttpConstants.HttpHeaders.PartitionKey, "[]");
requestMessage.ResourceType = ResourceType.Document;
requestMessage.OperationType = OperationType.Read;
await invoker.SendAsync(requestMessage, new CancellationToken());
Assert.AreEqual(expectedHandlerCalls, handlerCalls);
}
[TestMethod]
[ExpectedException(typeof(Exception))]
public async Task RetryHandlerDoesNotRetryOnException()
{
using CosmosClient client = MockCosmosUtil.CreateMockCosmosClient();
RetryHandler retryHandler = new RetryHandler(client);
int handlerCalls = 0;
int expectedHandlerCalls = 2;
TestHandler testHandler = new TestHandler((request, cancellationToken) =>
{
handlerCalls++;
if (handlerCalls == expectedHandlerCalls)
{
Assert.Fail("Should not retry on exception.");
}
throw new Exception("You shall not retry.");
});
retryHandler.InnerHandler = testHandler;
RequestInvokerHandler invoker = new RequestInvokerHandler(
client,
requestedClientConsistencyLevel: null,
requestedClientPriorityLevel: null,
requestedClientThroughputBucket: null)
{
InnerHandler = retryHandler
};
RequestMessage requestMessage = new RequestMessage(HttpMethod.Get, new System.Uri("https://dummy.documents.azure.com:443/dbs"));
requestMessage.Headers.Add(HttpConstants.HttpHeaders.PartitionKey, "[]");
requestMessage.ResourceType = ResourceType.Document;
requestMessage.OperationType = OperationType.Read;
await invoker.SendAsync(requestMessage, new CancellationToken());
}
[TestMethod]
public async Task RetryHandlerHttpClientExceptionRefreshesLocations()
{
using DocumentClient dc = new MockDocumentClient(RetryHandlerTests.TestUri, MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey);
using CosmosClient client = new CosmosClient(
RetryHandlerTests.TestUri.OriginalString,
MockCosmosUtil.RandomInvalidCorrectlyFormatedAuthKey,
new CosmosClientOptions(),
dc);
Mock<IDocumentClientRetryPolicy> mockClientRetryPolicy = new Mock<IDocumentClientRetryPolicy>();
mockClientRetryPolicy.Setup(m => m.ShouldRetryAsync(It.IsAny<Exception>(), It.IsAny<CancellationToken>()))
.Returns<Exception, CancellationToken>((ex, tooken) => Task.FromResult(ShouldRetryResult.RetryAfter(TimeSpan.FromMilliseconds(1))));
Mock<IRetryPolicyFactory> mockRetryPolicy = new Mock<IRetryPolicyFactory>();
mockRetryPolicy.Setup(m => m.GetRequestPolicy())
.Returns(() => mockClientRetryPolicy.Object);
RetryHandler retryHandler = new RetryHandler(client);
int handlerCalls = 0;
int expectedHandlerCalls = 2;
TestHandler testHandler = new TestHandler((request, response) =>
{
handlerCalls++;
if (handlerCalls == expectedHandlerCalls)
{
return TestHandler.ReturnSuccess();
}
throw new HttpRequestException("DNS or some other network issue");
});
retryHandler.InnerHandler = testHandler;
RequestInvokerHandler invoker = new RequestInvokerHandler(
client,
requestedClientConsistencyLevel: null,
requestedClientPriorityLevel: null,
requestedClientThroughputBucket: null)
{
InnerHandler = retryHandler
};
RequestMessage requestMessage = new RequestMessage(HttpMethod.Get, new System.Uri("https://dummy.documents.azure.com:443/dbs"));
requestMessage.Headers.Add(HttpConstants.HttpHeaders.PartitionKey, "[]");
requestMessage.ResourceType = ResourceType.Document;
requestMessage.OperationType = OperationType.Read;
await invoker.SendAsync(requestMessage, new CancellationToken());
Assert.AreEqual(expectedHandlerCalls, handlerCalls);
}
[TestMethod]
public async Task RetryHandlerNoRetryOnAuthError()
{
await this.RetryHandlerDontRetryOnStatusCode(HttpStatusCode.Unauthorized);
}
[TestMethod]
public async Task RetryHandlerNoRetryOnWriteForbidden()
{
await this.RetryHandlerDontRetryOnStatusCode(HttpStatusCode.Forbidden, SubStatusCodes.WriteForbidden);
}
[TestMethod]
public async Task RetryHandlerNoRetryOnSessionNotAvailable()
{
await this.RetryHandlerDontRetryOnStatusCode(HttpStatusCode.NotFound, SubStatusCodes.ReadSessionNotAvailable);
}
[TestMethod]
public async Task RetryHandlerNoRetryOnDatabaseAccountNotFound()
{
await this.RetryHandlerDontRetryOnStatusCode(HttpStatusCode.Forbidden, SubStatusCodes.DatabaseAccountNotFound);
}
private async Task RetryHandlerDontRetryOnStatusCode(
HttpStatusCode statusCode,
SubStatusCodes subStatusCode = SubStatusCodes.Unknown)
{
int handlerCalls = 0;
TestHandler testHandler = new TestHandler((request, response) =>
{
handlerCalls++;
if (handlerCalls == 0)
{
return TestHandler.ReturnStatusCode(statusCode, subStatusCode);
}
return TestHandler.ReturnSuccess();
});
using CosmosClient client = MockCosmosUtil.CreateMockCosmosClient();
RetryHandler retryHandler = new RetryHandler(client)
{
InnerHandler = testHandler
};
RequestInvokerHandler invoker = new RequestInvokerHandler(
client,
requestedClientConsistencyLevel: null,
requestedClientPriorityLevel: null,
requestedClientThroughputBucket: null)
{
InnerHandler = retryHandler
};
RequestMessage requestMessage = new RequestMessage(HttpMethod.Delete, RetryHandlerTests.TestUri);
requestMessage.Headers.Add(HttpConstants.HttpHeaders.PartitionKey, "[]");
requestMessage.ResourceType = ResourceType.Document;
requestMessage.OperationType = OperationType.Read;
await invoker.SendAsync(requestMessage, new CancellationToken());
int expectedHandlerCalls = 1;
Assert.AreEqual(expectedHandlerCalls, handlerCalls);
}
[TestMethod]
public async Task InvalidPartitionExceptionRetryHandlerDoesNotRetryOnSuccess()
{
using CosmosClient client = MockCosmosUtil.CreateMockCosmosClient();
NamedCacheRetryHandler retryHandler = new NamedCacheRetryHandler();
int handlerCalls = 0;
int expectedHandlerCalls = 1;
TestHandler testHandler = new TestHandler((request, cancellationToken) =>
{
handlerCalls++;
return TestHandler.ReturnSuccess();
});
retryHandler.InnerHandler = testHandler;
RequestInvokerHandler invoker = new RequestInvokerHandler(
client,
requestedClientConsistencyLevel: null,
requestedClientPriorityLevel: null,
requestedClientThroughputBucket: null)
{
InnerHandler = retryHandler
};
RequestMessage requestMessage = new RequestMessage(
HttpMethod.Get,
new Uri("https://dummy.documents.azure.com:443/dbs"));
await invoker.SendAsync(requestMessage, new CancellationToken());
Assert.AreEqual(expectedHandlerCalls, handlerCalls);
}
[TestMethod]
public async Task InvalidPartitionExceptionRetryHandlerDoesNotRetryOn410()
{
using CosmosClient client = MockCosmosUtil.CreateMockCosmosClient();
NamedCacheRetryHandler retryHandler = new NamedCacheRetryHandler();
int handlerCalls = 0;
int expectedHandlerCalls = 2;
TestHandler testHandler = new TestHandler((request, cancellationToken) =>
{
request.OnBeforeSendRequestActions(request.ToDocumentServiceRequest());
if (handlerCalls == 0)
{
handlerCalls++;
return TestHandler.ReturnStatusCode((HttpStatusCode)StatusCodes.Gone, SubStatusCodes.NameCacheIsStale);
}
handlerCalls++;
return TestHandler.ReturnSuccess();
});
retryHandler.InnerHandler = testHandler;
RequestInvokerHandler invoker = new RequestInvokerHandler(
client,
requestedClientConsistencyLevel: null,
requestedClientPriorityLevel: null,
requestedClientThroughputBucket: null)
{
InnerHandler = retryHandler
};
RequestMessage requestMessage = new RequestMessage(HttpMethod.Get, new Uri("https://dummy.documents.azure.com:443/dbs"));
await invoker.SendAsync(requestMessage, new CancellationToken());
Assert.AreEqual(expectedHandlerCalls, handlerCalls);
}
}
}