-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathThinClientStoreModelTests.cs
More file actions
450 lines (374 loc) · 21.7 KB
/
ThinClientStoreModelTests.cs
File metadata and controls
450 lines (374 loc) · 21.7 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
446
447
448
449
450
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Common;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
[TestClass]
public class ThinClientStoreModelTests
{
private ThinClientStoreModel thinClientStoreModel;
private GlobalEndpointManager endpointManager;
private SessionContainer sessionContainer;
private readonly ConsistencyLevel defaultConsistencyLevel = ConsistencyLevel.Session;
[TestInitialize]
public void TestInitialize()
{
this.sessionContainer = new SessionContainer("testhost");
Mock<IDocumentClientInternal> mockDocumentClient = new Mock<IDocumentClientInternal>();
mockDocumentClient
.Setup(c => c.ServiceEndpoint)
.Returns(new Uri("https://mock.proxy.com"));
ConnectionPolicy connectionPolicy = new ConnectionPolicy
{
UseMultipleWriteLocations = false
};
this.endpointManager = new GlobalEndpointManager(
owner: mockDocumentClient.Object,
connectionPolicy: connectionPolicy);
this.thinClientStoreModel = new ThinClientStoreModel(
endpointManager: this.endpointManager,
globalPartitionEndpointManager: GlobalPartitionEndpointManagerNoOp.Instance,
sessionContainer: this.sessionContainer,
defaultConsistencyLevel: (Cosmos.ConsistencyLevel)this.defaultConsistencyLevel,
eventSource: new DocumentClientEventSource(),
serializerSettings: null,
httpClient: null);
PartitionKeyRangeCache pkRangeCache =
(PartitionKeyRangeCache)FormatterServices.GetUninitializedObject(typeof(PartitionKeyRangeCache));
ClientCollectionCache collCache =
(ClientCollectionCache)FormatterServices.GetUninitializedObject(typeof(ClientCollectionCache));
this.thinClientStoreModel.SetCaches(pkRangeCache, collCache);
System.Diagnostics.Trace.CorrelationManager.ActivityId = Guid.NewGuid();
}
[TestCleanup]
public void TestCleanup()
{
System.Diagnostics.Trace.CorrelationManager.ActivityId = Guid.Empty;
this.thinClientStoreModel?.Dispose();
}
[TestMethod]
public async Task ProcessMessageAsync_Success_ShouldReturnDocumentServiceResponse()
{
// Arrange
// A single base64-encoded RNTBD response representing an HTTP 201 (Created)
string mockBase64 = "9AEAAMkAAAAIvhHfD23jSaynaR+gyTZ3AAAAAQIAByFUaHUsIDEzIEZlYiAyMDI1IDE0OjI1OjI4LjAyNCBHTVQEAAgmACIwMDAwYWQzZS0wMDAwLTAyMDAtMDAwMC02N2FlNjRjMDAwMDAiDgAIVABkb2N1bWVudFNpemU9NTEyMDA7ZG9jdW1lbnRzU2l6ZT01MjQyODgwMDtkb2N1bWVudHNDb3VudD0tMTtjb2xsZWN0aW9uU2l6ZT01MjQyODgwMDsPAAhBAGRvY3VtZW50U2l6ZT0wO2RvY3VtZW50c1NpemU9MTtkb2N1bWVudHNDb3VudD04O2NvbGxlY3Rpb25TaXplPTM7EAAHBDEuMTkTAAUKAAAAAAAAABUADgzDMAzDMBxAFwAIOgBkYnMvdGhpbi1jbGllbnQtdGVzdC1kYi9jb2xscy90aGluLWNsaWVudC10ZXN0LWNvbnRhaW5lci0xGAAIDABOSDF1QUo2QU5tMD0aAAUJAAAAAAAAAB4AAgMAAAAfAAIEAAAAIQAIAQAwJgACAQAAACkABQkAAAAAAAAAMAACAAAAADUAAgEAAAA6AAUKAAAAAAAAADsABQkAAAAAAAAAPgAIBQAtMSMxMFEADkjhehSuRxBAYwAIAQAweAAF//////////89AQAAeyJpZCI6IjNiMTFiNDM2LTViMTUtNGQwZS1iZWYwLWY1MzVmNjA0MTQxYyIsInBrIjoicGsiLCJuYW1lIjoiODM2MzI0NTA2IiwiZW1haWwiOiJhYmNAZGVmLmNvbSIsImJvZHkiOiJibGFibGEiLCJfcmlkIjoiTkgxdUFKNkFObTBKQUFBQUFBQUFBQT09IiwiX3NlbGYiOiJkYnMvTkgxdUFBPT0vY29sbHMvTkgxdUFKNkFObTA9L2RvY3MvTkgxdUFKNkFObTBKQUFBQUFBQUFBQT09LyIsIl9ldGFnIjoiXCIwMDAwYWQzZS0wMDAwLTAyMDAtMDAwMC02N2FlNjRjMDAwMDBcIiIsIl9hdHRhY2htZW50cyI6ImF0dGFjaG1lbnRzLyIsIl90cyI6MTczOTQ4MjMwNH0=";
HttpResponseMessage successResponse = new HttpResponseMessage(HttpStatusCode.Created)
{
Content = new ByteArrayContent(Convert.FromBase64String(mockBase64))
};
MockThinClientStoreClient thinClientStoreClient = new MockThinClientStoreClient(
(request, resourceType, uri, endpoint, globalDatabaseAccountName, clientCollectionCache, cancellationToken) =>
{
Stream responseBody = successResponse.Content.ReadAsStream();
INameValueCollection headers = new StoreResponseNameValueCollection();
return Task.FromResult(new DocumentServiceResponse(responseBody, headers, successResponse.StatusCode));
});
DocumentServiceRequest request = DocumentServiceRequest.Create(
operationType: OperationType.Create,
resourceType: ResourceType.Document,
resourceId: "NH1uAJ6ANm0=",
body: null,
authorizationTokenType: AuthorizationTokenType.PrimaryMasterKey);
Mock<IDocumentClientInternal> docClientMulti = new Mock<IDocumentClientInternal>();
docClientMulti.Setup(c => c.ServiceEndpoint).Returns(new Uri("http://localhost"));
AccountProperties validAccountProperties = new AccountProperties();
docClientMulti
.Setup(c => c.GetDatabaseAccountInternalAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(validAccountProperties);
ConnectionPolicy policy = new ConnectionPolicy
{
UseMultipleWriteLocations = true
};
GlobalEndpointManager multiEndpointMgr = new GlobalEndpointManager(docClientMulti.Object, policy);
ThinClientStoreModel storeModel = new ThinClientStoreModel(
endpointManager: multiEndpointMgr,
globalPartitionEndpointManager: GlobalPartitionEndpointManagerNoOp.Instance,
sessionContainer: this.sessionContainer,
defaultConsistencyLevel: (Cosmos.ConsistencyLevel)this.defaultConsistencyLevel,
eventSource: new DocumentClientEventSource(),
serializerSettings: null,
httpClient: null);
ClientCollectionCache clientCollectionCache = new Mock<ClientCollectionCache>(
this.sessionContainer,
storeModel,
null,
null,
null,
false).Object;
PartitionKeyRangeCache partitionKeyRangeCache = new Mock<PartitionKeyRangeCache>(
null,
storeModel,
clientCollectionCache,
multiEndpointMgr,
false).Object;
storeModel.SetCaches(partitionKeyRangeCache, clientCollectionCache);
// Inject the thinclient store client that returns 201
ReplaceThinClientStoreClientField(storeModel, thinClientStoreClient);
// Act
DocumentServiceResponse response = await storeModel.ProcessMessageAsync(request);
// Assert
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
}
[TestMethod]
public void Dispose_ShouldDisposeThinClientStoreClient()
{
// Arrange
bool disposeCalled = false;
ThinClientStoreClient thinClientStoreClient = new MockThinClientStoreClient(
(request, resourceType, uri, endpoint, globalDatabaseAccountName, clientCollectionCache, cancellationToken) =>
throw new NotImplementedException(),
() => disposeCalled = true);
ReplaceThinClientStoreClientField(this.thinClientStoreModel, thinClientStoreClient);
// Act
this.thinClientStoreModel.Dispose();
// Assert
Assert.IsTrue(disposeCalled, "Expected Dispose to be called on ThinClientStoreClient.");
}
[TestMethod]
public async Task ProcessMessageAsync_404_ShouldThrowDocumentClientException()
{
// Arrange
MockThinClientStoreClient thinClientStoreClient = new MockThinClientStoreClient(
(request, resourceType, uri, endpoint, globalDatabaseAccountName, clientCollectionCache, cancellationToken) =>
throw new DocumentClientException(
message: "Not Found",
innerException: null,
responseHeaders: new StoreResponseNameValueCollection(),
statusCode: HttpStatusCode.NotFound,
requestUri: uri));
DocumentServiceRequest request = DocumentServiceRequest.Create(
operationType: OperationType.Read,
resourceType: ResourceType.Document,
resourceId: "NH1uAJ6ANm0=",
body: null,
authorizationTokenType: AuthorizationTokenType.PrimaryMasterKey);
Mock<IDocumentClientInternal> docClientOkay = new Mock<IDocumentClientInternal>();
docClientOkay
.Setup(c => c.ServiceEndpoint)
.Returns(new Uri("https://myCosmosAccount.documents.azure.com/"));
AccountProperties validProperties = new AccountProperties();
docClientOkay
.Setup(c => c.GetDatabaseAccountInternalAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(validProperties);
ConnectionPolicy policy = new ConnectionPolicy
{
UseMultipleWriteLocations = false
};
GlobalEndpointManager endpointManagerOk = new GlobalEndpointManager(docClientOkay.Object, policy);
ThinClientStoreModel storeModel = new ThinClientStoreModel(
endpointManager: endpointManagerOk,
globalPartitionEndpointManager: GlobalPartitionEndpointManagerNoOp.Instance,
sessionContainer: this.sessionContainer,
defaultConsistencyLevel: (Cosmos.ConsistencyLevel)this.defaultConsistencyLevel,
eventSource: new DocumentClientEventSource(),
serializerSettings: null,
httpClient: null);
ClientCollectionCache clientCollectionCache = new Mock<ClientCollectionCache>(
this.sessionContainer,
storeModel,
null,
null,
null,
false).Object;
PartitionKeyRangeCache partitionKeyRangeCache = new Mock<PartitionKeyRangeCache>(
null,
storeModel,
clientCollectionCache,
endpointManagerOk,
false).Object;
storeModel.SetCaches(partitionKeyRangeCache, clientCollectionCache);
ReplaceThinClientStoreClientField(storeModel, thinClientStoreClient);
// Act & Assert
await Assert.ThrowsExceptionAsync<DocumentClientException>(
async () => await storeModel.ProcessMessageAsync(request),
"Expected 404 DocumentClientException from the final thinClientStore call");
}
[TestMethod]
public async Task PartitionLevelFailoverEnabled_ResolvesPartitionKeyRangeAndCallsLocationOverride()
{
Mock<IDocumentClientInternal> mockDocumentClient = new Mock<IDocumentClientInternal>();
mockDocumentClient.Setup(c => c.ServiceEndpoint).Returns(new Uri("https://mock.proxy.com"));
mockDocumentClient
.Setup(c => c.GetDatabaseAccountInternalAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new AccountProperties());
ConnectionPolicy connectionPolicy = new ConnectionPolicy();
GlobalEndpointManager endpointManager = new GlobalEndpointManager(mockDocumentClient.Object, connectionPolicy);
Mock<GlobalPartitionEndpointManager> globalPartitionEndpointManager = new Mock<GlobalPartitionEndpointManager>();
globalPartitionEndpointManager
.Setup(m => m.TryAddPartitionLevelLocationOverride(It.IsAny<DocumentServiceRequest>()))
.Returns(true)
.Verifiable();
ISessionContainer sessionContainer = new Mock<ISessionContainer>().Object;
DocumentClientEventSource eventSource = new Mock<DocumentClientEventSource>().Object;
Newtonsoft.Json.JsonSerializerSettings serializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
CosmosHttpClient httpClient = new Mock<CosmosHttpClient>().Object;
ThinClientStoreModel storeModel = new ThinClientStoreModel(
endpointManager,
globalPartitionEndpointManager.Object,
sessionContainer,
Cosmos.ConsistencyLevel.Session,
eventSource,
serializerSettings,
httpClient,
isPartitionLevelFailoverEnabled: true);
Mock<ClientCollectionCache> mockCollectionCache = new Mock<ClientCollectionCache>(
sessionContainer,
storeModel,
null,
null,
null,
false);
ContainerProperties containerProperties = new ContainerProperties("test", "/pk");
typeof(ContainerProperties)
.GetProperty("ResourceId", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public)
?.SetValue(containerProperties, "testCollectionRid");
containerProperties.PartitionKeyPath = "/pk";
mockCollectionCache
.Setup(c => c.ResolveCollectionAsync(It.IsAny<DocumentServiceRequest>(), It.IsAny<CancellationToken>(), It.IsAny<ITrace>()))
.ReturnsAsync(containerProperties);
Mock<PartitionKeyRangeCache> mockPartitionKeyRangeCache = new Mock<PartitionKeyRangeCache>(
null,
storeModel,
mockCollectionCache.Object,
endpointManager,
false);
PartitionKeyRange pkRange = new PartitionKeyRange { Id = "0", MinInclusive = "", MaxExclusive = "FF" };
List<PartitionKeyRange> pkRanges = new List<PartitionKeyRange> { pkRange };
IEnumerable<Tuple<PartitionKeyRange, ServiceIdentity>> rangeTuples = pkRanges.Select(r => Tuple.Create(r, (ServiceIdentity)null));
CollectionRoutingMap routingMap = CollectionRoutingMap.TryCreateCompleteRoutingMap(rangeTuples, "testCollectionRid");
mockPartitionKeyRangeCache
.Setup(c => c.TryLookupAsync(It.IsAny<string>(), It.IsAny<CollectionRoutingMap>(), It.IsAny<DocumentServiceRequest>(), It.IsAny<ITrace>()))
.ReturnsAsync(routingMap);
storeModel.SetCaches(mockPartitionKeyRangeCache.Object, mockCollectionCache.Object);
DocumentServiceRequest request = CreatePartitionedDocumentRequest();
MockThinClientStoreClient mockThinClientStoreClient = new MockThinClientStoreClient(
(DocumentServiceRequest req, ResourceType resourceType, Uri uri, Uri endpoint, string globalDatabaseAccountName, ClientCollectionCache clientCollectionCache, CancellationToken cancellationToken) =>
{
MemoryStream stream = new MemoryStream(new byte[] { 1, 2, 3 });
INameValueCollection headers = new StoreResponseNameValueCollection();
return Task.FromResult(new DocumentServiceResponse(stream, headers, HttpStatusCode.OK));
});
ReplaceThinClientStoreClientField(storeModel, mockThinClientStoreClient);
// Act
await storeModel.ProcessMessageAsync(request);
// Assert
globalPartitionEndpointManager.Verify(m => m.TryAddPartitionLevelLocationOverride(It.IsAny<DocumentServiceRequest>()), Times.Once());
}
[TestMethod]
public void CircuitBreaker_MarksPartitionUnavailableOnRepeatedFailures()
{
Mock<IDocumentClientInternal> mockDocumentClient = new Mock<IDocumentClientInternal>();
mockDocumentClient.Setup(c => c.ServiceEndpoint).Returns(new Uri("https://mock.proxy.com"));
ConnectionPolicy connectionPolicy = new ConnectionPolicy();
GlobalEndpointManager endpointManager = new GlobalEndpointManager(mockDocumentClient.Object, connectionPolicy);
Mock<GlobalPartitionEndpointManager> globalPartitionEndpointManager = new Mock<GlobalPartitionEndpointManager>();
globalPartitionEndpointManager
.Setup(m => m.TryAddPartitionLevelLocationOverride(It.IsAny<DocumentServiceRequest>()))
.Returns(true);
globalPartitionEndpointManager
.Setup(m => m.TryMarkEndpointUnavailableForPartitionKeyRange(It.IsAny<DocumentServiceRequest>()))
.Returns(true)
.Verifiable();
globalPartitionEndpointManager
.Setup(m => m.IncrementRequestFailureCounterAndCheckIfPartitionCanFailover(It.IsAny<DocumentServiceRequest>()))
.Returns(true);
ISessionContainer sessionContainer = new Mock<ISessionContainer>().Object;
DocumentClientEventSource eventSource = new Mock<DocumentClientEventSource>().Object;
Newtonsoft.Json.JsonSerializerSettings serializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
CosmosHttpClient httpClient = new Mock<CosmosHttpClient>().Object;
ThinClientStoreModel storeModel = new ThinClientStoreModel(
endpointManager,
globalPartitionEndpointManager.Object,
sessionContainer,
Cosmos.ConsistencyLevel.Session,
eventSource,
serializerSettings,
httpClient,
isPartitionLevelFailoverEnabled: true);
TestUtils.SetupCachesInGatewayStoreModel(storeModel, endpointManager);
DocumentServiceRequest request = CreatePartitionedDocumentRequest();
for (int i = 0; i < 3; i++)
{
globalPartitionEndpointManager.Object.IncrementRequestFailureCounterAndCheckIfPartitionCanFailover(request);
}
globalPartitionEndpointManager.Object.TryMarkEndpointUnavailableForPartitionKeyRange(request);
globalPartitionEndpointManager.Verify(m => m.TryMarkEndpointUnavailableForPartitionKeyRange(It.IsAny<DocumentServiceRequest>()), Times.Once());
}
private static void ReplaceThinClientStoreClientField(ThinClientStoreModel model, ThinClientStoreClient newClient)
{
FieldInfo field = typeof(ThinClientStoreModel).GetField(
"thinClientStoreClient",
BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException("Could not find 'thinClientStoreClient' field on ThinClientStoreModel");
field.SetValue(model, newClient);
}
private static DocumentServiceRequest CreatePartitionedDocumentRequest()
{
DocumentServiceRequest request = DocumentServiceRequest.Create(
OperationType.Read,
ResourceType.Document,
"/dbs/test/colls/test/docs/test",
AuthorizationTokenType.PrimaryMasterKey);
request.Headers[HttpConstants.HttpHeaders.PartitionKey] = "[\"test\"]";
request.RequestContext = new DocumentServiceRequestContext();
return request;
}
internal class MockThinClientStoreClient : ThinClientStoreClient
{
private readonly Func<DocumentServiceRequest, ResourceType, Uri, Uri, string, ClientCollectionCache, CancellationToken, Task<DocumentServiceResponse>> invokeAsyncFunc;
private readonly Action onDispose;
public MockThinClientStoreClient(
Func<DocumentServiceRequest, ResourceType, Uri, Uri, string, ClientCollectionCache, CancellationToken, Task<DocumentServiceResponse>> invokeAsyncFunc,
Action onDispose = null)
: base(
httpClient: null,
eventSource: null,
serializerSettings: null)
{
this.invokeAsyncFunc = invokeAsyncFunc;
this.onDispose = onDispose;
}
public override async Task<DocumentServiceResponse> InvokeAsync(
DocumentServiceRequest request,
ResourceType resourceType,
Uri physicalAddress,
Uri thinClientEndpoint,
string globalDatabaseAccountName,
ClientCollectionCache clientCollectionCache,
CancellationToken cancellationToken)
{
return await this.invokeAsyncFunc(
request,
resourceType,
physicalAddress,
thinClientEndpoint,
globalDatabaseAccountName,
clientCollectionCache,
cancellationToken);
}
public override void Dispose()
{
base.Dispose();
this.onDispose?.Invoke();
}
}
}
}