Пример #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();
        }
Пример #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);
            });
        }
Пример #4
0
 public DownstreamRouteFinderMiddleware(OcelotRequestDelegate next,
                                        IOcelotLoggerFactory loggerFactory,
                                        IDownstreamRouteProviderFactory downstreamRouteFinder,
                                        IMultiplexer multiplexer)
     : base(loggerFactory.CreateLogger <DownstreamRouteFinderMiddleware>())
 {
     _multiplexer = multiplexer;
     _next        = next;
     _factory     = downstreamRouteFinder;
 }
Пример #5
0
 public DownstreamRouteFinderMiddleware(OcelotRequestDelegate next,
                                        IOcelotLoggerFactory loggerFactory,
                                        IDownstreamRouteFinder downstreamRouteFinder,
                                        IOcelotConfigurationProvider configProvider,
                                        IMultiplexer multiplexer)
 {
     _configProvider        = configProvider;
     _multiplexer           = multiplexer;
     _next                  = next;
     _downstreamRouteFinder = downstreamRouteFinder;
     _logger                = loggerFactory.CreateLogger <DownstreamRouteFinderMiddleware>();
 }
Пример #6
0
 public DownstreamRouteFinderMiddleware(OcelotRequestDelegate next,
                                        IOcelotLoggerFactory loggerFactory,
                                        IDownstreamRouteFinder downstreamRouteFinder,
                                        IInternalConfigurationRepository repo,
                                        IMultiplexer multiplexer)
     : base(loggerFactory.CreateLogger <DownstreamRouteFinderMiddleware>())
 {
     _repo                  = repo;
     _multiplexer           = multiplexer;
     _next                  = next;
     _downstreamRouteFinder = downstreamRouteFinder;
 }
Пример #7
0
 /// <summary>
 /// Coordinates reading messages from a collection of <see cref="IMessageReceiveBuffer"/>
 /// and dispatching using a collection of <see cref="IMultiplexerSubscriber"/>.
 /// </summary>
 /// <param name="settings">The <see cref="SubscriptionGroupSettings"/> to use.</param>
 /// <param name="receiveBuffers">The collection of <see cref="IMessageReceiveBuffer"/> to read from.</param>
 /// <param name="multiplexer">The <see cref="IMultiplexer"/> to aggregate all messages into one stream.</param>
 /// <param name="subscribers">The collection of <see cref="IMultiplexerSubscriber"/> that will dispatch the messages</param>
 /// <param name="logger">The <see cref="ILogger"/> to be used.</param>
 public SubscriptionGroup(
     SubscriptionGroupSettings settings,
     ICollection <IMessageReceiveBuffer> receiveBuffers,
     IMultiplexer multiplexer,
     ICollection <IMultiplexerSubscriber> subscribers,
     ILogger <SubscriptionGroup> logger)
 {
     _receiveBuffers = receiveBuffers;
     _settings       = settings;
     _multiplexer    = multiplexer;
     _subscribers    = subscribers;
     _logger         = logger;
 }
Пример #8
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();

            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;
        }
Пример #9
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();
        }
Пример #10
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);
            });
        }
Пример #11
0
    private ISubscriptionGroup Create(ReceiveMiddleware receiveMiddleware, SubscriptionGroupSettings settings)
    {
        IMultiplexer multiplexer = CreateMultiplexer(settings.MultiplexerCapacity);
        ICollection <IMessageReceiveBuffer>  receiveBuffers = CreateBuffers(receiveMiddleware, settings);
        ICollection <IMultiplexerSubscriber> subscribers    = CreateSubscribers(settings);

        foreach (IMessageReceiveBuffer receiveBuffer in receiveBuffers)
        {
            multiplexer.ReadFrom(receiveBuffer.Reader);
        }

        foreach (IMultiplexerSubscriber subscriber in subscribers)
        {
            subscriber.Subscribe(multiplexer.GetMessagesAsync());
        }

        return(new SubscriptionGroup(
                   settings,
                   receiveBuffers,
                   multiplexer,
                   subscribers,
                   _loggerFactory.CreateLogger <SubscriptionGroup>()));
    }
 private static async Task ReadAllMessages(IMultiplexer multiplexer)
 {
     await foreach (var _ in multiplexer.GetMessagesAsync())
     {
     }
 }