-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathCancellationTokenTests.cs
More file actions
315 lines (277 loc) · 16.2 KB
/
CancellationTokenTests.cs
File metadata and controls
315 lines (277 loc) · 16.2 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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Common;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Tests;
using Microsoft.Azure.Documents;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
/// <summary>
/// Tests for <see cref="CancellationToken"/> scenarios.
/// </summary>
[TestClass]
public class CancellationTokenTests
{
[TestMethod]
[ExpectedException(typeof(OperationCanceledException))]
public async Task GatewayProcessMessageAsyncCancels()
{
using (CancellationTokenSource source = new CancellationTokenSource())
{
CancellationToken cancellationToken = source.Token;
int run = 0;
async Task<HttpResponseMessage> sendFunc(HttpRequestMessage request)
{
string content = await request.Content.ReadAsStringAsync();
Assert.AreEqual("content1", content);
if (run == 0)
{
// We force a retry but cancel the token to verify if the retry mechanism cancels inbetween
source.Cancel();
throw new WebException("", WebExceptionStatus.ConnectFailure);
}
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Response") };
}
Mock<IDocumentClientInternal> mockDocumentClient = new Mock<IDocumentClientInternal>();
mockDocumentClient.Setup(client => client.ServiceEndpoint).Returns(new Uri("https://foo"));
using GlobalEndpointManager endpointManager = new GlobalEndpointManager(mockDocumentClient.Object, new ConnectionPolicy());
ISessionContainer sessionContainer = new SessionContainer(string.Empty);
DocumentClientEventSource eventSource = DocumentClientEventSource.Instance;
HttpMessageHandler messageHandler = new MockMessageHandler(sendFunc);
using GatewayStoreModel storeModel = new GatewayStoreModel(
endpointManager,
sessionContainer,
ConsistencyLevel.Eventual,
eventSource,
null,
MockCosmosUtil.CreateCosmosHttpClient(
() => new HttpClient(messageHandler),
eventSource));
using (new ActivityScope(Guid.NewGuid()))
{
using (DocumentServiceRequest request =
DocumentServiceRequest.Create(
Documents.OperationType.Query,
Documents.ResourceType.Document,
new Uri("https://foo.com/dbs/db1/colls/coll1", UriKind.Absolute),
new MemoryStream(Encoding.UTF8.GetBytes("content1")),
AuthorizationTokenType.PrimaryMasterKey,
null))
{
await storeModel.ProcessMessageAsync(request, cancellationToken);
}
}
Assert.Fail();
}
}
[TestMethod]
[ExpectedException(typeof(OperationCanceledException))]
public async Task GatewayProcessMessageAsyncCancelsOnDeadline()
{
// Cancellation deadline is before Request timeout
using (CancellationTokenSource source = new CancellationTokenSource(TimeSpan.FromSeconds(2)))
{
CancellationToken cancellationToken = source.Token;
static async Task<HttpResponseMessage> sendFunc(HttpRequestMessage request)
{
string content = await request.Content.ReadAsStringAsync();
Assert.AreEqual("content1", content);
// Wait until CancellationTokenSource deadline expires
Thread.Sleep(2000);
// Force retries
throw new WebException("", WebExceptionStatus.ConnectFailure);
}
GatewayStoreModel storeModel = MockGatewayStoreModel(sendFunc);
using (new ActivityScope(Guid.NewGuid()))
{
using (DocumentServiceRequest request =
DocumentServiceRequest.Create(
Documents.OperationType.Query,
Documents.ResourceType.Document,
new Uri("https://foo.com/dbs/db1/colls/coll1", UriKind.Absolute),
new MemoryStream(Encoding.UTF8.GetBytes("content1")),
AuthorizationTokenType.PrimaryMasterKey,
null))
{
await storeModel.ProcessMessageAsync(request, cancellationToken);
}
}
Assert.Fail();
}
}
/// <summary>
/// This test verifies that if an HttpRequestException happens during an address resolution, the region is marked unavailable even if the user cancellationtoken is canceled.
///
/// 1. Get Addresses from AddressResolver (forceRefresh false)
/// 2. Do TCP requests calling the lambda -> Throws 410
/// 3. ReplicatedResourceClient + StoreClient handle the 410 by issueing an Address refresh
/// 4. AddressResolver throws HttpRequestException (forceRefresh true) simulating an HTTP connection error and cancels the token.
/// 5. ClientRetryPolicy should get it, mark the region unavailable on the GlobalEndpointManager.
/// 6. Initiate retry, which should be canceled because the token was canceled.
/// </summary>
[DataTestMethod]
[Timeout(5000)]
[DataRow(ConsistencyLevel.Eventual)]
[DataRow(ConsistencyLevel.Session)]
public async Task CancellationTokenDoesNotCancelFailover(ConsistencyLevel consistencyLevel)
{
using (CancellationTokenSource source = new CancellationTokenSource())
{
CancellationToken cancellationToken = source.Token;
Task<HttpResponseMessage> sendFunc(HttpRequestMessage request)
{
Assert.Fail("There should be no HTTP requests on this test, address resolution requests are handled by the mocked AddressResolver.");
throw new InvalidOperationException();
}
// TCP requests return 410 with TransportException to force address resolution
int tcpRequests = 0;
StoreResponse sendDirectFunc(Uri uri, ResourceOperation resourceOperation, DocumentServiceRequest request)
{
if (++tcpRequests > 1)
{
Assert.Fail("There should only be 1 TCP request that triggers an address resolution.");
}
throw new GoneException(new TransportException(TransportErrorCode.ConnectFailed, null, Guid.NewGuid(), new Uri("http://one.com"), "description", true, true), SubStatusCodes.Unknown);
}
using CosmosClient client = MockCosmosUtil.CreateMockCosmosClient();
client.DocumentClient.GatewayStoreModel = MockGatewayStoreModel(sendFunc);
client.DocumentClient.StoreModel = MockServerStoreModel(client.DocumentClient.Session, (Documents.ConsistencyLevel)consistencyLevel, source, sendDirectFunc);
CosmosOperationCanceledException ex = await Assert.ThrowsExceptionAsync<CosmosOperationCanceledException>(() => client.GetContainer("test", "test").ReadItemAsync<dynamic>("id", partitionKey: new Cosmos.PartitionKey("id"), cancellationToken: cancellationToken),
"Should have surfaced OperationCanceledException because the token was canceled.");
Assert.IsTrue(ex.CancellationToken.IsCancellationRequested);
Assert.AreEqual(cancellationToken.IsCancellationRequested, ex.CancellationToken.IsCancellationRequested);
Assert.AreEqual(cancellationToken.CanBeCanceled, ex.CancellationToken.CanBeCanceled);
Assert.AreEqual(cancellationToken.WaitHandle, ex.CancellationToken.WaitHandle);
((MockDocumentClient)client.DocumentClient).MockGlobalEndpointManager.Verify(gep => gep.MarkEndpointUnavailableForRead(It.IsAny<Uri>()), Times.Once, "Should had marked the endpoint unavailable");
((MockDocumentClient)client.DocumentClient).MockGlobalEndpointManager.Verify(gep => gep.RefreshLocationAsync(false), Times.Once, "Should had refreshed the account information");
string expectedHelpLink = "https://aka.ms/cosmosdb-tsg-request-timeout";
string expectedCancellationTokenStatus = $"Cancellation Token has expired: {cancellationToken.IsCancellationRequested}";
Assert.IsTrue(ex.Message.Contains(expectedHelpLink));
Assert.IsTrue(ex.Message.Contains(expectedCancellationTokenStatus));
Assert.IsTrue(ex.ToString().Contains(expectedHelpLink));
Assert.IsTrue(ex.ToString().Contains(expectedCancellationTokenStatus));
}
}
private class MockMessageHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, Task<HttpResponseMessage>> sendFunc;
public MockMessageHandler(Func<HttpRequestMessage, Task<HttpResponseMessage>> func)
{
this.sendFunc = func;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return await this.sendFunc(request);
}
}
private static GatewayStoreModel MockGatewayStoreModel(Func<HttpRequestMessage, Task<HttpResponseMessage>> sendFunc)
{
Mock<IDocumentClientInternal> mockDocumentClient = new Mock<IDocumentClientInternal>();
mockDocumentClient.Setup(client => client.ServiceEndpoint).Returns(new Uri("https://foo"));
Mock<GlobalEndpointManager> endpointManager = new Mock<GlobalEndpointManager>(mockDocumentClient.Object, new ConnectionPolicy(), false);
endpointManager.Setup(gep => gep.ResolveServiceEndpoint(It.IsAny<DocumentServiceRequest>())).Returns(new Uri("http://localhost"));
ISessionContainer sessionContainer = new SessionContainer(string.Empty);
HttpMessageHandler messageHandler = new MockMessageHandler(sendFunc);
return new GatewayStoreModel(
endpointManager.Object,
sessionContainer,
Cosmos.ConsistencyLevel.Eventual,
new DocumentClientEventSource(),
new JsonSerializerSettings(),
MockCosmosUtil.CreateCosmosHttpClient(() => new HttpClient(messageHandler)));
}
// Creates a StoreModel that will return addresses for normal requests and throw for address refresh
private static ServerStoreModel MockServerStoreModel(
object sessionContainer,
Documents.ConsistencyLevel consistencyLevel,
CancellationTokenSource cancellationTokenSource,
Func<Uri, ResourceOperation, DocumentServiceRequest, StoreResponse> sendDirectFunc)
{
MockTransportClient mockTransportClient = new MockTransportClient(sendDirectFunc);
AddressInformation[] addressInformation = GetMockAddressInformation();
Mock<IAddressResolver> mockAddressCache = new Mock<IAddressResolver>();
// Return mocked address for initial request
mockAddressCache.Setup(
cache => cache.ResolveAsync(
It.IsAny<DocumentServiceRequest>(),
It.Is<bool>(forceRefresh => forceRefresh == false),
It.IsAny<CancellationToken>()))
.Callback((DocumentServiceRequest dsr, bool refresh, CancellationToken ct) => dsr.RequestContext.ResolvedPartitionKeyRange = new PartitionKeyRange() { Id = "0" })
.ReturnsAsync(new PartitionAddressInformation(addressInformation));
// Return HttpRequestException for address refresh
int refreshes = 0;
mockAddressCache.Setup(
cache => cache.ResolveAsync(
It.IsAny<DocumentServiceRequest>(),
It.Is<bool>(forceRefresh => forceRefresh == true),
It.IsAny<CancellationToken>()))
.Callback((DocumentServiceRequest dsr, bool refresh, CancellationToken ct) =>
{
dsr.RequestContext.ResolvedPartitionKeyRange = new PartitionKeyRange() { Id = "0" };
// First refresh comes from a background task
if (++refreshes > 1)
{
cancellationTokenSource.Cancel();
throw new HttpRequestException();
}
})
.ReturnsAsync(new PartitionAddressInformation(addressInformation));
ReplicationPolicy replicationPolicy = new ReplicationPolicy
{
MaxReplicaSetSize = 1
};
Mock<IServiceConfigurationReader> mockServiceConfigReader = new Mock<IServiceConfigurationReader>();
Mock<IAuthorizationTokenProvider> mockAuthorizationTokenProvider = new Mock<IAuthorizationTokenProvider>();
mockServiceConfigReader.SetupGet(x => x.UserReplicationPolicy).Returns(replicationPolicy);
mockServiceConfigReader.SetupGet(x => x.DefaultConsistencyLevel).Returns(consistencyLevel);
return new ServerStoreModel(new StoreClient(
mockAddressCache.Object,
(SessionContainer)sessionContainer,
mockServiceConfigReader.Object,
mockAuthorizationTokenProvider.Object,
Documents.Client.Protocol.Tcp,
mockTransportClient));
}
private class MockTransportClient : TransportClient
{
private readonly Func<Uri, ResourceOperation, DocumentServiceRequest, StoreResponse> sendDirectFunc;
public MockTransportClient(Func<Uri, ResourceOperation, DocumentServiceRequest, StoreResponse> sendDirectFunc)
{
this.sendDirectFunc = sendDirectFunc;
}
internal override Task<StoreResponse> InvokeStoreAsync(Uri physicalAddress, ResourceOperation resourceOperation, DocumentServiceRequest request)
{
return Task.FromResult(this.sendDirectFunc(physicalAddress, resourceOperation, request));
}
}
private static AddressInformation[] GetMockAddressInformation()
{
// setup mocks for address information
AddressInformation[] addressInformation = new AddressInformation[3];
// construct URIs that look like the actual uri
// rntbd://yt1prdddc01-docdb-1.documents.azure.com:14003/apps/ce8ab332-f59e-4ce7-a68e-db7e7cfaa128/services/68cc0b50-04c6-4716-bc31-2dfefd29e3ee/partitions/5604283d-0907-4bf4-9357-4fa9e62de7b5/replicas/131170760736528207s/
for (int i = 0; i <= 2; i++)
{
addressInformation[i] = new AddressInformation(
physicalUri: "rntbd://dummytenant.documents.azure.com:14003/apps/APPGUID/services/SERVICEGUID/partitions/PARTITIONGUID/replicas/"
+ i.ToString("G", CultureInfo.CurrentCulture) + (i == 0 ? "p" : "s") + "/",
isPrimary: i == 0,
protocol: Documents.Client.Protocol.Tcp,
isPublic: true);
}
return addressInformation;
}
}
}