|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +namespace Azure.Functions.PowerShell.Tests.E2E |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Net; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Xunit; |
| 10 | + |
| 11 | + using System.Net.Http; |
| 12 | + using Newtonsoft.Json; |
| 13 | + |
| 14 | + [Collection(Constants.FunctionAppCollectionName)] |
| 15 | + public class DurableEndToEndTests |
| 16 | + { |
| 17 | + private readonly FunctionAppFixture _fixture; |
| 18 | + |
| 19 | + public DurableEndToEndTests(FunctionAppFixture fixture) |
| 20 | + { |
| 21 | + this._fixture = fixture; |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public async Task DurableClientFollowsAsyncPattern() |
| 26 | + { |
| 27 | + var initialResponse = await Utilities.GetHttpTriggerResponse("DurableClient", queryString: string.Empty); |
| 28 | + Assert.Equal(HttpStatusCode.Accepted, initialResponse.StatusCode); |
| 29 | + |
| 30 | + var location = initialResponse.Headers.Location; |
| 31 | + Assert.NotNull(location); |
| 32 | + |
| 33 | + var initialResponseBody = await initialResponse.Content.ReadAsStringAsync(); |
| 34 | + dynamic initialResponseBodyObject = JsonConvert.DeserializeObject(initialResponseBody); |
| 35 | + Assert.NotNull(initialResponseBodyObject.id); |
| 36 | + var statusQueryGetUri = (string)initialResponseBodyObject.statusQueryGetUri; |
| 37 | + Assert.Equal(location.ToString(), statusQueryGetUri); |
| 38 | + Assert.NotNull(initialResponseBodyObject.sendEventPostUri); |
| 39 | + Assert.NotNull(initialResponseBodyObject.purgeHistoryDeleteUri); |
| 40 | + Assert.NotNull(initialResponseBodyObject.terminatePostUri); |
| 41 | + Assert.NotNull(initialResponseBodyObject.rewindPostUri); |
| 42 | + |
| 43 | + var orchestrationCompletionTimeout = TimeSpan.FromSeconds(60); |
| 44 | + var startTime = DateTime.UtcNow; |
| 45 | + |
| 46 | + using (var httpClient = new HttpClient()) |
| 47 | + { |
| 48 | + while (true) |
| 49 | + { |
| 50 | + var statusResponse = await httpClient.GetAsync(statusQueryGetUri); |
| 51 | + switch (statusResponse.StatusCode) |
| 52 | + { |
| 53 | + case HttpStatusCode.Accepted: |
| 54 | + { |
| 55 | + var statusResponseBody = await GetResponseBodyAsync(statusResponse); |
| 56 | + var runtimeStatus = (string)statusResponseBody.runtimeStatus; |
| 57 | + Assert.True( |
| 58 | + runtimeStatus == "Running" || runtimeStatus == "Pending", |
| 59 | + $"Unexpected runtime status: {runtimeStatus}"); |
| 60 | + |
| 61 | + if (DateTime.UtcNow > startTime + orchestrationCompletionTimeout) |
| 62 | + { |
| 63 | + Assert.True(false, $"The orchestration has not completed after {orchestrationCompletionTimeout}"); |
| 64 | + } |
| 65 | + |
| 66 | + await Task.Delay(TimeSpan.FromSeconds(2)); |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + case HttpStatusCode.OK: |
| 71 | + { |
| 72 | + var statusResponseBody = await GetResponseBodyAsync(statusResponse); |
| 73 | + Assert.Equal("Completed", (string)statusResponseBody.runtimeStatus); |
| 74 | + Assert.Equal("Hello Tokyo", statusResponseBody.output[0].ToString()); |
| 75 | + Assert.Equal("Hello Seattle", statusResponseBody.output[1].ToString()); |
| 76 | + Assert.Equal("Hello London", statusResponseBody.output[2].ToString()); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + default: |
| 81 | + Assert.True(false, $"Unexpected orchestration status code: {statusResponse.StatusCode}"); |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static async Task<dynamic> GetResponseBodyAsync(HttpResponseMessage response) |
| 89 | + { |
| 90 | + var responseBody = await response.Content.ReadAsStringAsync(); |
| 91 | + return JsonConvert.DeserializeObject(responseBody); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments