public void CreateNewChannelDispatcherWithDefaultHandler() { var dispatcher = new ChannelDispatcher(); var defaultDispatcher = new CounterHandler(); dispatcher.RegisterDefault(defaultDispatcher); Assert.IsNotNull(dispatcher.DefaultHandler); dispatcher.Handle("/services/test", "data..."); Assert.AreEqual(1, defaultDispatcher.Calls, "Invalid number of notifications!"); }
public async ValueTask TestSendAsync() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var handler = new CounterHandler(); var channel = new PublishSubscribeChannel(provider.GetService <IApplicationContext>()); channel.Subscribe(handler); var message = Message.Create("test"); Assert.True(await channel.SendAsync(message)); Assert.Equal(1, handler.Count); }
public async Task TestSendAsyncOneHandler_10_000_000() { var channel = new DirectChannel(provider.GetService <IApplicationContext>()); var handler = new CounterHandler(); channel.Subscribe(handler); var message = Message.Create("test"); Assert.True(await channel.SendAsync(message)); for (var i = 0; i < 10000000; i++) { await channel.SendAsync(message); } Assert.Equal(10000001, handler.Count); }
public void TestGlobalWildcardChannelMatching() { var dispatcher = new ChannelDispatcher(); var handler1 = new CounterHandler(); var handler2 = new CounterHandler(); var handler3 = new CounterHandler(); var defaultHandler = new CounterHandler(); dispatcher.Register("/services/test/**", handler1); dispatcher.Register("/services/test", handler2); dispatcher.Register("/services/banana/*", handler2); dispatcher.Register("/services/banana/**", handler3); dispatcher.RegisterDefault(defaultHandler); Assert.IsNotNull(dispatcher.DefaultHandler); dispatcher.Handle("/services/test", "data..."); // hits handler2 Assert.AreEqual(0, defaultHandler.Calls); Assert.AreEqual(0, handler1.Calls); Assert.AreEqual(1, handler2.Calls); Assert.AreEqual(0, handler3.Calls); dispatcher.Handle("/services/test/edit", "data..."); // hits handler1 Assert.AreEqual(0, defaultHandler.Calls); Assert.AreEqual(1, handler1.Calls); Assert.AreEqual(1, handler2.Calls); Assert.AreEqual(0, handler3.Calls); dispatcher.Handle("/services/test/1/edit", "data..."); // hits handler1 Assert.AreEqual(0, defaultHandler.Calls); Assert.AreEqual(2, handler1.Calls); Assert.AreEqual(1, handler2.Calls); Assert.AreEqual(0, handler3.Calls); dispatcher.Handle("/services/banana", "data..."); // hits default handler Assert.AreEqual(1, defaultHandler.Calls); Assert.AreEqual(2, handler1.Calls); Assert.AreEqual(1, handler2.Calls); Assert.AreEqual(0, handler3.Calls); dispatcher.Handle("/services/banana/1/edit", "data..."); // hits handler3 Assert.AreEqual(1, defaultHandler.Calls); Assert.AreEqual(2, handler1.Calls); Assert.AreEqual(1, handler2.Calls); Assert.AreEqual(1, handler3.Calls); }
public void TestWildcardBasicChannelMatchingWith3Handlers() { var dispatcher = new ChannelDispatcher(); var handler1 = new CounterHandler(); var handler2 = new CounterHandler(); var handler3 = new CounterHandler(); var defaultHandler = new CounterHandler(); dispatcher.Register("/services/test/*/edit", handler1); dispatcher.Register("/services/test/2", handler2); dispatcher.Register("/services/banana/*", handler2); dispatcher.Register("/services/internal", handler3); dispatcher.RegisterDefault(defaultHandler); Assert.IsNotNull(dispatcher.DefaultHandler); dispatcher.Handle("/services/test/1", "data..."); // hits default Assert.AreEqual(1, defaultHandler.Calls); Assert.AreEqual(0, handler1.Calls); Assert.AreEqual(0, handler2.Calls); Assert.AreEqual(0, handler3.Calls); dispatcher.Handle("/services/internal", "data..."); // hits handler3 Assert.AreEqual(1, defaultHandler.Calls); Assert.AreEqual(0, handler1.Calls); Assert.AreEqual(0, handler2.Calls); Assert.AreEqual(1, handler3.Calls); dispatcher.Handle("/services/test/1/edit", "data..."); // hits handler1 Assert.AreEqual(1, defaultHandler.Calls); Assert.AreEqual(1, handler1.Calls); Assert.AreEqual(0, handler2.Calls); Assert.AreEqual(1, handler3.Calls); dispatcher.Handle("/services/banana/edit", "data..."); // hits handler2 Assert.AreEqual(1, defaultHandler.Calls); Assert.AreEqual(1, handler1.Calls); Assert.AreEqual(1, handler2.Calls); Assert.AreEqual(1, handler3.Calls); dispatcher.Handle("/services/banana/1/edit", "data..."); // hits default Assert.AreEqual(2, defaultHandler.Calls); Assert.AreEqual(1, handler1.Calls); Assert.AreEqual(1, handler2.Calls); Assert.AreEqual(1, handler3.Calls); }
public void DirectChannel_Send_10_000_000_FourHandlers() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var channel = new DirectChannel(provider); var count1 = new CounterHandler(); var count2 = new CounterHandler(); var count3 = new CounterHandler(); var count4 = new CounterHandler(); channel.Subscribe(count1); channel.Subscribe(count2); channel.Subscribe(count3); channel.Subscribe(count4); var message = new GenericMessage("test"); for (var i = 0; i < 10_000_000; i++) { channel.Send(message); } if (count1.Count != 10_000_000 / 4) { throw new InvalidOperationException("Handler count1 invalid"); } if (count2.Count != 10_000_000 / 4) { throw new InvalidOperationException("Handler count2 invalid"); } if (count3.Count != 10_000_000 / 4) { throw new InvalidOperationException("Handler count3 invalid"); } if (count4.Count != 10_000_000 / 4) { throw new InvalidOperationException("Handler count4 invalid"); } }
public void TestSendOneHandler_10_000_000() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var handler = new CounterHandler(); var channel = new PublishSubscribeChannel(provider.GetService <IApplicationContext>()); channel.Subscribe(handler); var message = Message.Create("test"); Assert.True(channel.Send(message)); for (var i = 0; i < 10000000; i++) { channel.Send(message); } Assert.Equal(10000001, handler.Count); }
public async ValueTask TestSendAsyncOneHandler_10_000_000() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var handler = new CounterHandler(); var channel = new PublishSubscribeChannel(provider); channel.Subscribe(handler); var message = new GenericMessage("test"); Assert.True(await channel.SendAsync(message)); for (var i = 0; i < 10000000; i++) { await channel.SendAsync(message); } Assert.Equal(10000001, handler.Count); }
public void TestBasicChannelMatchingWith2Handlers() { var dispatcher = new ChannelDispatcher(); var handler1 = new CounterHandler(); var handler2 = new CounterHandler(); var defaultHandler = new CounterHandler(); dispatcher.Register("/services/test/1", handler1); dispatcher.Register("/services/test/2", handler2); dispatcher.RegisterDefault(defaultHandler); Assert.IsNotNull(dispatcher.DefaultHandler); dispatcher.Handle("/services/test/2", "data..."); Assert.AreEqual(1, handler2.Calls, "Invalid number of test/2 notifications!"); Assert.AreEqual(0, handler1.Calls, "Too many test/2 handler calls on handler1!"); Assert.AreEqual(0, defaultHandler.Calls, "Too many test/2 handler calls on defaultHandler!"); dispatcher.Handle("/services/test/1", "data..."); Assert.AreEqual(1, handler2.Calls, "Invalid number of test/2 notifications!"); Assert.AreEqual(1, handler1.Calls, "Invalid number of test/1 notifications!"); Assert.AreEqual(0, defaultHandler.Calls, "Too many test/1 handler calls on defaultHandler!"); }
public async ValueTask TestSendAsyncTwoHandler_10_000_000() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var handler1 = new CounterHandler(); var handler2 = new CounterHandler(); var channel = new PublishSubscribeChannel(provider.GetService <IApplicationContext>()); channel.Subscribe(handler1); channel.Subscribe(handler2); var message = Message.Create("test"); for (var i = 0; i < 10000000; i++) { await channel.SendAsync(message); } Assert.Equal(10000000, handler1.Count); Assert.Equal(10000000, handler2.Count); }
public void PublishSubscribeChannel_Send_10_000_000_SingleHandler() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var channel = new PublishSubscribeChannel(provider); var handler = new CounterHandler(); channel.Subscribe(handler); var message = new GenericMessage("test"); channel.Send(message); for (var i = 0; i < 10_000_000; i++) { channel.Send(message); } if (handler.Count != 10_000_000 + 1) { throw new InvalidOperationException("Handler count invalid"); } }
public async Task DirectChannel_SendAsync_10_000_000_SingleHandler() { var services = new ServiceCollection(); services.AddSingleton <IIntegrationServices, IntegrationServices>(); var provider = services.BuildServiceProvider(); var directChannel = new DirectChannel(provider); var handler = new CounterHandler(); directChannel.Subscribe(handler); var message = new GenericMessage("test"); await directChannel.SendAsync(message); for (var i = 0; i < 10_000_000; i++) { await directChannel.SendAsync(message); } if (handler.Count != 10_000_000 + 1) { throw new InvalidOperationException("Handler count invalid"); } }
public void TestSendFourHandlers_10_000_000() { var channel = new DirectChannel(provider.GetService <IApplicationContext>()); var count1 = new CounterHandler(); var count2 = new CounterHandler(); var count3 = new CounterHandler(); var count4 = new CounterHandler(); channel.Subscribe(count1); channel.Subscribe(count2); channel.Subscribe(count3); channel.Subscribe(count4); var message = Message.Create("test"); for (var i = 0; i < 10000000; i++) { channel.Send(message); } Assert.Equal(10000000 / 4, count1.Count); Assert.Equal(10000000 / 4, count2.Count); Assert.Equal(10000000 / 4, count3.Count); Assert.Equal(10000000 / 4, count4.Count); }
public void CreateInvalidGlobalWildcardHandlerChannel() { var dispatcher = new ChannelDispatcher(); var handler1 = new CounterHandler(); dispatcher.Register("/services/test/**/edit", handler1); }