public void ReplayEventsTest(Type[] types, int expectedReplayCount)
        {
            var log                 = MockRepository.GenerateMock <ILog>();
            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()
                                                      .LogTo(type => log)
                                                      .UsingInMemoryPersistence()
                                                      .InitializeStorageEngine()
                                                      .UsingJsonSerialization()
                                                      .UsingSynchronousDispatchScheduler()
                                                      .DispatchTo(dispatchCommits));

            using (
                var engine = new InMemoryCqrsEngine(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", types);
                Thread.Sleep(2000);
                Console.WriteLine("Disposing...");
            }

            Assert.That(eventsListener.Handled.Count, Is.EqualTo(2 + expectedReplayCount), "Wrong number of events was replayed");
        }
        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");
        }
        public void GetEventStoreTest(bool getES)
        {
            var log                 = MockRepository.GenerateMock <ILog>();
            var eventsListener      = new EventsListener();
            var localBoundedContext = LocalBoundedContext.Named("local")
                                      .PublishingEvents(typeof(TestAggregateRootNameChangedEvent), typeof(TestAggregateRootCreatedEvent)).To("events").RoutedTo("events")
                                      .ListeningCommands(typeof(string)).On("commands1").RoutedFromSameEndpoint()
                                      .WithCommandsHandler <EsCommandHandler>();

            if (getES)
            {
                var eventStoreConnection = EventStoreConnection.Create(ConnectionSettings.Default, new IPEndPoint(IPAddress.Loopback, 1113));
                eventStoreConnection.Connect();
                localBoundedContext.WithEventStore(eventStoreConnection);
            }
            else
            {
                localBoundedContext.WithEventStore(dispatchCommits => Wireup.Init()
                                                   .LogTo(type => log)
                                                   .UsingInMemoryPersistence()
                                                   .InitializeStorageEngine()
                                                   .UsingJsonSerialization()
                                                   .UsingSynchronousDispatchScheduler()
                                                   .DispatchTo(dispatchCommits));
            }
            using (var engine = new InMemoryCqrsEngine(localBoundedContext, LocalBoundedContext.Named("projections").WithProjection(eventsListener, "local")))
            {
                var guid = Guid.NewGuid();
                engine.SendCommand(guid + ":create", "local");
                engine.SendCommand(guid + ":changeName:newName", "local");

                Thread.Sleep(5000);
                Console.WriteLine("Disposing...");
            }

            Assert.That(eventsListener.Handled.Select(e => e.GetType()).ToArray(), Is.EqualTo(new[] { typeof(TestAggregateRootCreatedEvent), typeof(TestAggregateRootNameChangedEvent) }), "Events were not stored or published");
            Console.WriteLine("Dispose completed.");
        }