Exemplo n.º 1
0
        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);
                }
            }
        }
Exemplo n.º 2
0
        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);
                }
            }
        }
Exemplo n.º 3
0
        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" }));
                }
            }
        }
Exemplo n.º 4
0
        public void FailingCommandTest()
        {
            var  dispatcher = new CommandDispatcher(_logFactory, "testBC");
            var  handler    = new CommandsHandler(true);
            bool ack        = true;

            dispatcher.Wire(handler);
            dispatcher.Dispatch("testCommand", (delay, acknowledge) => { ack = acknowledge; }, new Endpoint(), "route");

            Assert.False(ack, "Failed command was not unacked");
        }
Exemplo n.º 5
0
        public void MultipleHandlersAreNotAllowedDispatchTest()
        {
            var dispatcher = new CommandDispatcher(_logFactory, "testBC");
            var handler1   = new CommandsHandler();
            var handler2   = new CommandsHandler();

            Assert.Throws <InvalidOperationException>(() =>
            {
                dispatcher.Wire(handler1);
                dispatcher.Wire(handler2);
            });
        }
Exemplo n.º 6
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));
            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"));
                }
            }
        }
Exemplo n.º 7
0
        public void HandleTest()
        {
            var  dispatcher = new CommandDispatcher(_logFactory, "testBC");
            var  handler    = new CommandsHandler();
            var  now        = DateTime.UtcNow;
            bool ack1       = false;
            bool ack2       = false;
            bool ack3       = false;

            dispatcher.Wire(handler);
            dispatcher.Dispatch("test", (delay, acknowledge) => { ack1 = acknowledge; }, new Endpoint(), "route");
            dispatcher.Dispatch(1, (delay, acknowledge) => { ack2 = acknowledge; }, new Endpoint(), "route");
            dispatcher.Dispatch(now, (delay, acknowledge) => { ack3 = acknowledge; }, new Endpoint(), "route");

            Assert.That(handler.HandledCommands, Is.EquivalentTo(new object[] { "test", 1, now }), "Some commands were not dispatched");
            Assert.True(ack1, "String command was not acked");
            Assert.True(ack2, "Int command was not acked");
            Assert.True(ack3, "DateTime command was not acked");
        }
Exemplo n.º 8
0
        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");
                    }
                }
            }
        }
Exemplo n.º 9
0
        public void ProcessTest()
        {
            var testProcess    = new TestProcess();
            var commandHandler = new CommandsHandler();

            using (var engine = new InMemoryCqrsEngine(
                       _logFactory,
                       Register.BoundedContext("local")
                       .ListeningCommands(typeof(string)).On("commands1").WithLoopback()
                       .PublishingEvents(typeof(int)).With("events")
                       .WithCommandsHandler(commandHandler)
                       .WithProcess(testProcess)
                       ))
            {
                engine.StartAll();
                Assert.True(testProcess.Started.WaitOne(1000), "process was not started");
                Thread.Sleep(1000);
            }

            Assert.True(testProcess.Disposed.WaitOne(1000), "process was not disposed on engine dispose");
            Assert.True(commandHandler.HandledCommands.Count > 0, "commands sent by process were not processed");
        }
Exemplo n.º 10
0
        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");
                }
            }
        }