Skip to content

Commit cabe839

Browse files
committed
Add scheduled start time E2E Test
1 parent 1a17e1a commit cabe839

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

test/e2e/Apps/BasicDotNetIsolated/HelloCities.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,27 @@ public static async Task<HttpResponseData> HttpStart(
5454
// See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration
5555
return await client.CreateCheckStatusResponseAsync(req, instanceId);
5656
}
57+
58+
[Function("HelloCities_HttpStart_Scheduled")]
59+
public static async Task<HttpResponseData> HttpStartScheduled(
60+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
61+
[DurableClient] DurableTaskClient client,
62+
FunctionContext executionContext,
63+
DateTime ScheduledStartTime)
64+
{
65+
ILogger logger = executionContext.GetLogger("HelloCities_HttpStart");
66+
67+
var startOptions = new StartOrchestrationOptions(StartAt: ScheduledStartTime);
68+
69+
// Function input comes from the request content.
70+
string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(
71+
nameof(HelloCities), startOptions);
72+
73+
logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);
74+
75+
// Returns an HTTP 202 response with an instance management payload.
76+
// See https://learn.microsoft.com/azure/azure-functions/durable/durable-functions-http-api#start-orchestration
77+
return await client.CreateCheckStatusResponseAsync(req, instanceId);
78+
}
5779
}
5880
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
using System.Text.Json.Nodes;
5+
6+
namespace Microsoft.Azure.Durable.Tests.DotnetIsolatedE2E;
7+
8+
internal class DurableHelpers
9+
{
10+
internal static string ParseStatusQueryGetUri(HttpResponseMessage invocationStartResponse)
11+
{
12+
string? responseString = invocationStartResponse.Content?.ReadAsStringAsync().Result;
13+
if (string.IsNullOrEmpty(responseString))
14+
{
15+
return string.Empty;
16+
}
17+
JsonNode? responseJsonNode = JsonNode.Parse(responseString);
18+
if (responseJsonNode == null)
19+
{
20+
return string.Empty;
21+
}
22+
23+
string? statusQueryGetUri = responseJsonNode["StatusQueryGetUri"]?.GetValue<string>();
24+
return statusQueryGetUri ?? string.Empty;
25+
}
26+
internal static string GetRuntimeStatus(string statusQueryGetUri)
27+
{
28+
HttpClient client = new HttpClient();
29+
var statusQueryResponse = client.GetAsync(statusQueryGetUri);
30+
31+
string? statusQueryResponseString = statusQueryResponse.Result.Content.ReadAsStringAsync().Result;
32+
JsonNode? statusQueryJsonNode = JsonNode.Parse(statusQueryResponseString);
33+
if (statusQueryJsonNode == null)
34+
{
35+
return string.Empty;
36+
}
37+
return statusQueryJsonNode["runtimeStatus"]?.GetValue<string>() ?? string.Empty;
38+
}
39+
}

test/e2e/Tests/Tests/HelloCitiesTest.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Microsoft.Azure.Durable.Tests.DotnetIsolatedE2E;
99

10-
[Collection(Constants.FunctionAppCollectionName )]
10+
[Collection(Constants.FunctionAppCollectionName)]
1111
public class HttpEndToEndTests
1212
{
1313
private readonly FunctionAppFixture _fixture;
@@ -19,17 +19,34 @@ public HttpEndToEndTests(FunctionAppFixture fixture, ITestOutputHelper testOutpu
1919
}
2020

2121
[Theory]
22-
[InlineData("HelloCities_HttpStart", "", HttpStatusCode.Accepted, "")]
23-
public async Task HttpTriggerTests(string functionName, string queryString, HttpStatusCode expectedStatusCode, string expectedMessage)
22+
[InlineData("HelloCities_HttpStart", HttpStatusCode.Accepted)]
23+
public async Task HttpTriggerTests(string functionName, HttpStatusCode expectedStatusCode)
2424
{
25-
using HttpResponseMessage response = await HttpHelpers.InvokeHttpTrigger(functionName, queryString);
25+
using HttpResponseMessage response = await HttpHelpers.InvokeHttpTrigger(functionName, "");
2626
string actualMessage = await response.Content.ReadAsStringAsync();
2727

2828
Assert.Equal(expectedStatusCode, response.StatusCode);
29+
Assert.False(string.IsNullOrEmpty(actualMessage));
30+
}
31+
32+
[Theory]
33+
[InlineData("HelloCities_HttpStart_Scheduled", HttpStatusCode.Accepted)]
34+
public async Task ScheduledStartTests(string functionName, HttpStatusCode expectedStatusCode)
35+
{
36+
var scheduledStartDate = DateTime.Now + TimeSpan.FromSeconds(10);
37+
38+
using HttpResponseMessage response = await HttpHelpers.InvokeHttpTrigger(functionName, $"?ScheduledStartTime={scheduledStartDate.ToString("o")}");
39+
string actualMessage = await response.Content.ReadAsStringAsync();
40+
41+
string statusQueryGetUri = DurableHelpers.ParseStatusQueryGetUri(response);
42+
43+
Assert.Equal(expectedStatusCode, response.StatusCode);
44+
45+
string startRuntimeStatus = DurableHelpers.GetRuntimeStatus(statusQueryGetUri);
46+
Assert.Equal("Pending", startRuntimeStatus);
47+
Thread.Sleep(11000);
2948

30-
if (!string.IsNullOrEmpty(expectedMessage))
31-
{
32-
Assert.False(string.IsNullOrEmpty(actualMessage));
33-
}
49+
string endRuntimeStatus = DurableHelpers.GetRuntimeStatus(statusQueryGetUri);
50+
Assert.Equal("Completed", endRuntimeStatus);
3451
}
3552
}

0 commit comments

Comments
 (0)