Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace NServiceBus.AcceptanceTests.Routing
{
using System;
using System.Threading.Tasks;
using AcceptanceTesting;
using AcceptanceTesting.Customization;
using EndpointTemplates;
using Features;
using NServiceBus.Config;
using NServiceBus.Pipeline;
using NUnit.Framework;
using ScenarioDescriptors;
public class When_publishing_an_interface : NServiceBusAcceptanceTest
{
public static string PublisherEndpoint => Conventions.EndpointNamingConvention(typeof(Publisher));
[Test]
public async Task Should_receive_event_for_non_xml()
{
await Scenario.Define<Context>()
.WithEndpoint<Publisher>(b =>
b.When(c => c.Subscribed, (session, ctx) => session.Publish<IMyEvent>()))
.WithEndpoint<Subscriber>(b => b.When(async (session, context) =>
{
await session.Subscribe<IMyEvent>();
if (context.HasNativePubSubSupport)
{
context.Subscribed = true;
}
}))
.Done(c => c.GotTheEvent)
.Repeat(r => r.For(Serializers.Json))
.Should(c =>
{
Assert.True(c.GotTheEvent);
Assert.AreEqual(typeof(IMyEvent), c.EventTypePassedToRouting);
})
.Run();
}
public class Context : ScenarioContext
{
public bool GotTheEvent { get; set; }
public bool Subscribed { get; set; }
public Type EventTypePassedToRouting { get; set; }
}
public class Publisher : EndpointConfigurationBuilder
{
public Publisher()
{
EndpointSetup<DefaultPublisher>(c =>
{
c.Pipeline.Register("EventTypeSpy", typeof(EventTypeSpy), "EventTypeSpy");
c.OnEndpointSubscribed<Context>((s, context) =>
{
if (s.SubscriberReturnAddress.Contains("Subscriber"))
{
context.Subscribed = true;
}
});
c.Conventions().DefiningEventsAs(t => t.Name.EndsWith("Event"));
});
}
class EventTypeSpy : Behavior<IOutgoingLogicalMessageContext>
{
public EventTypeSpy(Context testContext)
{
this.testContext = testContext;
}
public override Task Invoke(IOutgoingLogicalMessageContext context, Func<Task> next)
{
testContext.EventTypePassedToRouting = context.Message.MessageType;
return next();
}
Context testContext;
}
}
public class Subscriber : EndpointConfigurationBuilder
{
public Subscriber()
{
EndpointSetup<DefaultServer>(c =>
{
c.DisableFeature<AutoSubscribe>();
c.Conventions().DefiningEventsAs(t => t.Name.EndsWith("Event"));
})
.WithConfig<UnicastBusConfig>(ubc =>
{
ubc.MessageEndpointMappings = new MessageEndpointMappingCollection();
var mapping = new MessageEndpointMapping
{
AssemblyName = typeof(IMyEvent).Assembly.GetName().Name,
Endpoint = PublisherEndpoint
};
ubc.MessageEndpointMappings.Add(mapping);
});
}
public class MyEventHandler : IHandleMessages<IMyEvent>
{
public Context Context { get; set; }
public Task Handle(IMyEvent @event, IMessageHandlerContext context)
{
Context.GotTheEvent = true;
return Task.FromResult(0);
}
}
}
public interface IMyEvent
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment