-
Notifications
You must be signed in to change notification settings - Fork 206
/
CosmosDBEndToEndTests.cs
313 lines (275 loc) · 13.6 KB
/
CosmosDBEndToEndTests.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.WebJobs.Extensions.Tests;
using Microsoft.Azure.WebJobs.Extensions.Tests.Common;
using Microsoft.Azure.WebJobs.Extensions.Tests.Extensions.CosmosDB.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.Azure.WebJobs.Extensions.CosmosDB.Tests
{
// The EndToEnd tests require the AzureWebJobsCosmosDBConnectionString environment variable to be set.
[Trait("Category", "E2E")]
public class CosmosDBEndToEndTests
{
private const string DatabaseName = "E2EDb";
private const string CollectionName = "E2ECollection";
private const string LeaseCollectionName = "leases";
private readonly TestLoggerProvider _loggerProvider = new TestLoggerProvider();
[Fact]
public async Task CosmosDBEndToEnd()
{
_loggerProvider.ClearAllLogMessages();
using var host = BuildHost(typeof(EndToEndTestClass));
using var client = await InitializeDocumentClientAsync(
host.Services.GetRequiredService<IConfiguration>(), DatabaseName, CollectionName, "/_partitionKey");
await host.StartAsync();
try
{
// Call the outputs function directly, which will write out 3 documents
// using with the 'input' property set to the value we provide.
var input = Guid.NewGuid().ToString();
var parameter = new Dictionary<string, object>();
parameter["input"] = input;
await host.GetJobHost().CallAsync(nameof(EndToEndTestClass.Outputs), parameter);
// Also insert a new Document so we can query on it.
var response = await client.GetContainer(DatabaseName, CollectionName).UpsertItemAsync<Item>(new Item() { Id = Guid.NewGuid().ToString() });
// Now craft a queue message to send to the Inputs, which will pull these documents.
var queueInput = new QueueItem
{
DocumentId = response.Resource.Id,
Input = input
};
parameter.Clear();
parameter["item"] = JsonConvert.SerializeObject(queueInput);
await host.GetJobHost().CallAsync(nameof(EndToEndTestClass.Inputs), parameter);
await TestHelpers.Await(() =>
{
var logMessages = _loggerProvider.GetAllLogMessages();
return logMessages.Count(p => p.FormattedMessage != null && p.FormattedMessage.Contains("Trigger called!")) == 4
&& logMessages.Count(p => p.FormattedMessage != null && p.FormattedMessage.Contains("Trigger with string called!")) == 4
&& logMessages.Count(p => p.FormattedMessage != null && p.FormattedMessage.Contains("Trigger with retry called!")) == 8
&& logMessages.Count(p => p.Exception != null && p.Exception.InnerException.Message.Contains("Test exception") && !p.Category.StartsWith("Host.Results")) > 0;
});
// Make sure the Options were logged. Just check a few values.
string optionsMessage = _loggerProvider.GetAllLogMessages()
.Single(m => m.Category == "Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService" && m.FormattedMessage.StartsWith(nameof(CosmosDBOptions)))
.FormattedMessage;
JObject loggedOptions = JObject.Parse(optionsMessage.Substring(optionsMessage.IndexOf(Environment.NewLine)));
Assert.Null(loggedOptions["ConnectionMode"].Value<string>());
}
finally
{
// Clean-up leases
await CleanUpLeaseContainer(client);
await host.StopAsync();
}
}
[Fact]
public async Task CosmosDBEndToEndCancellation()
{
_loggerProvider.ClearAllLogMessages();
using (var host = BuildHost(typeof(EndToEndCancellationTestClass)))
{
using var client = await InitializeDocumentClientAsync(
host.Services.GetRequiredService<IConfiguration>(), DatabaseName, CollectionName, "/_partitionKey");
await host.StartAsync();
// Insert an item to ensure the function is triggered
var response = await client.GetContainer(DatabaseName, CollectionName).UpsertItemAsync<Item>(new Item() { Id = Guid.NewGuid().ToString() });
// Trigger cancellation by stopping the host after the function has been triggered
await TestHelpers.Await(() => _loggerProvider.GetAllLogMessages().Any(p => p.FormattedMessage != null && p.FormattedMessage.Contains("Trigger called!")));
await host.StopAsync();
}
// Start the host again and wait for the logs to show the cancelled item was reprocessed
using (var host = BuildHost(typeof(EndToEndCancellationTestClass)))
{
using var client = await InitializeDocumentClientAsync(
host.Services.GetRequiredService<IConfiguration>(), DatabaseName, CollectionName, "/_partitionKey");
await host.StartAsync();
try
{
await TestHelpers.Await(() =>
{
var logMessages = _loggerProvider.GetAllLogMessages();
return logMessages.Count(p => p.FormattedMessage != null && p.FormattedMessage.Contains("Trigger called!")) > 1
&& logMessages.Count(p => p.FormattedMessage != null && p.FormattedMessage.Contains("Trigger canceled!")) == 1
&& logMessages.Count(p => p.FormattedMessage != null && p.FormattedMessage.Contains("Saw the first document again!")) == 1
&& logMessages.Count(p => p.Exception is TaskCanceledException) > 0;
});
}
finally
{
// Clean-up leases
await CleanUpLeaseContainer(client);
await host.StopAsync();
}
}
}
public static async Task<CosmosClient> InitializeDocumentClientAsync(
IConfiguration configuration, string databaseName, string collectionName, string partitionKey)
{
var client = new CosmosClient(configuration.GetConnectionStringOrSetting(Constants.DefaultConnectionStringName).Value);
Database database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
await database.CreateContainerIfNotExistsAsync(collectionName, partitionKey);
await database.CreateContainerIfNotExistsAsync(LeaseCollectionName, "/id");
return client;
}
public static async Task CleanUpLeaseContainer(CosmosClient client)
{
Container leaseContainer = client.GetContainer(DatabaseName, LeaseCollectionName);
using FeedIterator<JObject> leaseIterator = leaseContainer.GetItemQueryIterator<JObject>();
while (leaseIterator.HasMoreResults)
{
FeedResponse<JObject> leaseIteratorResponse = await leaseIterator.ReadNextAsync();
foreach (JObject lease in leaseIteratorResponse)
{
ResponseMessage delete = await leaseContainer.DeleteItemStreamAsync(lease.Value<string>("id"), new PartitionKey(lease.Value<string>("id")));
if (delete.StatusCode == System.Net.HttpStatusCode.NotFound)
{
// Support old non-partitioned lease container in CI
await leaseContainer.DeleteItemStreamAsync(lease.Value<string>("id"), PartitionKey.None);
}
}
}
}
private IHost BuildHost(Type testType)
{
ExplicitTypeLocator locator = new ExplicitTypeLocator(testType);
IHost host = new HostBuilder()
.ConfigureWebJobs(builder =>
{
builder
.AddAzureStorageBlobs()
.AddAzureStorageQueues()
.AddCosmosDB();
})
.ConfigureAppConfiguration(c =>
{
c.AddTestSettings();
})
.ConfigureServices(services =>
{
services.AddSingleton<ITypeLocator>(locator);
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddProvider(_loggerProvider);
})
.Build();
return host;
}
public class QueueItem
{
public string DocumentId { get; set; }
public string Input { get; set; }
}
private static class EndToEndTestClass
{
private static bool shouldThrow = true;
[NoAutomaticTrigger]
public static async Task Outputs(
string input,
[CosmosDB(DatabaseName, CollectionName, CreateIfNotExists = true)] IAsyncCollector<object> collector,
ILogger log)
{
for (int i = 0; i < 3; i++)
{
await collector.AddAsync(new { input = input, id = Guid.NewGuid().ToString() });
}
}
[NoAutomaticTrigger]
public static void Inputs(
[QueueTrigger("NotUsed")] QueueItem item,
[CosmosDB(DatabaseName, CollectionName, Id = "{DocumentId}")] JObject document,
[CosmosDB(DatabaseName, CollectionName, SqlQuery = "SELECT * FROM c where c.input = {Input}")] IEnumerable<Item> documents,
ILogger log)
{
Assert.NotNull(document);
Assert.Equal(3, documents.Count());
}
public static void Trigger(
[CosmosDBTrigger(DatabaseName, CollectionName, CreateLeaseContainerIfNotExists = true, LeaseContainerPrefix = "ciTrigger")]IReadOnlyList<Item> documents,
ILogger log)
{
foreach (var document in documents)
{
log.LogInformation("Trigger called!");
}
}
public static void TriggerWithString(
[CosmosDBTrigger(DatabaseName, CollectionName, CreateLeaseContainerIfNotExists = true, LeaseContainerPrefix = "ciTriggerWithString")] string documents,
ILogger log)
{
foreach (var document in JArray.Parse(documents))
{
log.LogInformation("Trigger with string called!");
}
}
[FixedDelayRetry(5, "00:00:01")]
public static void TriggerWithRetry(
[CosmosDBTrigger(DatabaseName, CollectionName, CreateLeaseContainerIfNotExists = true, LeaseContainerPrefix = "ciTriggerWithRetry")] IReadOnlyList<Item> documents,
ILogger log)
{
foreach (var document in documents)
{
log.LogInformation($"Trigger with retry called!");
}
if (shouldThrow)
{
shouldThrow = false;
throw new Exception("Test exception");
}
}
}
private static class EndToEndCancellationTestClass
{
private static string firstDocumentId = null;
public static async Task Trigger(
[CosmosDBTrigger(
DatabaseName,
CollectionName,
CreateLeaseContainerIfNotExists = true,
LeaseContainerPrefix = "ciTriggerWithCancellation",
LeaseExpirationInterval = 20 * 1000,
LeaseRenewInterval = 5 * 1000,
FeedPollDelay = 500,
StartFromBeginning = true)]IReadOnlyList<Item> documents,
ILogger log,
CancellationToken cancellationToken)
{
log.LogInformation("Trigger called!");
if (firstDocumentId == null)
{
// The first time around, make a note of the first item's ID
firstDocumentId = documents[0].Id;
try
{
// Use a delay to simulate processing
await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
}
catch (TaskCanceledException)
{
log.LogWarning("Trigger canceled!");
throw;
}
}
else if (documents.Any(document => document.Id == firstDocumentId))
{
// Log a message if we see the first item from the earlier delay again
log.LogInformation("Saw the first document again!");
}
}
}
}
}