Beispiel #1
0
        public async Task QueueCanBeAssignedToMultiplePumps()
        {
            var sqsQueue = TestQueue();
            var buffer   = CreateMessageReceiveBuffer(sqsQueue);

            IMessageDispatcher     dispatcher = new FakeDispatcher();
            IMultiplexerSubscriber consumer1  = CreateSubscriber(dispatcher);
            IMultiplexerSubscriber consumer2  = CreateSubscriber(dispatcher);

            IMultiplexer multiplexer = CreateMultiplexer();

            multiplexer.ReadFrom(buffer.Reader);
            consumer1.Subscribe(multiplexer.GetMessagesAsync());
            consumer2.Subscribe(multiplexer.GetMessagesAsync());

            using var cts = new CancellationTokenSource(TimeoutPeriod);

            // consumers
            var multiplexerCompletion = multiplexer.RunAsync(cts.Token);
            var consumer1Completion   = consumer1.RunAsync(cts.Token);
            var consumer2Completion   = consumer2.RunAsync(cts.Token);
            var buffer1Completion     = buffer.RunAsync(cts.Token);

            var results = await Task.WhenAll(
                multiplexerCompletion.HandleCancellation(),
                buffer1Completion.HandleCancellation(),
                consumer1Completion.HandleCancellation(),
                consumer2Completion.HandleCancellation());

            results.Any().ShouldBeTrue();
        }
Beispiel #2
0
        public async Task QueueCanBeAssignedToOnePump()
        {
            var sqsQueue = TestQueue();
            var buffer   = CreateMessageReceiveBuffer(sqsQueue);
            IMessageDispatcher     dispatcher  = new FakeDispatcher();
            IMultiplexerSubscriber consumer1   = CreateSubscriber(dispatcher);
            IMultiplexer           multiplexer = CreateMultiplexer();

            multiplexer.ReadFrom(buffer.Reader);
            consumer1.Subscribe(multiplexer.GetMessagesAsync());

            var cts = new CancellationTokenSource();

            var multiplexerCompletion = multiplexer.RunAsync(cts.Token);
            var consumer1Completion   = consumer1.RunAsync(cts.Token);
            var buffer1Completion     = buffer.RunAsync(cts.Token);

            cts.Cancel();

            await multiplexerCompletion.HandleCancellation();

            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => consumer1Completion);

            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => buffer1Completion);
        }
        public async Task Subscriber_Not_Started_No_Buffer_Filled_Then_No_More_Messages_Requested()
        {
            // Arrange
            int messagesFromQueue   = 0;
            int messagesDispatched  = 0;
            int receivebufferSize   = 2;
            int multiplexerCapacity = 2;

            // plus one "in flight" between buffer and multiplexer
            int expectedReceiveFromQueueCount = receivebufferSize + multiplexerCapacity + 1;

            var sqsQueue = TestQueue(() => Interlocked.Increment(ref messagesFromQueue));
            IMessageReceiveBuffer buffer     = CreateMessageReceiveBuffer(sqsQueue, receivebufferSize);
            IMessageDispatcher    dispatcher =
                new FakeDispatcher(() => Interlocked.Increment(ref messagesDispatched));
            IMultiplexerSubscriber consumer1   = CreateSubscriber(dispatcher);
            IMultiplexer           multiplexer = CreateMultiplexer(multiplexerCapacity);

            multiplexer.ReadFrom(buffer.Reader);
            consumer1.Subscribe(multiplexer.GetMessagesAsync());

            // need to start the multiplexer before calling Messages

            using var cts = new CancellationTokenSource();

            // Act and Assert
            var multiplexerCompletion = multiplexer.RunAsync(cts.Token);
            var bufferCompletion      = buffer.RunAsync(cts.Token);

            cts.CancelAfter(TimeSpan.FromSeconds(3));

            await multiplexerCompletion.HandleCancellation();

            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => bufferCompletion);

            await Patiently.AssertThatAsync(OutputHelper,
                                            () =>
            {
                messagesFromQueue.ShouldBe(expectedReceiveFromQueueCount);
                messagesDispatched.ShouldBe(0);

                return(true);
            });

            // Starting the consumer after the token is cancelled will not dispatch messages
            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => consumer1.RunAsync(cts.Token));

            await Patiently.AssertThatAsync(OutputHelper,
                                            () =>
            {
                messagesFromQueue.ShouldBe(expectedReceiveFromQueueCount);
                messagesDispatched.ShouldBe(0);

                return(true);
            });
        }
        public async Task MultipleQueuesCanBeAssignedToMultiplePumps()
        {
            var sqsQueue1 = TestQueue();
            var sqsQueue2 = TestQueue();
            var buffer1   = CreateMessageReceiveBuffer(sqsQueue1);
            var buffer2   = CreateMessageReceiveBuffer(sqsQueue2);

            // using 2 dispatchers for logging, they should be the same/stateless
            IMessageDispatcher     dispatcher1 = new FakeDispatcher();
            IMessageDispatcher     dispatcher2 = new FakeDispatcher();
            IMultiplexerSubscriber consumer1   = CreateSubscriber(dispatcher1);
            IMultiplexerSubscriber consumer2   = CreateSubscriber(dispatcher2);

            IMultiplexer multiplexer = CreateMultiplexer();

            multiplexer.ReadFrom(buffer1.Reader);
            multiplexer.ReadFrom(buffer2.Reader);

            consumer1.Subscribe(multiplexer.GetMessagesAsync());
            consumer2.Subscribe(multiplexer.GetMessagesAsync());

            var cts = new CancellationTokenSource();

            cts.CancelAfter(TimeoutPeriod);

            var multiplexerCompletion = multiplexer.RunAsync(cts.Token);

            // consumers
            var consumer1Completion = consumer1.RunAsync(cts.Token);
            var consumer2Completion = consumer2.RunAsync(cts.Token);

            var buffer1Completion = buffer1.RunAsync(cts.Token);
            var buffer2Completion = buffer2.RunAsync(cts.Token);

            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => buffer1Completion);

            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => buffer2Completion);

            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => consumer1Completion);

            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => consumer2Completion);

            await multiplexerCompletion;
        }
Beispiel #5
0
        public async Task MultipleQueuesCanBeAssignedToMultiplePumps()
        {
            var sqsQueue1 = TestQueue();
            var sqsQueue2 = TestQueue();
            var buffer1   = CreateMessageReceiveBuffer(sqsQueue1);
            var buffer2   = CreateMessageReceiveBuffer(sqsQueue2);

            // using 2 dispatchers for logging, they should be the same/stateless
            IMessageDispatcher     dispatcher1 = new FakeDispatcher();
            IMessageDispatcher     dispatcher2 = new FakeDispatcher();
            IMultiplexerSubscriber consumer1   = CreateSubscriber(dispatcher1);
            IMultiplexerSubscriber consumer2   = CreateSubscriber(dispatcher2);

            IMultiplexer multiplexer = CreateMultiplexer();

            multiplexer.ReadFrom(buffer1.Reader);
            multiplexer.ReadFrom(buffer2.Reader);

            consumer1.Subscribe(multiplexer.GetMessagesAsync());
            consumer2.Subscribe(multiplexer.GetMessagesAsync());

            var cts = new CancellationTokenSource();

            var multiplexerCompletion = multiplexer.RunAsync(cts.Token);

            // consumers
            var consumer1Completion = consumer1.RunAsync(cts.Token);
            var consumer2Completion = consumer2.RunAsync(cts.Token);

            var buffer1Completion = buffer1.RunAsync(cts.Token);
            var buffer2Completion = buffer2.RunAsync(cts.Token);

            cts.Cancel();

            var results = await Task.WhenAll(
                multiplexerCompletion.HandleCancellation(),
                buffer1Completion.HandleCancellation(),
                buffer2Completion.HandleCancellation(),
                consumer1Completion.HandleCancellation(),
                consumer2Completion.HandleCancellation());

            results.Any().ShouldBeTrue();
        }
Beispiel #6
0
        public async Task WhenSubscriberNotStarted_BufferShouldFillUp_AndStopDownloading(int receivePrefetch, int receiveBufferSize, int multiplexerCapacity, int expectedDownloadCount)
        {
            var sqsQueue = TestQueue();
            IMessageReceiveBuffer  buffer      = CreateMessageReceiveBuffer(sqsQueue, receivePrefetch, receiveBufferSize);
            FakeDispatcher         dispatcher  = new FakeDispatcher();
            IMultiplexerSubscriber consumer1   = CreateSubscriber(dispatcher);
            IMultiplexer           multiplexer = CreateMultiplexer(multiplexerCapacity);

            multiplexer.ReadFrom(buffer.Reader);
            consumer1.Subscribe(multiplexer.GetMessagesAsync());

            OutputHelper.WriteLine("Multiplexer" + JsonConvert.SerializeObject(multiplexer.Interrogate()));
            OutputHelper.WriteLine("MessageReceiveBuffer" + JsonConvert.SerializeObject(buffer.Interrogate()));

            using var cts = new CancellationTokenSource();

            // Act and Assert
            var multiplexerCompletion = multiplexer.RunAsync(cts.Token);
            var bufferCompletion      = buffer.RunAsync(cts.Token);

            cts.CancelAfter(3.Seconds());

            await multiplexerCompletion.HandleCancellation();

            await bufferCompletion.HandleCancellation();

            sqsQueue.ReceiveMessageRequests.Sum(x => x.NumMessagesReceived).ShouldBe(expectedDownloadCount);
            dispatcher.DispatchedMessages.Count.ShouldBe(0);

            // Starting the consumer after the token is cancelled will not dispatch messages
            await Assert.ThrowsAnyAsync <OperationCanceledException>(() => consumer1.RunAsync(cts.Token));

            await Patiently.AssertThatAsync(OutputHelper,
                                            () =>
            {
                sqsQueue.ReceiveMessageRequests.Sum(x => x.NumMessagesReceived).ShouldBe(expectedDownloadCount);
                dispatcher.DispatchedMessages.Count.ShouldBe(0);
            });
        }