Exemplo n.º 1
0
 public void ListenSameCommandOnDifferentEndpointsTest()
 {
     using (var messagingEngine = new MessagingEngine(
                _logFactory,
                new TransportResolver(new Dictionary <string, TransportInfo>
     {
         { "InMemory", new TransportInfo("none", "none", "none", null, "InMemory") }
     })))
     {
         var commandHandler = new CommandHandler();
         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.Start();
             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(6000);
             Assert.That(commandHandler.AcceptedCommands, Is.EquivalentTo(new[] { "test1", "test2" }));
         }
     }
 }
Exemplo n.º 2
0
        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")
                           .ListeningEvents(typeof(int)).From("operations").On("operationEventsRoute")
                           .ListeningCommands(typeof(string)).On("commandsRoute")
                           //same as .PublishingCommands(typeof(string)).To("bc").With("selfCommandsRoute")
                           .WithLoopback("selfCommandsRoute")
                           .PublishingEvents(typeof(int)).With("eventsRoute")
                           //same as.ListeningEvents(typeof(int)).From("bc").On("selfEventsRoute")
                           .WithLoopback("selfEventsRoute")

                           //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())
                           .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.Start();
                }
            }
        }
Exemplo n.º 3
0
        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));
            using (var messagingEngine = new MessagingEngine(
                       _logFactory,
                       new TransportResolver(new Dictionary <string, TransportInfo>
            {
                { "InMemory", new TransportInfo("none", "none", "none", null, "InMemory") }
            })))
            {
                var commandHandler = new CommandHandler(100);
                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.Start();
                    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);
                    Console.WriteLine(string.Join("\n", commandHandler.AcceptedCommands));
                    Assert.That(commandHandler.AcceptedCommands.Take(2).Any(c => (string)c == "high"), Is.True);
                }
            }
        }
Exemplo n.º 4
0
        public void SagaTest()
        {
            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)))
            {
                var commandHandler = new CommandHandler();
                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.Start();
                    engine.SendCommand(new CreateCashOutCommand {
                        Payload = "test data"
                    }, null, "lykke-wallet");

                    Assert.That(TestSaga.Complete.WaitOne(2000), Is.True,
                                "Saga has not got events or failed to send command");
                }
            }
        }
Exemplo n.º 5
0
        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.Start();
                    messagingEngine.Send(DateTime.Now, endpoint);
                    Thread.Sleep(20000);
                }
            }
        }
Exemplo n.º 6
0
        public void ContextUsesDefaultRouteForCommandPublishingIfItDoesNotHaveItsOwnTest()
        {
            var bcCommands      = new Endpoint("InMemory", "bcCommands", serializationFormat: SerializationFormat.Json);
            var defaultCommands = new Endpoint("InMemory", "defaultCommands", serializationFormat: SerializationFormat.Json);

            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("bc2")
                           .PublishingCommands(typeof(int)).To("bc1").With("bcCommands"),
                           Register.DefaultRouting
                           .PublishingCommands(typeof(string)).To("bc1").With("defaultCommands")
                           .PublishingCommands(typeof(int)).To("bc1").With("defaultCommands")))
                {
                    engine.Start();
                    var received = new AutoResetEvent(false);
                    using (messagingEngine.Subscribe(defaultCommands, o => received.Set(), s => { }, typeof(string)))
                    {
                        engine.SendCommand("test", "bc2", "bc1");
                        Assert.That(received.WaitOne(2000), Is.True, "not defined for context command was not routed with default route map");
                    }

                    using (messagingEngine.Subscribe(bcCommands, o => received.Set(), s => { }, typeof(int)))
                    {
                        engine.SendCommand(1, "bc2", "bc1");
                        Assert.That(received.WaitOne(2000), Is.True, "defined for context command was not routed with context route map");
                    }
                }
            }
        }