From 333f40597703375e5d355242afe9be4d1c481613 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 20 Aug 2014 12:11:35 +1000 Subject: [PATCH 1/3] add explicit failing test for bad conventions related to #2319 --- .../When_using_a_greedy_convention.cs | 65 +++++++++++++++++++ .../NServiceBus.AcceptanceTests.csproj | 11 ++++ 2 files changed, 76 insertions(+) create mode 100644 src/NServiceBus.AcceptanceTests/BasicMessaging/When_using_a_greedy_convention.cs diff --git a/src/NServiceBus.AcceptanceTests/BasicMessaging/When_using_a_greedy_convention.cs b/src/NServiceBus.AcceptanceTests/BasicMessaging/When_using_a_greedy_convention.cs new file mode 100644 index 00000000000..1cf6432cd8d --- /dev/null +++ b/src/NServiceBus.AcceptanceTests/BasicMessaging/When_using_a_greedy_convention.cs @@ -0,0 +1,65 @@ +namespace NServiceBus.AcceptanceTests.BasicMessaging +{ + using System; + using EndpointTemplates; + using AcceptanceTesting; + using NUnit.Framework; + using ScenarioDescriptors; + + public class When_using_a_greedy_convention : NServiceBusAcceptanceTest + { + [Test] + [Explicit] + public void Should_receive_the_message() + { + Scenario.Define(() => new Context { Id = Guid.NewGuid() }) + .WithEndpoint(b => b.Given((bus, context) => bus.SendLocal(new MyMessage + {Id = context.Id}))) + .Done(c => c.WasCalled) + .Repeat(r =>r + .For(Transports.Msmq) + ) + .Should(c => Assert.True(c.WasCalled, "The message handler should be called")) + .Run(); + } + + public class Context : ScenarioContext + { + public bool WasCalled { get; set; } + + public Guid Id { get; set; } + } + + public class EndPoint : EndpointConfigurationBuilder + { + public EndPoint() + { + EndpointSetup(c => c.DefiningMessagesAs(MessageConvention)); + } + + static bool MessageConvention(Type t) + { + + return t.Namespace != null && t.Namespace.EndsWith(".Messages"); + } + } + + [Serializable] + public class MyMessage : ICommand + { + public Guid Id { get; set; } + } + + public class MyMessageHandler : IHandleMessages + { + public Context Context { get; set; } + public void Handle(MyMessage message) + { + if (Context.Id != message.Id) + return; + + Context.WasCalled = true; + } + } + } +} diff --git a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj index add5476ccbc..f98935a7c33 100644 --- a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj +++ b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj @@ -99,6 +99,17 @@ + + + + + + + + + + + From 9ea2e024b07389fdefe362f17fd3e2c1eac7f080 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 20 Aug 2014 13:06:49 +1000 Subject: [PATCH 2/3] dont include types in NSB in message conventions add acceptance test for greedy messages fix acceptance test us SN for detecting particular assembly fix inspection error remove the extension method sign tests with a diff SN key redundant using --- .../When_using_a_greedy_convention.cs | 7 +- .../MessageConventionExtensionsTests.cs | 38 ++++++++++- .../NServiceBus.Core.Tests.csproj | 3 + src/NServiceBus.Core.Tests/Test.snk | Bin 0 -> 596 bytes src/NServiceBus.Core/InternalsVisibleTo.cs | 2 +- .../MessageConventionExtensions.cs | 60 ++++++++++++++++-- src/NServiceBus/NServiceBus.csproj | 3 + 7 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 src/NServiceBus.Core.Tests/Test.snk diff --git a/src/NServiceBus.AcceptanceTests/BasicMessaging/When_using_a_greedy_convention.cs b/src/NServiceBus.AcceptanceTests/BasicMessaging/When_using_a_greedy_convention.cs index 1cf6432cd8d..c0320ddaf44 100644 --- a/src/NServiceBus.AcceptanceTests/BasicMessaging/When_using_a_greedy_convention.cs +++ b/src/NServiceBus.AcceptanceTests/BasicMessaging/When_using_a_greedy_convention.cs @@ -9,7 +9,6 @@ public class When_using_a_greedy_convention : NServiceBusAcceptanceTest { [Test] - [Explicit] public void Should_receive_the_message() { Scenario.Define(() => new Context { Id = Guid.NewGuid() }) @@ -39,13 +38,13 @@ public EndPoint() static bool MessageConvention(Type t) { - - return t.Namespace != null && t.Namespace.EndsWith(".Messages"); + return t.Namespace != null && + (t.Namespace.EndsWith(".Messages") || (t == typeof(MyMessage))); } } [Serializable] - public class MyMessage : ICommand + public class MyMessage { public Guid Id { get; set; } } diff --git a/src/NServiceBus.Core.Tests/MessageConventionExtensionsTests.cs b/src/NServiceBus.Core.Tests/MessageConventionExtensionsTests.cs index 9e0a59a107a..87d844a861e 100644 --- a/src/NServiceBus.Core.Tests/MessageConventionExtensionsTests.cs +++ b/src/NServiceBus.Core.Tests/MessageConventionExtensionsTests.cs @@ -1,6 +1,9 @@ -namespace NServiceBus.Core.Tests.Encryption +namespace NServiceBus.Core.Tests { using System; + using System.Reflection; + using System.Reflection.Emit; + using Unicast.Messages; using NUnit.Framework; [TestFixture] @@ -12,6 +15,7 @@ public void Should_use_TimeToBeReceived_from_bottom_of_tree() var timeToBeReceivedAction = MessageConventionExtensions.TimeToBeReceivedAction(typeof(InheritedClassWithAttribute)); Assert.AreEqual(TimeSpan.FromSeconds(2), timeToBeReceivedAction); } + [Test] public void Should_use_inherited_TimeToBeReceived() { @@ -32,6 +36,38 @@ class InheritedClassWithNoAttribute : BaseClass { } + + + [Test] + public void Should_return_false_for_SN_and_non_particular_assembly() + { + Assert.IsFalse(MessageConventionExtensions.IsFromParticularAssembly(typeof(string))); + } + + [Test] + public void Should_return_true_for_particular_assembly() + { + Assert.IsTrue(MessageConventionExtensions.IsFromParticularAssembly(typeof(ExecuteLogicalMessagesBehavior))); + } + + [Test] + public void Should_return_false_for_non_SN_and_non_particular_assembly() + { + var type = GetNonSnFakeType(); + Assert.IsFalse(MessageConventionExtensions.IsFromParticularAssembly(type)); + } + + static Type GetNonSnFakeType() + { + var assemblyName = new AssemblyName + { + Name = "myAssembly" + }; + var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.ReflectionOnly); + var newModule = assemblyBuilder.DefineDynamicModule("myModule"); + var myType = newModule.DefineType("myType", TypeAttributes.Public); + return myType.CreateType(); + } } } \ No newline at end of file diff --git a/src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj b/src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj index a3a03b3aa30..81c29e53f97 100644 --- a/src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj +++ b/src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj @@ -13,6 +13,8 @@ 512 true ..\NServiceBus.snk + Test.snk + ..\ true @@ -310,6 +312,7 @@ Designer + diff --git a/src/NServiceBus.Core.Tests/Test.snk b/src/NServiceBus.Core.Tests/Test.snk new file mode 100644 index 0000000000000000000000000000000000000000..59da17bf7e1d4fead026a1c91ff9e0c04f5ec4a0 GIT binary patch literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50097h7UC0V{{#_(`n8hT=zBOKxGBHP? z(wT$S<&m&Lg5=r^o*2E59uX`6Gm>7n*M}@G7Y1cmk;Tc?a+jG;aD5i;nvX!)c<^NI zAN+H`+doGVX&WQIS#Na{4^GK=&v+139`%0O8O{+(dmO`*r>)R8nt#l=RZ-kJ02l zDMD)>6z*np1>f((g8)p(pPFPr(*;hv?aOW0t-h{USl=|nb=8Q`R3NPbS5`v8r2Uj2 zPU9RCkc3dn(Sp3bWR!Q}BnLq_?EKOh`BBD+c$^nvL>dXlAI6$nAS#tUzm)1BKm0)j zyLUYZ*O4Yclq*N}d-5?1F3t#jw_Ad@` zf_VIiv3!_lYBeVd79}7A#U$DDh21ja#h0Wbu7M)I0PSE public static class MessageConventionExtensions { + static byte[] nsbPublicKeyToken; + + static MessageConventionExtensions() + { + var currentAssemblyName = typeof(MessageConventionExtensions).Assembly.GetName(); + nsbPublicKeyToken = currentAssemblyName.GetPublicKeyToken(); + } + + internal static bool IsFromParticularAssembly(Type type) + { + return type.Assembly.GetName() + .GetPublicKeyToken() + .SequenceEqual(nsbPublicKeyToken); + } + /// /// Returns true if the given object is a message. /// @@ -27,10 +42,20 @@ public static bool IsMessageType(Type t) try { return MessagesConventionCache.ApplyConvention(t, - type => IsMessageTypeAction(type) || - IsCommandTypeAction(type) || - IsEventTypeAction(type) || - IsInSystemConventionList(type)); + type => + { + if (IsInSystemConventionList(type)) + { + return true; + } + if (IsFromParticularAssembly(type)) + { + return false; + } + return IsMessageTypeAction(type) || + IsCommandTypeAction(type) || + IsEventTypeAction(type); + }); } catch (Exception ex) { @@ -73,7 +98,14 @@ public static bool IsCommandType(Type t) { try { - return CommandsConventionCache.ApplyConvention(t, type => IsCommandTypeAction(type)); + return CommandsConventionCache.ApplyConvention(t, type => + { + if (IsFromParticularAssembly(type)) + { + return false; + } + return IsCommandTypeAction(type); + }); } catch (Exception ex) { @@ -96,7 +128,14 @@ public static bool IsExpressMessageType(Type t) { try { - return ExpressConventionCache.ApplyConvention(t, type => IsExpressMessageAction(type)); + return ExpressConventionCache.ApplyConvention(t, type => + { + if (IsFromParticularAssembly(type)) + { + return false; + } + return IsExpressMessageAction(type); + }); } catch (Exception ex) { @@ -151,7 +190,14 @@ public static bool IsEventType(Type t) { try { - return EventsConventionCache.ApplyConvention(t, type => IsEventTypeAction(type)); + return EventsConventionCache.ApplyConvention(t, type => + { + if (IsFromParticularAssembly(type)) + { + return false; + } + return IsEventTypeAction(type); + }); } catch (Exception ex) { diff --git a/src/NServiceBus/NServiceBus.csproj b/src/NServiceBus/NServiceBus.csproj index 9bc5821f71b..e527908b88d 100644 --- a/src/NServiceBus/NServiceBus.csproj +++ b/src/NServiceBus/NServiceBus.csproj @@ -55,6 +55,9 @@ + + InternalsVisibleTo.cs + From 3b6eac8e5ba4146d39330d505a94955c01cf9093 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 22 Aug 2014 10:03:47 +1000 Subject: [PATCH 3/3] fix dodgy cherry pick --- .../NServiceBus.AcceptanceTests.csproj | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj index f98935a7c33..f631b1ed95f 100644 --- a/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj +++ b/src/NServiceBus.AcceptanceTests/NServiceBus.AcceptanceTests.csproj @@ -100,16 +100,6 @@ - - - - - - - - - -