public void BatchDispatchTriggeringByTimeoutTest()
        {
            var dispatcher = new EventDispatcher(_logFactory, "testBC");
            var handler    = new EventHandlerWithBatchSupport();

            dispatcher.Wire(
                "testBC",
                handler,
                3,
                1,
                typeof(FakeBatchContext),
                h => ((EventHandlerWithBatchSupport)h).OnBatchStart(),
                (h, c) => ((EventHandlerWithBatchSupport)h).OnBatchFinish((FakeBatchContext)c));
            dispatcher.Dispatch("testBC", new[]
            {
                Tuple.Create <object, AcknowledgeDelegate>(new DateTime(2016, 3, 1), (delay, acknowledge) => { Tuple.Create(delay, acknowledge); }),
                Tuple.Create <object, AcknowledgeDelegate>(new DateTime(2016, 3, 2), (delay, acknowledge) => { })
            });

            Assert.AreEqual(0, handler.HandledEvents.Count, "Events were delivered before batch apply timeoout");

            Thread.Sleep(2000);

            Assert.AreEqual(2, handler.HandledEvents.Count, "Not all events were delivered");
            Assert.True(handler.BatchStartReported, "Batch start callback was not called");
            Assert.True(handler.BatchFinishReported, "Batch after apply  callback was not called");
            Assert.That(
                handler.HandledEvents.Select(t => t.Item2),
                Is.EqualTo(new object[] { handler.LastCreatedBatchContext, handler.LastCreatedBatchContext }),
                "Batch context was not the same for all evants in the batch");
        }
        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);
                }
            }
        }
        public void BatchDispatchUnackTest()
        {
            var dispatcher            = new EventDispatcher(_logFactory, "testBC");
            var handler               = new EventHandlerWithBatchSupport(1);
            Tuple <long, bool> result = null;

            dispatcher.Wire(
                "testBC",
                handler,
                3,
                0,
                typeof(FakeBatchContext),
                h => ((EventHandlerWithBatchSupport)h).OnBatchStart(),
                (h, c) => ((EventHandlerWithBatchSupport)h).OnBatchFinish((FakeBatchContext)c));
            dispatcher.Dispatch("testBC", new[]
            {
                Tuple.Create <object, AcknowledgeDelegate>(new DateTime(2016, 3, 1), (delay, acknowledge) => { result = Tuple.Create(delay, acknowledge); }),
                Tuple.Create <object, AcknowledgeDelegate>(new DateTime(2016, 3, 2), (delay, acknowledge) => { }),
            });

            Assert.AreEqual(0, handler.HandledEvents.Count, "Events were delivered before batch is filled");

            dispatcher.Dispatch("testBC", new[]
            {
                Tuple.Create <object, AcknowledgeDelegate>(new DateTime(2016, 3, 3), (delay, acknowledge) => { })
            });

            Assert.AreEqual(3, handler.HandledEvents.Count, "Not all events were delivered");
            Assert.True(handler.BatchStartReported, "Batch start callback was not called");
            Assert.True(handler.BatchFinishReported, "Batch after apply  callback was not called");
            Assert.That(
                handler.HandledEvents.Select(t => t.Item2),
                Is.EqualTo(new object[] { handler.LastCreatedBatchContext, handler.LastCreatedBatchContext, handler.LastCreatedBatchContext }),
                "Batch context was not the same for all evants in the batch");
            Assert.False(result.Item2, "failed event was acked");
            Assert.AreEqual(10, result.Item1, "failed event retry timeout was wrong");
        }