|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.IO; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Text.Json.Serialization; |
| 7 | +using Microsoft.Azure.Functions.Worker; |
| 8 | +using Microsoft.Azure.Functions.Worker.Http; |
| 9 | +using Microsoft.DurableTask; |
| 10 | +using Microsoft.DurableTask.Client; |
| 11 | +using Microsoft.DurableTask.Entities; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | + |
| 14 | +namespace AzureFunctionsSmokeTests; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Input payload for the generated orchestration scenario. |
| 18 | +/// </summary> |
| 19 | +/// <param name="Name">The name to use when composing greetings.</param> |
| 20 | +public record GeneratorRequest([property: JsonPropertyName("name")] string? Name); |
| 21 | + |
| 22 | +/// <summary> |
| 23 | +/// Output payload for the generated orchestration scenario. |
| 24 | +/// </summary> |
| 25 | +/// <param name="Greeting">The greeting text created by the activity function.</param> |
| 26 | +/// <param name="GreetingLength">The length of the generated greeting.</param> |
| 27 | +/// <param name="CounterTotal">The current total stored in the entity.</param> |
| 28 | +/// <param name="ChildMessage">The response returned by the child orchestrator.</param> |
| 29 | +/// <param name="EventMessage">The message carried by the durable event.</param> |
| 30 | +public record GeneratorResult( |
| 31 | + [property: JsonPropertyName("greeting")] string Greeting, |
| 32 | + [property: JsonPropertyName("greetingLength")] int GreetingLength, |
| 33 | + [property: JsonPropertyName("counterTotal")] int CounterTotal, |
| 34 | + [property: JsonPropertyName("childMessage")] string ChildMessage, |
| 35 | + [property: JsonPropertyName("eventMessage")] string EventMessage); |
| 36 | + |
| 37 | +/// <summary> |
| 38 | +/// Durable event payload used by the generated orchestration. |
| 39 | +/// </summary> |
| 40 | +/// <param name="Message">The event message.</param> |
| 41 | +[DurableEvent("GeneratorSignal")] |
| 42 | +public record GeneratorSignal([property: JsonPropertyName("message")] string Message); |
| 43 | + |
| 44 | +/// <summary> |
| 45 | +/// Entity state used by <see cref="GeneratorCounter"/>. |
| 46 | +/// </summary> |
| 47 | +public sealed class GeneratorCounterState |
| 48 | +{ |
| 49 | + /// <summary> |
| 50 | + /// Gets or sets the running total tracked by the entity. |
| 51 | + /// </summary> |
| 52 | + public int Count { get; set; } |
| 53 | +} |
| 54 | + |
| 55 | +/// <summary> |
| 56 | +/// Entity implementation used to validate source generator entity trigger output. |
| 57 | +/// </summary> |
| 58 | +[DurableTask(nameof(GeneratorCounter))] |
| 59 | +public sealed class GeneratorCounter : TaskEntity<GeneratorCounterState> |
| 60 | +{ |
| 61 | + /// <summary> |
| 62 | + /// Increments the counter by the specified amount. |
| 63 | + /// </summary> |
| 64 | + /// <param name="context">The task entity context.</param> |
| 65 | + /// <param name="amount">The amount to add.</param> |
| 66 | + public void Add(TaskEntityContext context, int amount) |
| 67 | + { |
| 68 | + this.State.Count += amount; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets the current counter value. |
| 73 | + /// </summary> |
| 74 | + /// <returns>The current counter total.</returns> |
| 75 | + public int GetCount() |
| 76 | + { |
| 77 | + return this.State.Count; |
| 78 | + } |
| 79 | + |
| 80 | + /// <inheritdoc/> |
| 81 | + protected override GeneratorCounterState InitializeState(TaskEntityOperation entityOperation) |
| 82 | + { |
| 83 | + return new GeneratorCounterState(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// <summary> |
| 88 | +/// Activity used to validate source generator activity trigger output. |
| 89 | +/// </summary> |
| 90 | +[DurableTask(nameof(CountCharactersActivity))] |
| 91 | +public sealed class CountCharactersActivity : TaskActivity<string, int> |
| 92 | +{ |
| 93 | + /// <inheritdoc/> |
| 94 | + public override Task<int> RunAsync(TaskActivityContext context, string input) |
| 95 | + { |
| 96 | + return Task.FromResult(input?.Length ?? 0); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// <summary> |
| 101 | +/// Child orchestrator used to validate generated sub-orchestration call methods. |
| 102 | +/// </summary> |
| 103 | +[DurableTask(nameof(ChildGeneratedOrchestration))] |
| 104 | +public sealed class ChildGeneratedOrchestration : TaskOrchestrator<int, string> |
| 105 | +{ |
| 106 | + /// <inheritdoc/> |
| 107 | + public override Task<string> RunAsync(TaskOrchestrationContext context, int input) |
| 108 | + { |
| 109 | + return Task.FromResult($"Child processed {input}"); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// <summary> |
| 114 | +/// Primary orchestration that exercises the Durable Task source generator output for Azure Functions. |
| 115 | +/// </summary> |
| 116 | +[DurableTask(nameof(GeneratedOrchestration))] |
| 117 | +public sealed class GeneratedOrchestration : TaskOrchestrator<GeneratorRequest?, GeneratorResult> |
| 118 | +{ |
| 119 | + internal const string DefaultName = "SourceGen"; |
| 120 | + |
| 121 | + /// <inheritdoc/> |
| 122 | + public override async Task<GeneratorResult> RunAsync(TaskOrchestrationContext context, GeneratorRequest? input) |
| 123 | + { |
| 124 | + string name = string.IsNullOrWhiteSpace(input?.Name) ? DefaultName : input!.Name!; |
| 125 | + |
| 126 | + // Function-based activity trigger call using generated extension. |
| 127 | + string greeting = await context.CallComposeGreetingAsync(name); |
| 128 | + |
| 129 | + // Class-based activity call using generated extension and activity trigger generated by source generator. |
| 130 | + int length = await context.CallCountCharactersActivityAsync(greeting); |
| 131 | + |
| 132 | + // Entity trigger generated by source generator. |
| 133 | + EntityInstanceId counterId = new EntityInstanceId(nameof(GeneratorCounter), context.InstanceId); |
| 134 | + await context.Entities.CallEntityAsync(counterId, "Add", length); |
| 135 | + int total = await context.Entities.CallEntityAsync<int>(counterId, "GetCount"); |
| 136 | + |
| 137 | + // Durable event extensions generated by source generator. |
| 138 | + context.SendGeneratorSignal(context.InstanceId, new GeneratorSignal($"Processed {name}")); |
| 139 | + GeneratorSignal confirmation = await context.WaitForGeneratorSignalAsync(); |
| 140 | + |
| 141 | + // Sub-orchestration call using generated extension methods. |
| 142 | + string childMessage = await context.CallChildGeneratedOrchestrationAsync(length); |
| 143 | + |
| 144 | + return new GeneratorResult(greeting, length, total, childMessage, confirmation.Message); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/// <summary> |
| 149 | +/// HTTP trigger and auxiliary functions used to start source generator scenarios. |
| 150 | +/// </summary> |
| 151 | +public static class GeneratorFunctions |
| 152 | +{ |
| 153 | + /// <summary> |
| 154 | + /// Composes a greeting string. Generates an activity trigger via source generators. |
| 155 | + /// </summary> |
| 156 | + /// <param name="name">The name to greet.</param> |
| 157 | + /// <returns>The greeting text.</returns> |
| 158 | + [Function(nameof(ComposeGreeting))] |
| 159 | + public static string ComposeGreeting([ActivityTrigger] string name) |
| 160 | + { |
| 161 | + return $"Hello, {name}!"; |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Starts the generated orchestration using a generated scheduling extension method. |
| 166 | + /// </summary> |
| 167 | + /// <param name="req">The HTTP request.</param> |
| 168 | + /// <param name="client">The durable client.</param> |
| 169 | + /// <param name="executionContext">The function execution context.</param> |
| 170 | + /// <returns>The HTTP response.</returns> |
| 171 | + [Function("GeneratedOrchestration_HttpStart")] |
| 172 | + public static async Task<HttpResponseData> StartGeneratedOrchestrationAsync( |
| 173 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req, |
| 174 | + [DurableClient] DurableTaskClient client, |
| 175 | + FunctionContext executionContext) |
| 176 | + { |
| 177 | + ILogger logger = executionContext.GetLogger("GeneratedOrchestration_HttpStart"); |
| 178 | + |
| 179 | + GeneratorRequest? request = await TryReadRequestAsync(req); |
| 180 | + string instanceId = await client.ScheduleNewGeneratedOrchestrationInstanceAsync( |
| 181 | + request ?? new GeneratorRequest(GeneratedOrchestration.DefaultName)); |
| 182 | + |
| 183 | + logger.LogInformation("Started generated orchestration with ID = '{InstanceId}'.", instanceId); |
| 184 | + return client.CreateCheckStatusResponse(req, instanceId); |
| 185 | + } |
| 186 | + |
| 187 | + static async Task<GeneratorRequest?> TryReadRequestAsync(HttpRequestData req) |
| 188 | + { |
| 189 | + if (req.Body.CanSeek) |
| 190 | + { |
| 191 | + req.Body.Seek(0, SeekOrigin.Begin); |
| 192 | + } |
| 193 | + |
| 194 | + using StreamReader reader = new(req.Body); |
| 195 | + string body = await reader.ReadToEndAsync(); |
| 196 | + if (string.IsNullOrWhiteSpace(body)) |
| 197 | + { |
| 198 | + return null; |
| 199 | + } |
| 200 | + |
| 201 | + try |
| 202 | + { |
| 203 | + return JsonSerializer.Deserialize<GeneratorRequest>(body); |
| 204 | + } |
| 205 | + catch (JsonException) |
| 206 | + { |
| 207 | + return null; |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments