|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. See License.txt in the project root for license information. |
3 | 3 |
|
4 | | -using System; |
5 | | -using System.Threading.Tasks; |
6 | 4 | using Microsoft.Azure.Cosmos; |
7 | 5 | using Microsoft.Azure.Storage.Queue; |
| 6 | +using System; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Xml.Linq; |
8 | 9 | using Xunit; |
9 | 10 |
|
10 | 11 | namespace Microsoft.Azure.WebJobs.Script.Tests.CosmosDB |
11 | 12 | { |
12 | 13 | public abstract class CosmosDBEndToEndTestsBase<TTestFixture> : |
13 | 14 | EndToEndTestsBase<TTestFixture> where TTestFixture : CosmosDBEndtoEndTestFixture, new() |
14 | 15 | { |
| 16 | + private CosmosDBEndtoEndTestFixture _fixture; |
| 17 | + |
15 | 18 | public CosmosDBEndToEndTestsBase(TTestFixture fixture) : base(fixture) |
16 | 19 | { |
| 20 | + _fixture = fixture; |
17 | 21 | } |
18 | 22 |
|
19 | 23 | protected async Task CosmosDBTriggerToBlobTest() |
20 | 24 | { |
21 | 25 | // Waiting for the Processor to acquire leases |
22 | | - await Task.Delay(10000); |
| 26 | + await Task.Delay(5000); |
| 27 | + |
| 28 | + var dbName = "TriggerItemDb"; |
| 29 | + await SetUpTriggerListener(); |
23 | 30 |
|
24 | | - bool collectionsCreated = await Fixture.CreateContainers(); |
25 | | - var resultBlob = Fixture.TestOutputContainer.GetBlockBlobReference("cosmosdbtriggere2e-completed"); |
| 31 | + var resultBlob = _fixture.TestOutputContainer.GetBlockBlobReference("cosmosdbtriggere2e-completed"); |
26 | 32 | await resultBlob.DeleteIfExistsAsync(); |
27 | 33 |
|
28 | 34 | string id = Guid.NewGuid().ToString(); |
29 | 35 |
|
30 | 36 | var documentToTest = new { id }; |
31 | 37 |
|
32 | | - await Fixture.CosmosClient.GetContainer("ItemDb", "ItemCollection") |
| 38 | + await _fixture.CosmosClient.GetContainer(dbName, "ItemCollection") |
33 | 39 | .CreateItemAsync(documentToTest, new PartitionKey(id)); |
34 | 40 |
|
35 | 41 | // now wait for function to be invoked |
36 | 42 | string result = await TestHelpers.WaitForBlobAndGetStringAsync(resultBlob, |
37 | | - () => string.Join(Environment.NewLine, Fixture.Host.GetScriptHostLogMessages())); |
38 | | - |
39 | | - if (collectionsCreated) |
40 | | - { |
41 | | - // cleanup collections |
42 | | - await Fixture.DeleteContainers(); |
43 | | - } |
| 43 | + () => string.Join(Environment.NewLine, _fixture.Host.GetScriptHostLogMessages())); |
44 | 44 |
|
45 | 45 | Assert.False(string.IsNullOrEmpty(result)); |
| 46 | + |
| 47 | + await _fixture.DeleteCosmosDbResources(dbName); |
46 | 48 | } |
47 | 49 |
|
48 | 50 | protected async Task CosmosDBTest() |
49 | 51 | { |
50 | | - bool collectionsCreated = await Fixture.CreateContainers(); |
51 | | - string id = Guid.NewGuid().ToString(); |
| 52 | + var dbName = "InOutItemDb"; |
| 53 | + await _fixture.CreateContainers(dbName); |
| 54 | + await SetUpTriggerListener(); |
52 | 55 |
|
53 | | - await Fixture.Host.BeginFunctionAsync("CosmosDBOut", id); |
54 | | - |
55 | | - dynamic doc = await WaitForItemAsync(id); |
| 56 | + string id = Guid.NewGuid().ToString(); |
| 57 | + await _fixture.Host.BeginFunctionAsync("CosmosDBOut", id); |
56 | 58 |
|
| 59 | + dynamic doc = await WaitForItemAsync(id, dbName); |
57 | 60 | Assert.Equal((string)doc.id, id); |
58 | 61 |
|
59 | | - var queue = await Fixture.GetNewQueue("documentdb-input"); |
| 62 | + var queue = await _fixture.GetNewQueue("cosmosdb-input"); |
60 | 63 | string messageContent = string.Format("{{ \"id\": \"{0}\" }}", id); |
61 | 64 | await queue.AddMessageAsync(new CloudQueueMessage(messageContent)); |
62 | 65 |
|
63 | 66 | // And wait for the text to be updated |
64 | | - dynamic updatedDoc = await WaitForItemAsync(id, "This was updated!"); |
| 67 | + dynamic updatedDoc = await WaitForItemAsync(id, dbName, "This was updated!"); |
65 | 68 |
|
66 | 69 | Assert.Equal(updatedDoc.id, doc.id); |
67 | 70 | Assert.NotEqual(doc._etag, updatedDoc._etag); |
| 71 | + |
| 72 | + await _fixture.DeleteCosmosDbResources(dbName); |
68 | 73 | } |
69 | 74 |
|
70 | 75 | protected async Task CosmosDBMultipleItemsTest() |
71 | 76 | { |
72 | | - bool collectionsCreated = await Fixture.CreateContainers(); |
73 | | - var resultBlob = Fixture.TestOutputContainer.GetBlockBlobReference("cosmosdbin-multiple-e2e-completed"); |
74 | | - await resultBlob.DeleteIfExistsAsync(); |
| 77 | + var dbName = "MultipleInOutItemDb"; |
| 78 | + await _fixture.CreateContainers(dbName); |
| 79 | + await SetUpTriggerListener(); |
75 | 80 |
|
76 | 81 | string id = Guid.NewGuid().ToString(); |
77 | | - |
78 | | - await Fixture.Host.BeginFunctionAsync("CosmosDBOutMultiple", id); |
79 | | - |
| 82 | + await _fixture.Host.BeginFunctionAsync("CosmosDBOutMultiple", id); |
80 | 83 | var testId = id + "-0"; |
81 | | - dynamic doc = await WaitForItemAsync(testId); |
| 84 | + dynamic doc = await WaitForItemAsync(testId, dbName); |
82 | 85 |
|
83 | | - var queue = await Fixture.GetNewQueue("documentdb-input"); |
| 86 | + var queue = await _fixture.GetNewQueue("cosmosdb-input-multiple"); |
84 | 87 | string messageContent = string.Format("{{ \"id\": \"{0}\" }}", id); |
85 | 88 | await queue.AddMessageAsync(new CloudQueueMessage(messageContent)); |
86 | 89 |
|
87 | 90 | // And wait for the text to be updated |
88 | | - dynamic updatedDoc = await WaitForItemAsync(id, "Hello from Node with multiple input bindings!"); |
89 | | - } |
90 | | - |
91 | | - protected async Task TestConnectToEmulator() |
92 | | - { |
93 | | - bool collectionsCreated = await Fixture.CreateContainers(); |
94 | | - |
95 | | - var container = Fixture.CosmosClient.GetContainer("ItemDb", "ItemCollection"); |
96 | | - |
97 | | - string id = Guid.NewGuid().ToString(); |
98 | | - var documentToTest = new { id, text = "connect to emulator test" }; |
99 | | - |
100 | | - await container.CreateItemAsync(documentToTest, new PartitionKey(id)); |
101 | | - dynamic doc = await WaitForItemAsync(id); |
| 91 | + dynamic updatedDoc = await WaitForItemAsync(id, dbName, "Hello from Node with multiple input bindings!"); |
102 | 92 |
|
103 | | - Assert.Equal((string)doc.id, id); |
104 | | - Assert.Equal((string)doc.text, "connect to emulator test"); |
| 93 | + await _fixture.DeleteCosmosDbResources(dbName); |
105 | 94 | } |
106 | | - |
107 | 95 |
|
108 | | - protected async Task<dynamic> WaitForItemAsync(string itemId, string textToMatch = null) |
| 96 | + protected async Task<dynamic> WaitForItemAsync(string itemId, string itemDb, string textToMatch = null) |
109 | 97 | { |
110 | | - var container = Fixture.CosmosClient.GetContainer("ItemDb", "ItemCollection"); |
| 98 | + var container = _fixture.CosmosClient.GetContainer(itemDb, "ItemCollection"); |
111 | 99 |
|
112 | 100 | dynamic document = null; |
113 | 101 |
|
@@ -139,11 +127,24 @@ await TestHelpers.Await(async () => |
139 | 127 | }, |
140 | 128 | userMessageCallback: () => |
141 | 129 | { |
142 | | - var logs = string.Join(Environment.NewLine, Fixture.Host.GetScriptHostLogMessages()); |
| 130 | + var logs = string.Join(Environment.NewLine, _fixture.Host.GetScriptHostLogMessages()); |
143 | 131 | return logs; |
144 | 132 | }); |
145 | 133 |
|
146 | 134 | return document; |
147 | 135 | } |
| 136 | + |
| 137 | + // Regardless of which function is being tested, the trigger listener needs to be set up or the test host fails |
| 138 | + private async Task SetUpTriggerListener() |
| 139 | + { |
| 140 | + var dbName = "TriggerItemDb"; |
| 141 | + bool collectionsCreated = await _fixture.CreateContainers(dbName); |
| 142 | + } |
| 143 | + |
| 144 | + private async Task RemoveTriggerDb() |
| 145 | + { |
| 146 | + var dbName = "TriggerItemDb"; |
| 147 | + await _fixture.DeleteCosmosDbResources(dbName); |
| 148 | + } |
148 | 149 | } |
149 | 150 | } |
0 commit comments