|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.Azure.Functions.Worker.Sdk.Generators; |
| 8 | +using Microsoft.CodeAnalysis.CSharp; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Microsoft.Azure.Functions.SdkGeneratorTests |
| 12 | +{ |
| 13 | + public partial class FunctionMetadataProviderGeneratorTests |
| 14 | + { |
| 15 | + public class ServiceBusTests |
| 16 | + { |
| 17 | + private readonly Assembly[] _referencedExtensionAssemblies; |
| 18 | + |
| 19 | + public ServiceBusTests() |
| 20 | + { |
| 21 | + // load all extensions used in tests (match extensions tested on E2E app? Or include ALL extensions?) |
| 22 | + var abstractionsExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Abstractions.dll"); |
| 23 | + var sbExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.ServiceBus.dll"); |
| 24 | + var hostingExtension = Assembly.LoadFrom("Microsoft.Extensions.Hosting.dll"); |
| 25 | + var diExtension = Assembly.LoadFrom("Microsoft.Extensions.DependencyInjection.dll"); |
| 26 | + var hostingAbExtension = Assembly.LoadFrom("Microsoft.Extensions.Hosting.Abstractions.dll"); |
| 27 | + var diAbExtension = Assembly.LoadFrom("Microsoft.Extensions.DependencyInjection.Abstractions.dll"); |
| 28 | + var blobExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs.dll"); |
| 29 | + var azSb = Assembly.LoadFrom("Azure.Messaging.ServiceBus.dll"); |
| 30 | + |
| 31 | + _referencedExtensionAssemblies = new[] |
| 32 | + { |
| 33 | + abstractionsExtension, |
| 34 | + blobExtension, |
| 35 | + sbExtension, |
| 36 | + azSb, |
| 37 | + hostingExtension, |
| 38 | + hostingAbExtension, |
| 39 | + diExtension, |
| 40 | + diAbExtension |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [InlineData(LanguageVersion.CSharp7_3)] |
| 46 | + [InlineData(LanguageVersion.CSharp8)] |
| 47 | + [InlineData(LanguageVersion.CSharp9)] |
| 48 | + [InlineData(LanguageVersion.CSharp10)] |
| 49 | + [InlineData(LanguageVersion.CSharp11)] |
| 50 | + [InlineData(LanguageVersion.Latest)] |
| 51 | + public async Task ServiceBusWithSessionsEnabled(LanguageVersion languageVersion) |
| 52 | + { |
| 53 | + // test generating function metadata for a simple HttpTrigger |
| 54 | + string inputCode = """ |
| 55 | + using System; |
| 56 | + using System.Threading.Tasks; |
| 57 | + using Azure.Messaging.ServiceBus; |
| 58 | + using Microsoft.Azure.Functions.Worker; |
| 59 | + using Microsoft.Extensions.Logging; |
| 60 | +
|
| 61 | + namespace SampleApp |
| 62 | + { |
| 63 | + public static class TimerFunction |
| 64 | + { |
| 65 | + [Function("ServiceBusFunction")] |
| 66 | + public static void Run([ServiceBusTrigger("queue", Connection = "ServiceBusConnection", IsSessionsEnabled = true)] |
| 67 | + ServiceBusReceivedMessage message, |
| 68 | + ServiceBusMessageActions messageActions) |
| 69 | + { |
| 70 | + throw new NotImplementedException(); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + """; |
| 75 | + |
| 76 | + |
| 77 | + string expectedGeneratedFileName = $"GeneratedFunctionMetadataProvider.g.cs"; |
| 78 | + string expectedOutput = """ |
| 79 | + // <auto-generated/> |
| 80 | + using System; |
| 81 | + using System.Collections.Generic; |
| 82 | + using System.Collections.Immutable; |
| 83 | + using System.Text.Json; |
| 84 | + using System.Threading.Tasks; |
| 85 | + using Microsoft.Azure.Functions.Worker; |
| 86 | + using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata; |
| 87 | + using Microsoft.Extensions.DependencyInjection; |
| 88 | + using Microsoft.Extensions.Hosting; |
| 89 | + |
| 90 | + namespace TestProject |
| 91 | + { |
| 92 | + /// <summary> |
| 93 | + /// Custom <see cref="IFunctionMetadataProvider"/> implementation that returns function metadata definitions for the current worker."/> |
| 94 | + /// </summary> |
| 95 | + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)] |
| 96 | + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] |
| 97 | + public class GeneratedFunctionMetadataProvider : IFunctionMetadataProvider |
| 98 | + { |
| 99 | + /// <inheritdoc/> |
| 100 | + public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string directory) |
| 101 | + { |
| 102 | + var metadataList = new List<IFunctionMetadata>(); |
| 103 | + var Function0RawBindings = new List<string>(); |
| 104 | + Function0RawBindings.Add(@"{""name"":""message"",""type"":""serviceBusTrigger"",""direction"":""In"",""properties"":{""supportsDeferredBinding"":""True""},""queueName"":""queue"",""connection"":""ServiceBusConnection"",""isSessionsEnabled"":true,""cardinality"":""One""}"); |
| 105 | +
|
| 106 | + var Function0 = new DefaultFunctionMetadata |
| 107 | + { |
| 108 | + Language = "dotnet-isolated", |
| 109 | + Name = "ServiceBusFunction", |
| 110 | + EntryPoint = "SampleApp.TimerFunction.Run", |
| 111 | + RawBindings = Function0RawBindings, |
| 112 | + ScriptFile = "TestProject.dll" |
| 113 | + }; |
| 114 | + metadataList.Add(Function0); |
| 115 | + |
| 116 | + return Task.FromResult(metadataList.ToImmutableArray()); |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | + /// <summary> |
| 121 | + /// Extension methods to enable registration of the custom <see cref="IFunctionMetadataProvider"/> implementation generated for the current worker. |
| 122 | + /// </summary> |
| 123 | + public static class WorkerHostBuilderFunctionMetadataProviderExtension |
| 124 | + { |
| 125 | + ///<summary> |
| 126 | + /// Adds the GeneratedFunctionMetadataProvider to the service collection. |
| 127 | + /// During initialization, the worker will return generated function metadata instead of relying on the Azure Functions host for function indexing. |
| 128 | + ///</summary> |
| 129 | + public static IHostBuilder ConfigureGeneratedFunctionMetadataProvider(this IHostBuilder builder) |
| 130 | + { |
| 131 | + builder.ConfigureServices(s => |
| 132 | + { |
| 133 | + s.AddSingleton<IFunctionMetadataProvider, GeneratedFunctionMetadataProvider>(); |
| 134 | + }); |
| 135 | + return builder; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + """; |
| 140 | + |
| 141 | + await TestHelpers.RunTestAsync<FunctionMetadataProviderGenerator>( |
| 142 | + _referencedExtensionAssemblies, |
| 143 | + inputCode, |
| 144 | + expectedGeneratedFileName, |
| 145 | + expectedOutput, |
| 146 | + languageVersion: languageVersion); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments