diff --git a/src/modules/Elsa.Hosting.Management/AssemblyInfo.cs b/src/modules/Elsa.Hosting.Management/AssemblyInfo.cs new file mode 100644 index 000000000..487b06135 --- /dev/null +++ b/src/modules/Elsa.Hosting.Management/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Elsa.Hosting.Management.UnitTests")] diff --git a/src/modules/Elsa.Hosting.Management/Services/ConfiguredApplicationInstanceNameProvider.cs b/src/modules/Elsa.Hosting.Management/Services/ConfiguredApplicationInstanceNameProvider.cs index dd44c96d5..2fb7ef941 100644 --- a/src/modules/Elsa.Hosting.Management/Services/ConfiguredApplicationInstanceNameProvider.cs +++ b/src/modules/Elsa.Hosting.Management/Services/ConfiguredApplicationInstanceNameProvider.cs @@ -21,9 +21,9 @@ namespace Elsa.Hosting.Management.Services; /// public class ConfiguredApplicationInstanceNameProvider : IApplicationInstanceNameProvider { - private const int AzureServiceBusSubscriptionNameMaxLength = 50; - private const string TriggerChangeTokenSignalEndpointNameSuffix = "-elsa-trigger-change-token-signal"; - private static readonly int ConfiguredInstanceNameMaxLength = AzureServiceBusSubscriptionNameMaxLength - TriggerChangeTokenSignalEndpointNameSuffix.Length; + internal const int AzureServiceBusSubscriptionNameMaxLength = 50; + internal const string TriggerChangeTokenSignalEndpointNameSuffix = "-elsa-trigger-change-token-signal"; + internal static readonly int ConfiguredInstanceNameMaxLength = AzureServiceBusSubscriptionNameMaxLength - TriggerChangeTokenSignalEndpointNameSuffix.Length; private readonly string _instanceName; @@ -89,6 +89,9 @@ public class ConfiguredApplicationInstanceNameProvider : IApplicationInstanceNam private static bool IsValidConfiguredInstanceName(string instanceName) { + if (instanceName.Length == 0) + return false; + return IsAsciiLetterOrDigit(instanceName[0]) && IsAsciiLetterOrDigit(instanceName[^1]) && instanceName.All(c => IsAsciiLetterOrDigit(c) || c is '.' or '-' or '_'); diff --git a/test/unit/Elsa.Hosting.Management.UnitTests/Services/ConfiguredApplicationInstanceNameProviderTests.cs b/test/unit/Elsa.Hosting.Management.UnitTests/Services/ConfiguredApplicationInstanceNameProviderTests.cs index 4446df2fe..901496b8e 100644 --- a/test/unit/Elsa.Hosting.Management.UnitTests/Services/ConfiguredApplicationInstanceNameProviderTests.cs +++ b/test/unit/Elsa.Hosting.Management.UnitTests/Services/ConfiguredApplicationInstanceNameProviderTests.cs @@ -1,17 +1,20 @@ +using Elsa.Features.Services; using Elsa.Hosting.Management.Contracts; using Elsa.Hosting.Management.Options; using Elsa.Hosting.Management.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; +using NSubstitute; +using ClusteringFeature = Elsa.Hosting.Management.Features.ClusteringFeature; using ShellClusteringFeature = Elsa.Hosting.Management.ShellFeatures.ClusteringFeature; namespace Elsa.Hosting.Management.UnitTests.Services; public class ConfiguredApplicationInstanceNameProviderTests { - private const int AzureServiceBusSubscriptionNameMaxLength = 50; - private const string TriggerChangeTokenSignalEndpointNameSuffix = "-elsa-trigger-change-token-signal"; - private static readonly int ConfiguredInstanceNameMaxLength = AzureServiceBusSubscriptionNameMaxLength - TriggerChangeTokenSignalEndpointNameSuffix.Length; + private static int AzureServiceBusSubscriptionNameMaxLength => ConfiguredApplicationInstanceNameProvider.AzureServiceBusSubscriptionNameMaxLength; + private static string TriggerChangeTokenSignalEndpointNameSuffix => ConfiguredApplicationInstanceNameProvider.TriggerChangeTokenSignalEndpointNameSuffix; + private static int ConfiguredInstanceNameMaxLength => ConfiguredApplicationInstanceNameProvider.ConfiguredInstanceNameMaxLength; [Fact] public void ExplicitInstanceName_IsUsedDirectly() @@ -217,6 +220,29 @@ public class ConfiguredApplicationInstanceNameProviderTests Assert.Equal("pod-0", provider.GetName()); } + [Fact] + public void ClusteringFeature_UsesConfiguredInstanceNameProvider() + { + var services = new ServiceCollection(); + var module = Substitute.For(); + module.Services.Returns(services); + + var feature = new ClusteringFeature(module) + { + ApplicationInstanceOptions = options => options.InstanceName = "pod-0" + }; + + services.AddLogging(); + feature.Apply(); + + using var serviceProvider = services.BuildServiceProvider(); + + var provider = serviceProvider.GetRequiredService(); + + Assert.IsType(provider); + Assert.Equal("pod-0", provider.GetName()); + } + private static ConfiguredApplicationInstanceNameProvider CreateProvider(ApplicationInstanceOptions options) { return new ConfiguredApplicationInstanceNameProvider(