Skip to content

Commit

Permalink
Added multi-router tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonPobiega committed Oct 18, 2023
1 parent 3859dd0 commit 6a6b93e
Show file tree
Hide file tree
Showing 8 changed files with 950 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public void Charlie()
{
SetStoragePath("Charlie");
}


public void Delta()
{
SetStoragePath("Delta");
}

public void Yankee()
{
SetStoragePath("Yankee");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.Threading.Tasks;
using NServiceBus.AcceptanceTesting;
using NUnit.Framework;

namespace NServiceBus.Router.AcceptanceTests.MultipleRouters
{
using AcceptanceTesting.Customization;

[TestFixture]
public class When_connecting_to_two_routers : NServiceBusAcceptanceTest
{
[Test]
public async Task Should_deliver_the_messages_to_destination_endpoints()
{
var result = await Scenario.Define<Context>()
.WithRouter("RouterA", cfg =>
{
cfg.AddInterface("Left", false).Broker().Charlie();
cfg.AddInterface("Right", false).Broker().Alpha();
cfg.UseStaticRoutingProtocol().AddForwardRoute("Left", "Right");
})
.WithRouter("RouterB", cfg =>
{
cfg.AddInterface("Left", false).Broker().Charlie();
cfg.AddInterface("Right", false).Broker().Bravo();
cfg.UseStaticRoutingProtocol().AddForwardRoute("Left", "Right");
})
.WithEndpoint<Sender>(c => c.When(async s =>
{
await s.Send(new MyRequestA());
await s.Send(new MyRequestB());
}))
.WithEndpoint<ReceiverA>()
.WithEndpoint<ReceiverB>()
.Done(c => c.ReceivedByB && c.ReceivedByA)
.Run();

Assert.IsTrue(result.ReceivedByB);
Assert.IsTrue(result.ReceivedByA);
}

class Context : ScenarioContext
{
public bool ReceivedByA { get; set; }
public bool ReceivedByB { get; set; }
}

class Sender : EndpointConfigurationBuilder
{
public Sender()
{
EndpointSetup<DefaultServer>(c =>
{
c.ConfigureBroker().Charlie();
var routing = c.ConfigureRouting();
var routerA = routing.ConnectToRouter("RouterA");
routerA.RouteToEndpoint(typeof(MyRequestA), Conventions.EndpointNamingConvention(typeof(ReceiverA)));
var routerB = routing.ConnectToRouter("RouterB");
routerB.RouteToEndpoint(typeof(MyRequestB), Conventions.EndpointNamingConvention(typeof(ReceiverB)));
});
}
}

class ReceiverA : EndpointConfigurationBuilder
{
public ReceiverA()
{
EndpointSetup<DefaultServer>(c =>
{
//No bridge configuration needed for reply
c.ConfigureBroker().Alpha();
});
}

class MyRequestHandler : IHandleMessages<MyRequestA>
{
Context scenarioContext;

public MyRequestHandler(Context scenarioContext)
{
this.scenarioContext = scenarioContext;
}

public Task Handle(MyRequestA requestA, IMessageHandlerContext context)
{
scenarioContext.ReceivedByA = true;
return Task.CompletedTask;
}
}
}

class ReceiverB : EndpointConfigurationBuilder
{
public ReceiverB()
{
EndpointSetup<DefaultServer>(c =>
{
//No bridge configuration needed for reply
c.ConfigureBroker().Bravo();
});
}

class MyRequestHandler : IHandleMessages<MyRequestB>
{
Context scenarioContext;

public MyRequestHandler(Context scenarioContext)
{
this.scenarioContext = scenarioContext;
}

public Task Handle(MyRequestB requestB, IMessageHandlerContext context)
{
scenarioContext.ReceivedByB = true;
return Task.CompletedTask;
}
}
}

class MyRequestA : IMessage
{
}

class MyRequestB : IMessage
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Threading.Tasks;
using NServiceBus.AcceptanceTesting;
using NUnit.Framework;

namespace NServiceBus.Router.AcceptanceTests.MultipleRouters
{
using AcceptanceTesting.Customization;

[TestFixture]
public class When_publishing_via_double_unicast_bridge : NServiceBusAcceptanceTest
{
[Test]
public async Task It_should_deliver_the_message()
{
var result = await Scenario.Define<Context>()
.WithRouter("Green-Blue", cfg =>
{
cfg.AddInterface("Green", false).Broker().Alpha();
cfg.AddInterface("Blue", false).Broker().Bravo();
cfg.UseStaticRoutingProtocol().AddForwardRoute("Blue", "Green");
})
.WithRouter("Blue-Red", cfg =>
{
cfg.AddInterface("Blue", false).Broker().Bravo();
cfg.AddInterface("Red", false).Broker().Charlie();
cfg.UseStaticRoutingProtocol().AddForwardRoute("Red", "Blue", "Green-Blue");
})
.WithEndpoint<Publisher>(c => c.When(x => x.EventSubscribed, s => s.Publish(new MyEvent())))
.WithEndpoint<Subscriber>()
.Done(c => c.EventDelivered)
.Run();

Assert.IsTrue(result.EventDelivered);
}

class Context : ScenarioContext
{
public bool EventDelivered { get; set; }
public bool EventSubscribed { get; set; }
}

class Publisher : EndpointConfigurationBuilder
{
public Publisher()
{
EndpointSetup<DefaultServer>(c =>
{
//No bridge configuration needed for publisher
c.ConfigureBroker().Alpha();
c.ConfigureRouting().EnableMessageDrivenPubSubCompatibilityMode();
c.OnEndpointSubscribed<Context>((args, context) =>
{
context.EventSubscribed = true;
});
});
}
}

class Subscriber : EndpointConfigurationBuilder
{
public Subscriber()
{
EndpointSetup<DefaultServer>(c =>
{
c.ConfigureBroker().Charlie();
var routing = c.ConfigureRouting();
var ramp = routing.ConnectToRouter("Blue-Red");
ramp.RegisterPublisher(typeof(MyEvent), Conventions.EndpointNamingConvention(typeof(Publisher)));
});
}

class MyEventHandler : IHandleMessages<MyEvent>
{
Context scenarioContext;

public MyEventHandler(Context scenarioContext)
{
this.scenarioContext = scenarioContext;
}

public Task Handle(MyEvent message, IMessageHandlerContext context)
{
scenarioContext.EventDelivered = true;
return Task.CompletedTask;
}
}
}

class MyEvent : IEvent
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Threading.Tasks;
using NServiceBus.AcceptanceTesting;
using NUnit.Framework;

namespace NServiceBus.Router.AcceptanceTests.MultipleRouters
{
using AcceptanceTesting.Customization;

[TestFixture]
public class When_publishing_via_double_unicast_multicast_bridge : NServiceBusAcceptanceTest
{
[Test]
public async Task It_should_deliver_the_message()
{
var result = await Scenario.Define<Context>()
.WithRouter("Green-Blue", cfg =>
{
cfg.AddInterface("Green", false).Broker().Alpha();
cfg.AddInterface("Blue", false).Broker().Bravo();
cfg.UseStaticRoutingProtocol().AddForwardRoute("Blue", "Green");
})
.WithRouter("Blue-Red", cfg =>
{
cfg.AddInterface("Blue", false).Broker().Bravo();
cfg.AddInterface("Red", false).Broker().Charlie();
cfg.UseStaticRoutingProtocol().AddForwardRoute("Red", "Blue", "Green-Blue");
})
.WithEndpoint<Publisher>(c => c.When(x => x.EventSubscribed, s => s.Publish(new MyEvent())))
.WithEndpoint<Subscriber>()
.Done(c => c.EventDelivered)
.Run();

Assert.IsTrue(result.EventDelivered);
}

class Context : ScenarioContext
{
public bool EventDelivered { get; set; }
public bool EventSubscribed { get; set; }
}

class Publisher : EndpointConfigurationBuilder
{
public Publisher()
{
EndpointSetup<DefaultServer>(c =>
{
//No bridge configuration needed for publisher
c.ConfigureBroker().Alpha();
c.ConfigureRouting().EnableMessageDrivenPubSubCompatibilityMode();
c.OnEndpointSubscribed<Context>((args, context) =>
{
context.EventSubscribed = true;
});
});
}
}

class Subscriber : EndpointConfigurationBuilder
{
public Subscriber()
{
EndpointSetup<DefaultServer>(c =>
{
c.ConfigureBroker().Charlie();
var routing = c.ConfigureRouting();
var bridge = routing.ConnectToRouter("Blue-Red");
bridge.RegisterPublisher(typeof(MyEvent), Conventions.EndpointNamingConvention(typeof(Publisher)));
});
}

class MyEventHandler : IHandleMessages<MyEvent>
{
Context scenarioContext;

public MyEventHandler(Context scenarioContext)
{
this.scenarioContext = scenarioContext;
}

public Task Handle(MyEvent message, IMessageHandlerContext context)
{
scenarioContext.EventDelivered = true;
return Task.CompletedTask;
}
}
}

class MyEvent : IEvent
{
}
}
}
Loading

0 comments on commit 6a6b93e

Please sign in to comment.