public void CommandLoggingInterceptorDoesNotBreakProcessingChain() { var commandLoggingInterceptor = new DefaultCommandLoggingInterceptor(_logFactory); var commandSimpleInterceptor = new CommandSimpleInterceptor(); var commandsHandler = new CommandsHandler(); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null) } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.CommandInterceptors(commandLoggingInterceptor, commandSimpleInterceptor), Register.BoundedContext("test1") .ListeningCommands(typeof(int)).On("lykke-wallet-events") .WithCommandsHandler(commandsHandler))) { engine.StartSubscribers(); messagingEngine.Send(1, new Endpoint("InMemory", "lykke-wallet-events", serializationFormat: SerializationFormat.Json)); Thread.Sleep(1000); Assert.True(commandSimpleInterceptor.Intercepted); } } }
public void TwoSimpleEventInterceptorsTest() { var simpleEventInterceptorOne = new EventSimpleInterceptor(); var simpleEventInterceptorTwo = new EventSimpleInterceptor(); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null) } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.EventInterceptors(simpleEventInterceptorOne), Register.EventInterceptors(simpleEventInterceptorTwo), Register.Saga <TestSaga>("test2") .ListeningEvents(typeof(string)).From("lykke-wallet").On("lykke-wallet-events"))) { engine.StartSubscribers(); messagingEngine.Send("2", new Endpoint("InMemory", "lykke-wallet-events", serializationFormat: SerializationFormat.Json)); Thread.Sleep(1000); Assert.True(simpleEventInterceptorOne.Intercepted); Assert.True(simpleEventInterceptorTwo.Intercepted); Assert.NotNull(simpleEventInterceptorOne.InterceptionTimestamp); Assert.NotNull(simpleEventInterceptorTwo.InterceptionTimestamp); Assert.True(simpleEventInterceptorOne.InterceptionTimestamp < simpleEventInterceptorTwo.InterceptionTimestamp); Assert.True(TestSaga.Messages.Contains("2")); } } }
public void EventLoggingInterceptorTestForDefaultLogging() { var eventLoggingInterceptor = new DefaultEventLoggingInterceptor(_logFactory); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null) } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.EventInterceptors(eventLoggingInterceptor), Register.Saga <TestSaga>("test1") .ListeningEvents(typeof(string)).From("lykke-wallet").On("lykke-wallet-events"))) { engine.StartSubscribers(); using (var writer = new StringWriter()) { var prevOut = Console.Out; Console.SetOut(writer); messagingEngine.Send("1", new Endpoint("InMemory", "lykke-wallet-events", serializationFormat: SerializationFormat.Json)); Thread.Sleep(1000); Console.SetOut(prevOut); var output = writer.ToString(); Assert.IsFalse(output.IsNullOrEmpty(), "Event was not logged"); } } } }
public void EventLoggingInterceptorDoesNotBreakProcessingChain() { var eventLoggingInterceptor = new DefaultEventLoggingInterceptor(_logFactory); var simpleEventInterceptor = new EventSimpleInterceptor(); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null) } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.EventInterceptors(eventLoggingInterceptor, simpleEventInterceptor), Register.Saga <TestSaga>("test1") .ListeningEvents(typeof(string)).From("lykke-wallet").On("lykke-wallet-events"))) { engine.StartSubscribers(); messagingEngine.Send("1", new Endpoint("InMemory", "lykke-wallet-events", serializationFormat: SerializationFormat.Json)); Thread.Sleep(1000); Assert.True(simpleEventInterceptor.Intercepted); } } }
public void TwoSimpleCommandInterceptorsTest() { var commandSimpleInterceptorOne = new CommandSimpleInterceptor(); var commandSimpleInterceptorTwo = new CommandSimpleInterceptor(); var commandsHandler = new CommandsHandler(); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null) } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.CommandInterceptors(commandSimpleInterceptorOne, commandSimpleInterceptorTwo), Register.BoundedContext("swift-cashout") .ListeningCommands(typeof(int)).On("lykke-wallet-events") .WithCommandsHandler(commandsHandler))) { engine.StartSubscribers(); messagingEngine.Send(1, new Endpoint("InMemory", "lykke-wallet-events", serializationFormat: SerializationFormat.Json)); Thread.Sleep(1000); Assert.True(commandSimpleInterceptorOne.Intercepted); Assert.True(commandSimpleInterceptorTwo.Intercepted); Assert.NotNull(commandSimpleInterceptorOne.InterceptionTimestamp); Assert.NotNull(commandSimpleInterceptorTwo.InterceptionTimestamp); Assert.True(commandSimpleInterceptorOne.InterceptionTimestamp < commandSimpleInterceptorTwo.InterceptionTimestamp); Assert.True(commandsHandler.HandledCommands.Count > 0); } } }
public void EventLoggingInterceptorTest() { int eventLoggedCount = 0; var eventLoggingInterceptor = new CustomEventLoggingInterceptor( _logFactory, new Dictionary <Type, EventLoggingDelegate> { { typeof(string), (l, h, e) => ++ eventLoggedCount } }); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null) } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.EventInterceptors(eventLoggingInterceptor), Register.Saga <TestSaga>("test1") .ListeningEvents(typeof(string)).From("lykke-wallet").On("lykke-wallet-events"))) { engine.StartSubscribers(); messagingEngine.Send("1", new Endpoint("InMemory", "lykke-wallet-events", serializationFormat: SerializationFormat.Json)); Thread.Sleep(1000); Assert.True(eventLoggedCount > 0, "Event was not logged"); Assert.True(eventLoggedCount == 1, "Event was logged more than once"); } } }
public void ListenSameCommandOnDifferentEndpointsTest() { var commandHandler = new CommandsHandler(); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null, "InMemory") } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.BoundedContext("bc") .PublishingEvents(typeof(int)).With("eventExchange") .ListeningCommands(typeof(string)).On("exchange1") .ListeningCommands(typeof(string)).On("exchange2") .WithCommandsHandler(commandHandler))) { engine.StartPublishers(); engine.StartSubscribers(); messagingEngine.Send("test1", new Endpoint("InMemory", "exchange1", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("test2", new Endpoint("InMemory", "exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("test3", new Endpoint("InMemory", "exchange3", serializationFormat: SerializationFormat.Json)); Thread.Sleep(7000); Assert.That(commandHandler.HandledCommands, Is.EquivalentTo(new[] { "test1", "test2" })); } } }
public void FluentApiTest() { var endpointProvider = new Mock <IEndpointProvider>(); endpointProvider.Setup(r => r.Get("high")).Returns(new Endpoint("InMemory", "high", true, SerializationFormat.Json)); endpointProvider.Setup(r => r.Get("low")).Returns(new Endpoint("InMemory", "low", true, SerializationFormat.Json)); endpointProvider.Setup(r => r.Get("medium")).Returns(new Endpoint("InMemory", "medium", true, SerializationFormat.Json)); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null, "InMemory") }, { "rmq", new TransportInfo("none", "none", "none", null, "InMemory") } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, endpointProvider.Object, Register.BoundedContext("bc") .PublishingCommands(typeof(string)).To("operations").With("operationsCommandsRoute") .ListeningCommands(typeof(string)).On("commandsRoute") //same as .PublishingCommands(typeof(string)).To("bc").With("selfCommandsRoute") .WithLoopback("selfCommandsRoute") .PublishingEvents(typeof(int)).With("eventsRoute") //explicit prioritization .ListeningCommands(typeof(string)).On("explicitlyPrioritizedCommandsRoute") .Prioritized(lowestPriority: 2) .WithEndpoint("high").For(key => key.Priority == 0) .WithEndpoint("medium").For(key => key.Priority == 1) .WithEndpoint("low").For(key => key.Priority == 2) //resolver based prioritization .ListeningCommands(typeof(string)).On("prioritizedCommandsRoute") .Prioritized(lowestPriority: 2) .WithEndpointResolver(new InMemoryEndpointResolver()) .WithCommandsHandler(typeof(CommandsHandler)) .ProcessingOptions("explicitlyPrioritizedCommandsRoute").MultiThreaded(10) .ProcessingOptions("prioritizedCommandsRoute").MultiThreaded(10).QueueCapacity(1024), Register.Saga <TestSaga>("saga") .PublishingCommands(typeof(string)).To("operations").With("operationsCommandsRoute") .ListeningEvents(typeof(int)).From("operations").On("operationEventsRoute"), Register.DefaultRouting .PublishingCommands(typeof(string)).To("operations").With("defaultCommandsRoute") .PublishingCommands(typeof(int)).To("operations").With("defaultCommandsRoute"), Register.DefaultEndpointResolver( new RabbitMqConventionEndpointResolver("rmq", SerializationFormat.Json)) )) { engine.StartPublishers(); engine.StartSubscribers(); } } }
public void PrioritizedCommandsProcessingTest() { var endpointProvider = new Mock <IEndpointProvider>(); endpointProvider.Setup(r => r.Get("exchange1")).Returns(new Endpoint("InMemory", "bc.exchange1", true, SerializationFormat.Json)); endpointProvider.Setup(r => r.Get("exchange2")).Returns(new Endpoint("InMemory", "bc.exchange2", true, SerializationFormat.Json)); var commandHandler = new CommandsHandler(false, 100); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null, "InMemory") } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, endpointProvider.Object, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.BoundedContext("bc") .PublishingEvents(typeof(int)).With("eventExchange")//.WithLoopback("eventQueue") .ListeningCommands(typeof(string)).On("commandsRoute") .Prioritized(lowestPriority: 1) .WithEndpoint("exchange1").For(key => key.Priority == 1) .WithEndpoint("exchange2").For(key => key.Priority == 2) .ProcessingOptions("commandsRoute").MultiThreaded(2) .WithCommandsHandler(commandHandler))) { engine.StartPublishers(); engine.StartSubscribers(); messagingEngine.Send("low1", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low2", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low3", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low4", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low5", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low6", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low7", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low8", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low9", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("low10", new Endpoint("InMemory", "bc.exchange2", serializationFormat: SerializationFormat.Json)); messagingEngine.Send("high", new Endpoint("InMemory", "bc.exchange1", serializationFormat: SerializationFormat.Json)); Thread.Sleep(2000); Assert.True(commandHandler.HandledCommands.Take(2).Any(c => (string)c == "high")); } } }
public void SagaTest() { var commandHandler = new CustomCommandsHandler(); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "rmq", new TransportInfo("amqp://localhost/LKK", "guest", "guest", "None", "RabbitMq") } }), new RabbitMqTransportFactory(_logFactory))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver( new RabbitMqConventionEndpointResolver("rmq", SerializationFormat.Json, environment: "dev")), Register.BoundedContext("operations") .PublishingCommands(typeof(CreateCashOutCommand)).To("lykke-wallet").With("operations-commands") .ListeningEvents(typeof(CashOutCreatedEvent)).From("lykke-wallet").On("lykke-wallet-events"), Register.BoundedContext("lykke-wallet") .FailedCommandRetryDelay((long)TimeSpan.FromSeconds(2).TotalMilliseconds) .ListeningCommands(typeof(CreateCashOutCommand)).On("operations-commands") .PublishingEvents(typeof(CashOutCreatedEvent)).With("lykke-wallet-events") .WithCommandsHandler(commandHandler), Register.Saga <TestSaga>("swift-cashout") .ListeningEvents(typeof(CashOutCreatedEvent)).From("lykke-wallet").On("lykke-wallet-events"), Register.DefaultRouting.PublishingCommands(typeof(CreateCashOutCommand)).To("lykke-wallet") .With("operations-commands")) ) { engine.StartPublishers(); engine.StartSubscribers(); engine.SendCommand(new CreateCashOutCommand { Payload = "test data" }, null, "lykke-wallet"); Assert.True(TestSaga.Complete.WaitOne(2000), "Saga has not got events or failed to send command"); } } }
public void CommandLoggingInterceptorTestForNoLogging() { var commandLoggingInterceptor = new CustomCommandLoggingInterceptor( _logFactory, new Dictionary <Type, CommandLoggingDelegate> { { typeof(int), null } }); var commandsHandler = new CommandsHandler(); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null) } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.CommandInterceptors(commandLoggingInterceptor), Register.BoundedContext("test1") .ListeningCommands(typeof(int)).On("lykke-wallet-events") .WithCommandsHandler(commandsHandler))) { engine.StartSubscribers(); using (var writer = new StringWriter()) { var prevOut = Console.Out; Console.SetOut(writer); messagingEngine.Send(1, new Endpoint("InMemory", "lykke-wallet-events", serializationFormat: SerializationFormat.Json)); Thread.Sleep(1000); Console.SetOut(prevOut); var output = writer.ToString(); Assert.True(output.IsNullOrEmpty(), "Command was logged"); } } } }
public void BatchDispatchUnackRmqTest() { var handler = new EventHandlerWithBatchSupport(1); var endpointProvider = new Mock <IEndpointProvider>(); using ( var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "RabbitMq", new TransportInfo("amqp://localhost", "guest", "guest", null, "RabbitMq") } }), new RabbitMqTransportFactory(_logFactory))) { messagingEngine.CreateTemporaryDestination("RabbitMq", null); var endpoint = new Endpoint("RabbitMq", "testExchange", "testQueue", true, SerializationFormat.Json); endpointProvider.Setup(r => r.Get("route")).Returns(endpoint); endpointProvider.Setup(r => r.Contains("route")).Returns(true); using (var engine = new CqrsEngine( _logFactory, new DefaultDependencyResolver(), messagingEngine, endpointProvider.Object, false, Register.BoundedContext("bc") .ListeningEvents(typeof(DateTime)).From("other").On("route") .WithProjection(handler, "other", 1, 0, h => h.OnBatchStart(), (h, c) => h.OnBatchFinish(c) ))) { engine.StartSubscribers(); messagingEngine.Send(DateTime.Now, endpoint); Thread.Sleep(20000); } } }
private CqrsEngine CreateEngine(IComponentContext ctx, IMessagingEngine messagingEngine) { var rabbitMqConventionEndpointResolver = new RabbitMqConventionEndpointResolver( "RabbitMq", SerializationFormat.MessagePack, environment: _settings.EnvironmentName); var log = new LykkeLoggerAdapter <CqrsModule>(ctx.Resolve <ILogger <CqrsModule> >()); var engine = new CqrsEngine( log, ctx.Resolve <IDependencyResolver>(), messagingEngine, new DefaultEndpointProvider(), true, Register.DefaultEndpointResolver(rabbitMqConventionEndpointResolver), RegisterContext(), Register.CommandInterceptors(new DefaultCommandLoggingInterceptor(log)), Register.EventInterceptors(new DefaultEventLoggingInterceptor(log))); engine.StartSubscribers(); return(engine); }
public void CommandLoggingInterceptorTest() { int commandLoggedCount = 0; var commandLoggingInterceptor = new CustomCommandLoggingInterceptor( _logFactory, new Dictionary <Type, CommandLoggingDelegate> { { typeof(int), (l, h, c) => ++ commandLoggedCount } }); var commandsHandler = new CommandsHandler(); using (var messagingEngine = new MessagingEngine( _logFactory, new TransportResolver(new Dictionary <string, TransportInfo> { { "InMemory", new TransportInfo("none", "none", "none", null) } }))) { using (var engine = new CqrsEngine( _logFactory, messagingEngine, Register.DefaultEndpointResolver(new InMemoryEndpointResolver()), Register.CommandInterceptors(commandLoggingInterceptor), Register.BoundedContext("test1") .ListeningCommands(typeof(int)).On("lykke-wallet-events") .WithCommandsHandler(commandsHandler))) { engine.StartSubscribers(); messagingEngine.Send(1, new Endpoint("InMemory", "lykke-wallet-events", serializationFormat: SerializationFormat.Json)); Thread.Sleep(1000); Assert.True(commandLoggedCount > 0, "Command was not logged"); Assert.True(commandLoggedCount == 1, "Command was logged more than once"); } } }