Exemplo n.º 1
0
        public void ReplayEventsRmqTest()
        {
            var endpointResolver = MockRepository.GenerateMock <IEndpointResolver>();

            endpointResolver.Expect(r => r.Resolve("local", "local", "commands", typeof(string), RouteType.Commands)).Return(new Endpoint("rmq", "commandsExchange", "commands", true, "json"));
            endpointResolver.Expect(r => r.Resolve("local", "local", "events", typeof(TestAggregateRootNameChangedEvent), RouteType.Events)).Return(new Endpoint("rmq", "eventsExchange", "events", true, "json"));
            endpointResolver.Expect(r => r.Resolve("local", "local", "events", typeof(TestAggregateRootCreatedEvent), RouteType.Events)).Return(new Endpoint("rmq", "eventsExchange", "events", true, "json"));


            var transports = new Dictionary <string, TransportInfo> {
                { "rmq", new TransportInfo("localhost", "guest", "guest", null, "RabbitMq") }
            };
            var messagingEngine = new MessagingEngine(new TransportResolver(transports), new RabbitMqTransportFactory());


            var eventsListener      = new EventsListener();
            var localBoundedContext = LocalBoundedContext.Named("local")
                                      .PublishingEvents(typeof(TestAggregateRootNameChangedEvent), typeof(TestAggregateRootCreatedEvent)).To("events").RoutedTo("events")
                                      .ListeningCommands(typeof(string)).On("commands").RoutedFromSameEndpoint()
                                      .ListeningInfrastructureCommands().On("commands").RoutedFromSameEndpoint()
                                      .WithCommandsHandler <EsCommandHandler>()
                                      .WithEventStore(dispatchCommits => Wireup.Init()
                                                      .LogToOutputWindow()
                                                      .UsingInMemoryPersistence()
                                                      .InitializeStorageEngine()
                                                      .UsingJsonSerialization()
                                                      .UsingSynchronousDispatchScheduler()
                                                      .DispatchTo(dispatchCommits));

            using (messagingEngine)
            {
                using (
                    var engine = new CqrsEngine(messagingEngine, endpointResolver, localBoundedContext,
                                                LocalBoundedContext.Named("projections").WithProjection(eventsListener, "local")))
                {
                    var guid = Guid.NewGuid();
                    engine.SendCommand(guid + ":create", "local");
                    engine.SendCommand(guid + ":changeName:newName", "local");

                    Thread.Sleep(2000);
                    //engine.SendCommand(new ReplayEventsCommand { Destination = "events", From = DateTime.MinValue }, "local");
                    engine.ReplayEvents("local");
                    Thread.Sleep(2000);
                    Console.WriteLine("Disposing...");
                }
            }


            Assert.That(eventsListener.Handled.Count, Is.EqualTo(4), "Events were not redelivered");
        }
Exemplo n.º 2
0
        public void CqrsEngineTest()
        {
            var serializationManager = new SerializationManager();

            serializationManager.RegisterSerializerFactory(new JsonSerializerFactory());
            var transportResolver =
                new TransportResolver(new Dictionary <string, TransportInfo>
            {
                { "test", new TransportInfo("localhost", "guest", "guest", null, "RabbitMq") }
            });
            var messagingEngine = new MessagingEngine(transportResolver, new RabbitMqTransportFactory());


            using (messagingEngine)
            {
                var cqrsEngine = new CqrsEngine(messagingEngine, new FakeEndpointResolver(),
                                                LocalBoundedContext.Named("integration")
                                                .PublishingEvents(typeof(int)).To("eventExchange").RoutedTo("eventQueue")
                                                .ListeningCommands(typeof(string)).On("commandExchange").RoutedFrom("commandQueue")
                                                .WithCommandsHandler <CommandsHandler>(),
                                                LocalBoundedContext.Named("bc").WithProjection <EventListener>("integration")
                                                );

/*                var cqrsEngine = new CqrsEngine(messagingEngine, new RabbitMqConventionEndpointResolver("test","json",new EndpointResolver(new Dictionary<string, Endpoint>())),
 *                                              LocalBoundedContext.Named("integration")
 *                                                                 .PublishingEvents(typeof (int)).To("events").RoutedToSameEndpoint()
 *                                                                 .ListeningCommands(typeof (string)).On("commands").RoutedFromSameEndpoint()
 *                                                                 .WithCommandsHandler<CommandsHandler>(),
 *                                              LocalBoundedContext.Named("bc").WithProjection<EventListener>("integration")
 *                  );*/
                /* var c=new commandSender(messagingEngine, RemoteBoundedContext.Named("integration")
                 *                                  .ListeningCommands(typeof(TestCommand)).On(new Endpoint())
                 *                                  .PublishingEvents(typeof(TransferCreatedEvent)).To(new Endpoint()),
                 *                                  LocalBoundedContext.Named("testBC")
                 *                                  .ListeningCommands(typeof(TestCommand)).On(new Endpoint("test", "unistream.u1.commands", true))
                 *                                  .ListeningCommands(typeof(int)).On(new Endpoint("test", "unistream.u1.commands", true))
                 *                                  .PublishingEvents(typeof (int)).To(new Endpoint()).RoutedTo(new Endpoint())
                 *                                  .PublishingEvents(typeof (string)).To(new Endpoint())
                 *                                  .WithEventStore(dispatchCommits => Wireup.Init()
                 *                                                                           .LogToOutputWindow()
                 *                                                                           .UsingInMemoryPersistence()
                 *                                                                           .InitializeStorageEngine()
                 *                                                                           .UsingJsonSerialization()
                 *                                                                           .UsingSynchronousDispatchScheduler()
                 *                                                                               .DispatchTo(dispatchCommits))
                 *                              ); */



                //  messagingEngine.Send("test", new Endpoint("test", "unistream.u1.commands", true,"json"));
                cqrsEngine.SendCommand("test", "integration");
                Thread.Sleep(3000);
            }
        }
Exemplo n.º 3
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");
                    }
                }
            }
        }
Exemplo n.º 4
0
        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");
                }
            }
        }
Exemplo n.º 5
0
        public void Test()
        {
            var endpointProvider = MockRepository.GenerateMock <IEndpointProvider>();

            endpointProvider.Expect(p => p.Contains(null)).IgnoreArguments().Return(false);

            var messagingEngine = MockRepository.GenerateStrictMock <IMessagingEngine>();

            string error;

            messagingEngine.Expect(e => e.VerifyEndpoint(new Endpoint(), EndpointUsage.None, false, out error)).IgnoreArguments().Return(true).Repeat.Times(12);
            //subscription for remote events
            messagingEngine.Expect(e => e.Subscribe(
                                       Arg <Endpoint> .Is.Anything,
                                       Arg <CallbackDelegate <object> > .Is.Anything,
                                       Arg <Action <string, AcknowledgeDelegate> > .Is.Anything,
                                       Arg <string> .Is.Equal("remoteEvents"),
                                       Arg <int> .Is.Equal(0),
                                       Arg <Type[]> .List.Equal(new [] { typeof(int), typeof(long) }))).Return(Disposable.Empty);

            //subscription for local events
            messagingEngine.Expect(e => e.Subscribe(
                                       Arg <Endpoint> .Is.Anything,
                                       Arg <CallbackDelegate <object> > .Is.Anything,
                                       Arg <Action <string, AcknowledgeDelegate> > .Is.Anything,
                                       Arg <string> .Is.Equal("localEvents"),
                                       Arg <int> .Is.Equal(0),
                                       Arg <Type[]> .List.Equal(new [] { typeof(bool) }))).Return(Disposable.Empty);

            //subscription for localCommands
            messagingEngine.Expect(e => e.Subscribe(
                                       Arg <Endpoint> .Is.Anything,
                                       Arg <CallbackDelegate <object> > .Is.Anything,
                                       Arg <Action <string, AcknowledgeDelegate> > .Is.Anything,
                                       Arg <string> .Is.Equal("localCommands"),
                                       Arg <int> .Is.Equal(0),
                                       Arg <Type[]> .List.Equal(new[] { typeof(string), typeof(DateTime) }))).Return(Disposable.Empty);

            //send command to remote BC
            messagingEngine.Expect(e => e.Send(
                                       Arg <object> .Is.Equal("testCommand"),
                                       Arg <Endpoint> .Is.Anything,
                                       Arg <string> .Is.Equal("remoteCommands")
                                       ));

            //publish event from local BC
            messagingEngine.Expect(e => e.Send(
                                       Arg <object> .Is.Equal(true),
                                       Arg <Endpoint> .Is.Anything,
                                       Arg <string> .Is.Equal("localEvents")
                                       ));


            using (var ce = new CqrsEngine(messagingEngine,
                                           new RabbitMqConventionEndpointResolver("tr1", "protobuf", endpointProvider),
                                           LocalBoundedContext.Named("operations")
                                           .PublishingEvents(typeof(bool)).To("localEvents").RoutedToSameEndpoint()
                                           .ListeningCommands(typeof(string), typeof(DateTime)).On("localCommands").RoutedFromSameEndpoint(),

                                           RemoteBoundedContext.Named("integration", "operations")
                                           .PublishingEvents(typeof(int), typeof(long)).To("remoteEvents")
                                           .ListeningCommands(typeof(string)).On("remoteCommands"))
                   )
            {
                ce.SendCommand("testCommand", "integration");
                ce.BoundedContexts.Find(bc => bc.Name == "operations").EventsPublisher.PublishEvent(true);
            }
        }