diff --git a/src/NServiceBus.AcceptanceTests/HostInformation/When_feature_overrides_hostid.cs b/src/NServiceBus.AcceptanceTests/HostInformation/When_feature_overrides_hostid.cs new file mode 100644 index 00000000000..81cff1312e7 --- /dev/null +++ b/src/NServiceBus.AcceptanceTests/HostInformation/When_feature_overrides_hostid.cs @@ -0,0 +1,87 @@ +namespace NServiceBus.AcceptanceTests.HostInformation +{ + using System; + using System.Collections.Concurrent; + using System.Reflection; + using NServiceBus.AcceptanceTesting; + using NServiceBus.AcceptanceTests.EndpointTemplates; + using NServiceBus.Features; + using NUnit.Framework; + + public class When_feature_overrides_hostid : NServiceBusAcceptanceTest + { + + [Test] + public void MD5_should_not_be_used() + { + var context = new Context(); + + Scenario.Define(context) + .WithEndpoint(e => e.Given(b => b.SendLocal(new MyMessage()))) + .Done(c => c.Done) + .Run(); + + Assert.IsTrue(context.NotSet); + } + + public class MyEndpoint : EndpointConfigurationBuilder + { + public MyEndpoint() + { + EndpointSetup(c => c.UniquelyIdentifyRunningInstance().UsingCustomIdentifier(Guid.NewGuid())); + } + } + + public class MyFeatureThatOverridesHostInformationDefaults : Feature + { + bool notSet; + + public MyFeatureThatOverridesHostInformationDefaults() + { + EnableByDefault(); + DependsOn("UnicastBus"); + Defaults(s => + { + // remove the override, we need to hack it via reflection! + var fieldInfo = s.GetType().GetField("Overrides", BindingFlags.Instance | BindingFlags.NonPublic); + var dictionary = (ConcurrentDictionary)fieldInfo.GetValue(s); + object s2; + dictionary.TryRemove("NServiceBus.HostInformation.HostId", out s2); + + // Try to get value, setting should not exist + notSet = !s.HasSetting("NServiceBus.HostInformation.HostId"); + + // Set override again so we have something + s.Set("NServiceBus.HostInformation.HostId", Guid.NewGuid()); + + }); + } + + protected override void Setup(FeatureConfigurationContext context) + { + context.Container.ConfigureProperty(c => c.NotSet, notSet); + } + } + + public class MyMessageHandler : IHandleMessages + { + public Context Context { get; set; } + + public void Handle(MyMessage message) + { + Context.Done = true; + } + } + + public class Context : ScenarioContext + { + public bool NotSet { get; set; } + public bool Done { get; set; } + } + + [Serializable] + public class MyMessage : ICommand + { + } + } +} diff --git a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj index b4157f87638..40b75cdb1b0 100644 --- a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj +++ b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj @@ -83,6 +83,7 @@ + diff --git a/src/NServiceBus.Core.Tests/Hosting/When_creating_host_information_from_environment.cs b/src/NServiceBus.Core.Tests/Hosting/When_creating_host_information_from_environment.cs deleted file mode 100644 index e6ed9ce9a0d..00000000000 --- a/src/NServiceBus.Core.Tests/Hosting/When_creating_host_information_from_environment.cs +++ /dev/null @@ -1,61 +0,0 @@ -namespace NServiceBus.Hosting.Tests -{ - using System; - using NServiceBus.Unicast; - using NUnit.Framework; - - [TestFixture] - public class When_creating_host_information_from_environment - { - [Test] - public void HostId_is_parsed_from_path_without_spaces_but_with_quotes() - { - var information1 = new DefaultHostIdGenerator("\"pathto\\mysuperduper.exe\" somevar", "MyMachine"); - - Assert.IsTrue(information1.HostId == Guid.Parse("{8dd7bbcc-dfc3-d84a-41ac-82c14f1ed7db}")); - } - - [Test] - public void HostId_is_parsed_from_path_without_spaces_but_without_quotes() - { - var information2 = new DefaultHostIdGenerator("pathto\\mysuperduper.exe somevar", "MyMachine"); - - Assert.IsTrue(information2.HostId == Guid.Parse("{8dd7bbcc-dfc3-d84a-41ac-82c14f1ed7db}")); - } - - [Test] - public void Both_hostIds_for_paths_without_spaces_are_equal() - { - var information1 = new DefaultHostIdGenerator("\"pathto\\mysuperduper.exe\" somevar", "MyMachine"); - var information2 = new DefaultHostIdGenerator("pathto\\mysuperduper.exe somevar", "MyMachine"); - - Assert.IsTrue(information1.HostId == information2.HostId); - } - - [Test] - public void HostId_is_parsed_from_path_with_spaces_having_a_parameter_with_spaces() - { - var information3 = new DefaultHostIdGenerator("\"pathto\\mysuper duper.exe\" \"somevar with spaces\"", "MyMachine"); - - Assert.IsTrue(information3.HostId == Guid.Parse("{db3ff7ff-508f-cce7-0a8b-0092a7d750b9}")); - } - - [Test] - public void HostId_is_parsed_from_path_with_spaces_having_a_parameter_without_spaces() - { - var information4 = new DefaultHostIdGenerator("\"pathto\\mysuper duper.exe\" somevar", "MyMachine"); - - Assert.IsTrue(information4.HostId == Guid.Parse("{db3ff7ff-508f-cce7-0a8b-0092a7d750b9}")); - } - - [Test] - public void Both_hostIds_for_paths_with_spaces_are_equal() - { - var information3 = new DefaultHostIdGenerator("\"pathto\\mysuper duper.exe\" \"somevar with spaces\"", "MyMachine"); - var information4 = new DefaultHostIdGenerator("\"pathto\\mysuper duper.exe\" somevar", "MyMachine"); - - Assert.IsTrue(information3.HostId == information4.HostId); - } - - } -} diff --git a/src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj b/src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj index 521124db8f0..0849ddf827a 100644 --- a/src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj +++ b/src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj @@ -153,7 +153,6 @@ - @@ -260,6 +259,7 @@ + diff --git a/src/NServiceBus.Core.Tests/Unicast/Config/PathUtilities_Tests.cs b/src/NServiceBus.Core.Tests/Unicast/Config/PathUtilities_Tests.cs new file mode 100644 index 00000000000..069b88099a4 --- /dev/null +++ b/src/NServiceBus.Core.Tests/Unicast/Config/PathUtilities_Tests.cs @@ -0,0 +1,58 @@ +namespace NServiceBus.Features.Tests +{ + using NUnit.Framework; + + [TestFixture] + public class PathUtilities_Tests + { + [Test] + public void Parse_from_path_without_spaces_but_with_quotes() + { + var path = PathUtilities.SanitizedPath("\"pathto\\mysuperduper.exe\" somevar"); + + Assert.AreEqual("pathto\\mysuperduper.exe", path); + } + + [Test] + public void Parse_from_path_without_spaces_but_without_quotes() + { + var path = PathUtilities.SanitizedPath("pathto\\mysuperduper.exe somevar"); + + Assert.AreEqual("pathto\\mysuperduper.exe", path); + } + + [Test] + public void Paths_without_spaces_are_equal() + { + var path1 = PathUtilities.SanitizedPath("\"pathto\\mysuperduper.exe\" somevar"); + var path2 = PathUtilities.SanitizedPath("pathto\\mysuperduper.exe somevar"); + + Assert.AreEqual(path1, path2); + } + + [Test] + public void Parse_from_path_with_spaces_having_a_parameter_with_spaces() + { + var path = PathUtilities.SanitizedPath("\"pathto\\mysuper duper.exe\" \"somevar with spaces\""); + + Assert.AreEqual("pathto\\mysuper duper.exe", path); + } + + [Test] + public void Parse_from_path_with_spaces_having_a_parameter_without_spaces() + { + var path = PathUtilities.SanitizedPath("\"pathto\\mysuper duper.exe\" somevar"); + + Assert.AreEqual("pathto\\mysuper duper.exe", path); + } + + [Test] + public void Both_paths_with_spaces_are_equal() + { + var path1 = PathUtilities.SanitizedPath("\"pathto\\mysuper duper.exe\" \"somevar with spaces\""); + var path2 = PathUtilities.SanitizedPath("\"pathto\\mysuper duper.exe\" somevar"); + + Assert.AreEqual(path1, path2); + } + } +} diff --git a/src/NServiceBus.Core/NServiceBus.Core.csproj b/src/NServiceBus.Core/NServiceBus.Core.csproj index dc5b7a0bc94..4b1307aa147 100644 --- a/src/NServiceBus.Core/NServiceBus.Core.csproj +++ b/src/NServiceBus.Core/NServiceBus.Core.csproj @@ -244,13 +244,13 @@ + - diff --git a/src/NServiceBus.Core/Unicast/Config/PathUtilities.cs b/src/NServiceBus.Core/Unicast/Config/PathUtilities.cs new file mode 100644 index 00000000000..9e2c9708db0 --- /dev/null +++ b/src/NServiceBus.Core/Unicast/Config/PathUtilities.cs @@ -0,0 +1,19 @@ +namespace NServiceBus.Features +{ + using System.Linq; + using System.Text.RegularExpressions; + + internal static class PathUtilities + { + public static string SanitizedPath(string commandLine) + { + if (commandLine.StartsWith("\"")) + { + return (from Match match in Regex.Matches(commandLine, "\"([^\"]*)\"") + select match.ToString()).First().Trim('"'); + } + + return commandLine.Split(' ').First(); + } + } +} diff --git a/src/NServiceBus.Core/Unicast/Config/UnicastBus.cs b/src/NServiceBus.Core/Unicast/Config/UnicastBus.cs index 67b1a96c3f8..42119ae23b1 100644 --- a/src/NServiceBus.Core/Unicast/Config/UnicastBus.cs +++ b/src/NServiceBus.Core/Unicast/Config/UnicastBus.cs @@ -10,7 +10,7 @@ namespace NServiceBus.Features using Logging; using NServiceBus.Hosting; using NServiceBus.Support; - using NServiceBus.Unicast; + using NServiceBus.Utils; using Pipeline; using Pipeline.Contexts; using Transports; @@ -26,8 +26,12 @@ internal UnicastBus() Defaults(s => { - string fullPathToStartingExe; - s.SetDefault("NServiceBus.HostInformation.HostId", GenerateDefaultHostId(out fullPathToStartingExe)); + var fullPathToStartingExe = PathUtilities.SanitizedPath(Environment.CommandLine); + + if (!s.HasExplicitValue("NServiceBus.HostInformation.HostId")) + { + s.SetDefault("NServiceBus.HostInformation.HostId", DeterministicGuid.Create(fullPathToStartingExe, RuntimeEnvironment.MachineName)); + } s.SetDefault("NServiceBus.HostInformation.DisplayName", RuntimeEnvironment.MachineName); s.SetDefault("NServiceBus.HostInformation.Properties", new Dictionary { @@ -72,15 +76,6 @@ protected internal override void Setup(FeatureConfigurationContext context) SetTransportThresholds(context); } - static Guid GenerateDefaultHostId(out string fullPathToStartingExe) - { - var gen = new DefaultHostIdGenerator(Environment.CommandLine, RuntimeEnvironment.MachineName); - - fullPathToStartingExe = gen.FullPathToStartingExe; - - return gen.HostId; - } - void SetTransportThresholds(FeatureConfigurationContext context) { var transportConfig = context.Settings.GetConfigSection(); diff --git a/src/NServiceBus.Core/Unicast/DefaultHostIdGenerator.cs b/src/NServiceBus.Core/Unicast/DefaultHostIdGenerator.cs deleted file mode 100644 index 53803023d01..00000000000 --- a/src/NServiceBus.Core/Unicast/DefaultHostIdGenerator.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace NServiceBus.Unicast -{ - using System; - using System.Linq; - using System.Text.RegularExpressions; - using NServiceBus.Utils; - - struct DefaultHostIdGenerator - { - public string FullPathToStartingExe; - public Guid HostId; - - public DefaultHostIdGenerator(string commandLine, string machineName) - { - if (commandLine.StartsWith("\"")) - { - FullPathToStartingExe = (from Match match in Regex.Matches(commandLine, "\"([^\"]*)\"") - select match.ToString()).First().Trim('"'); - } - else - { - FullPathToStartingExe = commandLine.Split(' ').First(); - } - - HostId = DeterministicGuid.Create(FullPathToStartingExe, machineName); - } - } -} \ No newline at end of file