public void When_the_same_message_is_handled_by_multiple_consequenters_it_is_delivered_to_each_only_once()
        {
            var aggregateId            = Any.Guid();
            var consequenter1WasCalled = new AsyncValue <bool>();
            var consequenter2WasCalled = new AsyncValue <bool>();
            var consequenter1CallCount = 0;
            var consequenter2CallCount = 0;
            var consequenter1          = Consequenter.Create <Order.Fulfilled>(e =>
            {
                if (e.AggregateId == aggregateId)
                {
                    Interlocked.Increment(ref consequenter1CallCount);
                    consequenter1WasCalled.Set(true);
                }
            })
                                         .Named(MethodBase.GetCurrentMethod().Name + "-1")
                                         .UseServiceBusForDurability(settings, configuration: configuration);
            var consequenter2 = Consequenter.Create <Order.Fulfilled>(e =>
            {
                if (e.AggregateId == aggregateId)
                {
                    Interlocked.Increment(ref consequenter2CallCount);
                    consequenter2WasCalled.Set(true);
                }
            })
                                .Named(MethodBase.GetCurrentMethod().Name + "-2")
                                .UseServiceBusForDurability(settings, configuration: configuration);

            using (var catchup = CreateReadModelCatchup <CommandSchedulerDbContext>(consequenter1, consequenter2))
            {
                Events.Write(1, i => new Order.Fulfilled {
                    AggregateId = aggregateId
                });

                catchup.Run();

                // give the service bus messages times to be delivered
                Task.WaitAll(new Task[]
                {
                    consequenter1WasCalled,
                    consequenter2WasCalled
                }, DefaultTimeout());

                consequenter1CallCount.Should().Be(1);
                consequenter2CallCount.Should().Be(1);
            }
        }
        public async Task Catchup_can_be_used_with_intercepted_consequenters_to_queue_event_messages_on_the_service_bus()
        {
            var onCancelledCalled           = new AsyncValue <bool>();
            var onCreatedCalled             = new AsyncValue <bool>();
            var onDeliveredCalled           = new AsyncValue <bool>();
            var anonymousConsequenterCalled = new AsyncValue <bool>();

            var consequenter1 = new TestConsequenter(
                onCancelled: e => onCancelledCalled.Set(true),
                onCreated: e => onCreatedCalled.Set(true),
                onDelivered: e => onDeliveredCalled.Set(true))
                                .UseServiceBusForDurability(settings, configuration: configuration);

            var name          = MethodBase.GetCurrentMethod().Name;
            var consequenter2 = Consequenter.Create <Order.Fulfilled>(e => anonymousConsequenterCalled.Set(true))
                                .Named(name)
                                .UseServiceBusForDurability(settings, configuration: configuration);

            using (var catchup = CreateReadModelCatchup <CommandSchedulerDbContext>(
                       consequenter1,
                       consequenter2))
            {
                Events.Write(1, i => new Order.Created());
                Events.Write(1, i => new Order.Delivered());
                Events.Write(1, i => new Order.Fulfilled());
                Events.Write(1, i => new Order.Cancelled());

                catchup.Run();

                // give the service bus messages times to be delivered
                Task.WaitAll(new Task[]
                {
                    onCancelledCalled,
                    onCreatedCalled,
                    onDeliveredCalled,
                    anonymousConsequenterCalled
                }, DefaultTimeout());

                onCancelledCalled.Result.Should().Be(true);
                onCreatedCalled.Result.Should().Be(true);
                onDeliveredCalled.Result.Should().Be(true);
                anonymousConsequenterCalled.Result.Should().Be(true);
            }
        }