From 633381bfc2d4eeb94ff1d19ef7827ca66eea03bc Mon Sep 17 00:00:00 2001 From: Mike Minutillo Date: Wed, 23 Oct 2024 11:34:07 +0800 Subject: [PATCH] Better test --- ..._conventions_in_an_endpoint_with_a_Saga.cs | 55 ------------------- ...ntaining_a_ref_struct_and_sagas_enabled.cs | 54 ++++++++++++++++++ 2 files changed, 54 insertions(+), 55 deletions(-) delete mode 100644 src/NServiceBus.AcceptanceTests/Core/Conventions/When_a_ref_struct_gets_checked_by_conventions_in_an_endpoint_with_a_Saga.cs create mode 100644 src/NServiceBus.AcceptanceTests/Core/Conventions/When_scanning_an_assembly_containing_a_ref_struct_and_sagas_enabled.cs diff --git a/src/NServiceBus.AcceptanceTests/Core/Conventions/When_a_ref_struct_gets_checked_by_conventions_in_an_endpoint_with_a_Saga.cs b/src/NServiceBus.AcceptanceTests/Core/Conventions/When_a_ref_struct_gets_checked_by_conventions_in_an_endpoint_with_a_Saga.cs deleted file mode 100644 index bcf124030f..0000000000 --- a/src/NServiceBus.AcceptanceTests/Core/Conventions/When_a_ref_struct_gets_checked_by_conventions_in_an_endpoint_with_a_Saga.cs +++ /dev/null @@ -1,55 +0,0 @@ -namespace NServiceBus.AcceptanceTests.Core.Conventions; - -using System; -using System.Threading.Tasks; -using AcceptanceTesting; -using EndpointTemplates; -using NUnit.Framework; - -public class When_a_ref_struct_gets_checked_by_conventions_in_an_endpoint_with_a_Saga : NServiceBusAcceptanceTest -{ - // NOTE: AssemblyRouteSource and NamespaceRouteSource do not filter out ref structs encountered in a message assembly. - // The conventions added by the Sagas feature were throwing an exception when passed a ref struct. - // See https://github.com/Particular/NServiceBus/issues/7179 for details. - // This test simulates what the RouteSource objects do so that a separate message assembly is not needed for the test. - [Test] - public void It_should_not_throw_an_exception() - => Assert.DoesNotThrowAsync( - () => Scenario.Define() - .WithEndpoint( - // Capture conventions - b => b.CustomConfig((endpoint, ctx) => ctx.Conventions = endpoint.Conventions().Conventions) - ) - .Done(ctx => !ctx.Conventions.IsMessageType(typeof(RefStruct))) - .Run() - ); - - class Context : ScenarioContext - { - public NServiceBus.Conventions Conventions { get; set; } - } - - class SomeMessage : IMessage - { - public Guid BusinessId { get; set; } - } - - public ref struct RefStruct { } - - class EndpointWithASaga : EndpointConfigurationBuilder - { - public EndpointWithASaga() => EndpointSetup(); - - class MySagaData : ContainSagaData - { - public Guid BusinessId { get; set; } - } - - class MySaga : Saga, IAmStartedByMessages - { - public Task Handle(SomeMessage message, IMessageHandlerContext context) => Task.CompletedTask; - protected override void ConfigureHowToFindSaga(SagaPropertyMapper mapper) - => mapper.MapSaga(saga => saga.BusinessId).ToMessage(msg => msg.BusinessId); - } - } -} diff --git a/src/NServiceBus.AcceptanceTests/Core/Conventions/When_scanning_an_assembly_containing_a_ref_struct_and_sagas_enabled.cs b/src/NServiceBus.AcceptanceTests/Core/Conventions/When_scanning_an_assembly_containing_a_ref_struct_and_sagas_enabled.cs new file mode 100644 index 0000000000..59c2042db7 --- /dev/null +++ b/src/NServiceBus.AcceptanceTests/Core/Conventions/When_scanning_an_assembly_containing_a_ref_struct_and_sagas_enabled.cs @@ -0,0 +1,54 @@ +namespace NServiceBus.AcceptanceTests.Core.Conventions; + +using System; +using System.Threading.Tasks; +using AcceptanceTesting; +using EndpointTemplates; +using NServiceBus.AcceptanceTesting.Customization; +using NUnit.Framework; + +public class When_scanning_an_assembly_containing_a_ref_struct_and_sagas_enabled : NServiceBusAcceptanceTest +{ + [Test] + public void It_should_not_throw_an_exception() + => Assert.DoesNotThrowAsync( + () => Scenario.Define() + .WithEndpoint() + .Run() + ); + + public class SomeMessage : IMessage + { + public Guid BusinessId { get; set; } + } + + // HINT: This will get picked up by the AssemblyRouteSource created by the routing call below + // Even though it is not a message type, it is still checked by passing it to conventions. + // The conventions added by Sagas were throwing an exception when passed a ref struct. + // See https://github.com/Particular/NServiceBus/issues/7179 for details. + ref struct RefStruct { } + + class EndpointWithASaga : EndpointConfigurationBuilder + { + public EndpointWithASaga() => EndpointSetup(cfg => cfg + .ConfigureRouting() + .RouteToEndpoint( + typeof(RefStruct).Assembly, + Conventions.EndpointNamingConvention(typeof(EndpointWithASaga)) + ) + ); + + + class RealSagaToSetUpConventions : Saga, IAmStartedByMessages + { + public Task Handle(SomeMessage message, IMessageHandlerContext context) => Task.CompletedTask; + protected override void ConfigureHowToFindSaga(SagaPropertyMapper mapper) + => mapper.MapSaga(saga => saga.BusinessId).ToMessage(msg => msg.BusinessId); + + public class RealSagaToSetUpConventionsSagaData : ContainSagaData + { + public virtual Guid BusinessId { get; set; } + } + } + } +}