public async Task When_handler_throws_Then_invoke_exception_callback()
        {
            using (IStoreEvents eventStore = Wireup.Init().UsingInMemoryPersistence().Build())
            {
                var projectedEvents = new List<DomainEventMessage<TestEvent>>();
                var handlerModule = new TestHandlerModule(projectedEvents);

                using (var host = new DurableCommitDispatcher(
                    new EventStoreClient(new PollingClient(eventStore.Advanced)),
                    new InMemoryCheckpointRepository(),
                    handlerModule.DispatchCommit))
                {
                    await host.Start();
                    Guid streamId = Guid.NewGuid();
                    Guid commitId = Guid.NewGuid();
                    Task<ICommit> commitProjected = host
                        .CommitsProjectedStream
                        .Take(1)
                        .ToTask();

                    using (IEventStream stream = eventStore.CreateStream(streamId))
                    {
                        stream.Add(new EventMessage { Body = new TestEventThatThrows() });
                        stream.CommitChanges(commitId);
                    }
                    host.PollNow();

                    Func<Task> act = async () => await commitProjected;

                    act.ShouldThrow<Exception>();

                }
            }
        }
        public async Task When_new_commit_then_should_dispatch()
        {
            using (IStoreEvents eventStore = Wireup.Init().UsingInMemoryPersistence().Build())
            {
                var dispatchedEvents = new List <EventMessage <TestEvent> >();
                var handlerModule    = new TestHandlerModule(dispatchedEvents);

                using (var host = new DurableCommitDispatcher(
                           new EventStoreClient(eventStore.Advanced),
                           new InMemoryCheckpointRepository(),
                           new HandlerResolver(handlerModule)))
                {
                    var projectedCommits = host
                                           .ProjectedCommits
                                           .Replay();

                    using (projectedCommits.Connect())
                    {
                        await host.Start();

                        var streamId = Guid.NewGuid().ToString().FormatStreamIdWithBucket();

                        Guid commitId = Guid.NewGuid();

                        Task <ICommit> commitProjected = projectedCommits
                                                         .Take(1)
                                                         .ToTask();

                        using (IEventStream stream = eventStore.CreateStream(streamId))
                        {
                            stream.Add(new global::NEventStore.EventMessage {
                                Body = new TestEvent()
                            });
                            stream.CommitChanges(commitId);
                        }
                        host.PollNow();
                        await commitProjected;

                        dispatchedEvents.Count.Should().Be(1);
                        dispatchedEvents[0].Commit().Should().NotBeNull();
                        dispatchedEvents[0].Headers.Should().NotBeNull();
                        dispatchedEvents[0].Version.Should().Be(1);
                        dispatchedEvents[0].DomainEvent.Should().BeOfType <TestEvent>();
                    }
                }
            }
        }
        public async Task When_handler_throws_Then_invoke_exception_callback()
        {
            using (IStoreEvents eventStore = Wireup.Init().UsingInMemoryPersistence().Build())
            {
                var projectedEvents = new List <EventMessage <TestEvent> >();
                var handlerModule   = new TestHandlerModule(projectedEvents);

                using (var host = new DurableCommitDispatcher(
                           new EventStoreClient(eventStore.Advanced),
                           new InMemoryCheckpointRepository(),
                           new HandlerResolver(handlerModule)))
                {
                    var projectedCommits = host
                                           .ProjectedCommits
                                           .Replay();

                    using (projectedCommits.Connect())
                    {
                        await host.Start();

                        var streamId = Guid.NewGuid().ToString().FormatStreamIdWithBucket();

                        Guid commitId = Guid.NewGuid();

                        Task <ICommit> commitProjected = projectedCommits
                                                         .Take(1)
                                                         .ToTask();

                        using (IEventStream stream = eventStore.CreateStream(streamId))
                        {
                            stream.Add(new global::NEventStore.EventMessage {
                                Body = new TestEventThatThrows()
                            });
                            stream.CommitChanges(commitId);
                        }
                        host.PollNow();

                        Func <Task> act = async() => await commitProjected;

                        act.ShouldThrow <Exception>();
                    }
                }
            }
        }
        public async Task When_new_commit_then_should_dispatch()
        {
            using (IStoreEvents eventStore = Wireup.Init().UsingInMemoryPersistence().Build())
            {
                var dispatchedEvents = new List<DomainEventMessage<TestEvent>>();
                var handlerModule = new TestHandlerModule(dispatchedEvents);

                using (var host = new DurableCommitDispatcher(
                    new EventStoreClient(new PollingClient(eventStore.Advanced)),
                    new InMemoryCheckpointRepository(),
                    handlerModule))
                {
                    await host.Start();
                    Guid streamId = Guid.NewGuid();
                    Guid commitId = Guid.NewGuid();
                    Task<ICommit> commitProjected = host
                        .CommitsProjectedStream
                        .Take(1)
                        .ToTask();

                    using (IEventStream stream = eventStore.CreateStream(streamId))
                    {
                        stream.Add(new EventMessage {Body = new TestEvent()});
                        stream.CommitChanges(commitId);
                    }
                    host.PollNow();
                    await commitProjected;

                    dispatchedEvents.Count.Should().Be(1);
                    dispatchedEvents[0].Commit.Should().NotBeNull();
                    dispatchedEvents[0].EventHeaders.Should().NotBeNull();
                    dispatchedEvents[0].Version.Should().Be(1);
                    dispatchedEvents[0].DomainEvent.Should().BeOfType<TestEvent>();
                }
            }
        }