Exemplo n.º 1
0
        public async Task WhenATasksIsAdded_AvailableWorkersIsDecremented()
        {
            var messageProcessingStrategy = new Throttled(123, _fakeMonitor);
            var tcs = new TaskCompletionSource <object>();

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

            messageProcessingStrategy.AvailableWorkers.ShouldBe(122);
            await AllowTasksToComplete(tcs);
        }
Exemplo n.º 2
0
        public async Task WhenATasksIsAdded_MaxWorkersIsUnaffected()
        {
            var messageProcessingStrategy = new Throttled(123, _fakeMonitor);
            var tcs = new TaskCompletionSource <object>();

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

            Assert.That(messageProcessingStrategy.MaxWorkers, Is.EqualTo(123));

            await AllowTasksToComplete(tcs);
        }
Exemplo n.º 3
0
        public async Task WhenATasksIsAdded_MaxWorkersIsUnaffected()
        {
            var messageProcessingStrategy = new Throttled(123, _fakeMonitor);
            var tcs = new TaskCompletionSource <object>();

            await messageProcessingStrategy.StartWorker(() => tcs.Task, CancellationToken.None);

            messageProcessingStrategy.MaxWorkers.ShouldBe(123);

            await AllowTasksToComplete(tcs);
        }
Exemplo n.º 4
0
        public async Task WhenATaskCompletes_AvailableWorkersIsIncremented()
        {
            var messageProcessingStrategy = new Throttled(3, _fakeMonitor);
            var tcs = new TaskCompletionSource <object>();

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

            Assert.That(messageProcessingStrategy.AvailableWorkers, Is.EqualTo(2));

            await AllowTasksToComplete(tcs);

            Assert.That(messageProcessingStrategy.MaxWorkers, Is.EqualTo(3));
            Assert.That(messageProcessingStrategy.AvailableWorkers, Is.EqualTo(3));
        }
Exemplo n.º 5
0
        public async Task WhenATaskCompletes_AvailableWorkersIsIncremented()
        {
            var messageProcessingStrategy = new Throttled(3, _fakeMonitor);
            var tcs = new TaskCompletionSource <object>();

            await messageProcessingStrategy.StartWorker(() => tcs.Task, CancellationToken.None);

            messageProcessingStrategy.AvailableWorkers.ShouldBe(2);

            await AllowTasksToComplete(tcs);

            messageProcessingStrategy.MaxWorkers.ShouldBe(3);
            messageProcessingStrategy.AvailableWorkers.ShouldBe(3);
        }
Exemplo n.º 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);
            }

            messageProcessingStrategy.MaxWorkers.ShouldBe(capacity);
            messageProcessingStrategy.AvailableWorkers.ShouldBe(0);
            await AllowTasksToComplete(tcs);
        }
Exemplo n.º 7
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);
                messageProcessingStrategy.AvailableWorkers.ShouldBeGreaterThanOrEqualTo(0);
            }

            await AllowTasksToComplete(tcs);
        }
Exemplo n.º 8
0
        public async Task AvailableWorkers_CanGoToZeroAndBackToFull()
        {
            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.EqualTo(0));

            await AllowTasksToComplete(tcs);

            Assert.That(messageProcessingStrategy.AvailableWorkers, Is.EqualTo(capacity));
        }
Exemplo n.º 9
0
        public async Task AvailableWorkers_CanGoToZeroAndBackToFull()
        {
            const int capacity = 10;
            var       messageProcessingStrategy = new Throttled(capacity, _fakeMonitor);
            var       tcs = new TaskCompletionSource <object>();

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

            messageProcessingStrategy.AvailableWorkers.ShouldBe(0);

            await AllowTasksToComplete(tcs);

            messageProcessingStrategy.AvailableWorkers.ShouldBe(capacity);
        }