コード例 #1
0
 public void SetUp()
 {
     _fakeMonitor = Substitute.For<IMessageMonitor>();
     _fakeAmazonBatchSize = 10;
     _concurrencyLevel = 20;
     _messageProcessingStrategy = new Throttled(_concurrencyLevel, _fakeAmazonBatchSize, _fakeMonitor);
     _actionsProcessed = 0;
 }
コード例 #2
0
        public void SimulatedListenLoop_WhenThrottlingOccurs_CallsMessageMonitor()
        {
            var actions = BuildFakeIncomingMessages(50);
            _messageProcessingStrategy = new Throttled(20, _fakeAmazonBatchSize, _fakeMonitor);

            ListenLoopExecuted(actions);

            _fakeMonitor.Received().IncrementThrottlingStatistic();
        }
コード例 #3
0
        public async Task WhenATasksIsAdded_AvailableWorkersIsDecremented()
        {
            var messageProcessingStrategy = new Throttled(123, _fakeMonitor);
            var tcs = new TaskCompletionSource<object>();

            messageProcessingStrategy.StartWorker(() => tcs.Task);

            Assert.That(messageProcessingStrategy.AvailableWorkers, Is.EqualTo(122));
            await AllowTasksToComplete(tcs);
        }
コード例 #4
0
        public void ChangeMaxAllowedMessagesInFlightAtRuntime_TheChangeIsApplied()
        {
            var MaxAllowedMessagesInFlight = Substitute.For<Func<int>>();
            MaxAllowedMessagesInFlight().Returns(100);
            _messageProcessingStrategy = new Throttled(MaxAllowedMessagesInFlight, 10, _fakeMonitor);

            MaxAllowedMessagesInFlight().Returns(90);

            Assert.That(_messageProcessingStrategy.BlockingThreshold, Is.EqualTo(90 - 10));

        }
コード例 #5
0
        public async Task SimulatedListenLoop_WhenThrottlingOccurs_CallsMessageMonitor(int messageCount, int capacity)
        {
            Assert.That(messageCount, Is.GreaterThan(capacity), "To cause throttling, message count must be over capacity");

            var fakeMonitor = Substitute.For<IMessageMonitor>();
            var messageProcessingStrategy = new Throttled(capacity, fakeMonitor);
            var counter = new ThreadSafeCounter();

            var actions = BuildFakeIncomingMessages(messageCount, counter);

            await ListenLoopExecuted(actions, messageProcessingStrategy);

            fakeMonitor.Received().IncrementThrottlingStatistic();
            fakeMonitor.Received().HandleThrottlingTime(Arg.Any<long>());
        }
コード例 #6
0
        public async Task AvailableWorkers_CanReachZero()
        {
            const int capacity = 10;
            var messageProcessingStrategy = new Throttled(capacity, _fakeMonitor);
            var tcs = new TaskCompletionSource<object>();

            for (int i = 0; i < capacity; i++)
            {
                messageProcessingStrategy.StartWorker(() => tcs.Task);
            }

            Assert.That(messageProcessingStrategy.MaxWorkers, Is.EqualTo(capacity));
            Assert.That(messageProcessingStrategy.AvailableWorkers, Is.EqualTo(0));
            await AllowTasksToComplete(tcs);
        }
コード例 #7
0
        public async Task SimulatedListenLoop_ProcessedAllMessages(int numberOfMessagesToProcess)
        {
            var fakeMonitor = Substitute.For<IMessageMonitor>();
            var messageProcessingStrategy = new Throttled(ConcurrencyLevel, fakeMonitor);
            var counter = new ThreadSafeCounter();

            var watch = new Stopwatch();
            watch.Start();

            var actions = BuildFakeIncomingMessages(numberOfMessagesToProcess, counter);
            await ListenLoopExecuted(actions, messageProcessingStrategy);

            watch.Stop();

            await Task.Yield();
            await Task.Delay(2000);
            await Task.Yield();

            Assert.That(counter.Count, Is.EqualTo(numberOfMessagesToProcess));
        }
コード例 #8
0
        public void Ctor_WhenMaxAllowedMessagesInFlightIsNearToBatchSize_BlockingThresholdIsNeverNegative(int maxAllowedMessagesInFlight, int maxBatchSize, int expectedBlockingThreshold)
        {
            _messageProcessingStrategy = new Throttled(maxAllowedMessagesInFlight, maxBatchSize, _fakeMonitor);

            Assert.That(_messageProcessingStrategy.BlockingThreshold, Is.EqualTo(expectedBlockingThreshold));
        }
コード例 #9
0
        public async Task SimulatedListenLoop_WhenThrottlingDoesNotOccur_DoNotCallMessageMonitor(int messageCount, int capacity)
        {
            Assert.That(messageCount, Is.LessThanOrEqualTo(capacity), "To avoid throttling, message count must be not be over capacity");

            var fakeMonitor = Substitute.For<IMessageMonitor>();
            var messageProcessingStrategy = new Throttled(capacity, fakeMonitor);
            var counter = new ThreadSafeCounter();

            var actions = BuildFakeIncomingMessages(messageCount, counter);

            await ListenLoopExecuted(actions, messageProcessingStrategy);

            fakeMonitor.DidNotReceive().IncrementThrottlingStatistic();
        }
コード例 #10
0
        public void AvailableWorkers_StartsAtCapacity()
        {
            var messageProcessingStrategy = new Throttled(123, _fakeMonitor);

            Assert.That(messageProcessingStrategy.AvailableWorkers, Is.EqualTo(123));
        }
コード例 #11
0
        public async Task AvailableWorkers_IsNeverNegative()
        {
            const int capacity = 10;
            var messageProcessingStrategy = new Throttled(capacity, _fakeMonitor);
            var tcs = new TaskCompletionSource<object>();


            for (int i = 0; i < capacity; i++)
            {
                messageProcessingStrategy.StartWorker(() => tcs.Task);
                Assert.That(messageProcessingStrategy.AvailableWorkers, Is.GreaterThanOrEqualTo(0));
            }

            await AllowTasksToComplete(tcs);
        }