public async Task manage_until_cancelled()
        {
            var cts                   = new CancellationTokenSource();
            var mockQueue             = new Mock <IResolutionQueue>();
            var manager               = new ResolutionManager(NullStandardLogger.Instance, null, null, mockQueue.Object, null);
            var awaitingEnqueueSignal = new ManualResetEventSlim(false);
            var mockEnqueueSignal     = new ManualResetEventSlim(false);

            mockQueue.Setup(x => x.TryDequeue(out It.Ref <ResolutionStreamEvent> .IsAny)).Returns(false);
            mockQueue.Setup(x => x.AwaitEnqueueSignalAsync()).Callback(() => awaitingEnqueueSignal.Set()).Returns(mockEnqueueSignal.WaitHandle.AsTask());

            var manageTask = manager.ManageAsync(cts.Token);

            var timeoutToken1 = new CancellationTokenSource(10000).Token;
            await Task.WhenAny(new[] { awaitingEnqueueSignal.WaitHandle.AsTask(), timeoutToken1.WaitHandle.AsTask() });

            if (timeoutToken1.IsCancellationRequested)
            {
                throw new TimeoutException();
            }

            cts.Cancel();

            var timeoutToken2 = new CancellationTokenSource(10000).Token;
            await Task.WhenAny(new[] { manageTask, timeoutToken2.WaitHandle.AsTask() });

            if (timeoutToken2.IsCancellationRequested)
            {
                throw new TimeoutException();
            }
        }
        public async Task wait_for_enqueue_when_managing_and_no_events_in_queue()
        {
            var cts                   = new CancellationTokenSource(10000);
            var mockResolver          = new Mock <IBusinessEventResolver>();
            var mockQueue             = new Mock <IResolutionQueue>();
            var mockSortingManager    = new Mock <ISortingManager>();
            var manager               = new ResolutionManager(NullStandardLogger.Instance, mockResolver.Object, null, mockQueue.Object, mockSortingManager.Object);
            var streamId              = "s";
            var position              = 1;
            var eventType             = "x";
            var data                  = new byte[] { };
            var streamEvent           = new StreamEvent(streamId, position, null, eventType, data);
            var mockBusinessEvent     = new Mock <IBusinessEvent>();
            var awaitingEnqueueSignal = new ManualResetEventSlim(false);
            var mockEnqueueSignal     = new ManualResetEventSlim(false);

            mockResolver.Setup(x => x.Resolve(eventType, data)).Returns(mockBusinessEvent.Object);
            mockQueue.Setup(x => x.TryDequeue(out It.Ref <ResolutionStreamEvent> .IsAny)).Returns(false);
            mockQueue.Setup(x => x.AwaitEnqueueSignalAsync()).Callback(() => awaitingEnqueueSignal.Set()).Returns(mockEnqueueSignal.WaitHandle.AsTask());

            var manageTask = manager.ManageAsync(cts.Token);

            await Task.WhenAny(new[] { awaitingEnqueueSignal.WaitHandle.AsTask(), cts.Token.WaitHandle.AsTask() });

            if (cts.IsCancellationRequested)
            {
                throw new TimeoutException();
            }

            cts.Cancel();
        }
        public async Task rethrow_exception_when_managing()
        {
            var cts       = new CancellationTokenSource(10000);
            var ex        = new TestException();
            var mockQueue = new Mock <IResolutionQueue>();
            var manager   = new ResolutionManager(NullStandardLogger.Instance, null, null, mockQueue.Object, null);

            mockQueue.Setup(x => x.TryDequeue(out It.Ref <ResolutionStreamEvent> .IsAny)).Throws(ex);

            await Assert.ThrowsAsync <TestException>(() => manager.ManageAsync(cts.Token));
        }
        public async Task resolve_stream_event_with_link_and_send_to_sorting_manager()
        {
            var cts                 = new CancellationTokenSource(10000);
            var mockResolver        = new Mock <IBusinessEventResolver>();
            var mockQueue           = new Mock <IResolutionQueue>();
            var mockSortingManager  = new Mock <ISortingManager>();
            var manager             = new ResolutionManager(NullStandardLogger.Instance, mockResolver.Object, null, mockQueue.Object, mockSortingManager.Object);
            var regionId            = "r";
            var streamId            = "str";
            var position            = 1;
            var subStreamId         = "sub";
            var subPosition         = 20;
            var eventType           = "x";
            var data                = new byte[] { };
            var streamEvent         = new ResolutionStreamEvent(regionId, new StreamEvent(subStreamId, subPosition, new StreamEventLink(streamId, position), eventType, data));
            var streamEventDequeued = false;
            var mockBusinessEvent   = new Mock <IBusinessEvent>();

            mockResolver.Setup(x => x.CanResolve(eventType)).Returns(true);
            mockResolver.Setup(x => x.Resolve(eventType, data)).Returns(mockBusinessEvent.Object);
            mockQueue.Setup(x => x.TryDequeue(out streamEvent)).Returns(!streamEventDequeued).Callback(() => streamEventDequeued = true);
            mockSortingManager
            .Setup(x => x.ReceiveSubscriberEventAsync(It.IsAny <SubscriberEvent>(), It.IsAny <CancellationToken>()))
            .Callback(() => cts.Cancel())
            .Returns(Task.CompletedTask);

            await manager.ManageAsync(cts.Token);

            mockSortingManager.Verify(x => x.ReceiveSubscriberEventAsync(
                                          It.Is <SubscriberEvent>(e =>
                                                                  e.RegionId == regionId &&
                                                                  e.StreamId == streamId && e.Position == position &&
                                                                  e.SubscriptionStreamId == subStreamId && e.SubscriptionPosition == subPosition &&
                                                                  e.EventType == eventType &&
                                                                  e.ResolvedEvent == mockBusinessEvent.Object
                                                                  ),
                                          cts.Token
                                          ));
        }