Exemplo n.º 1
0
            public void Should_unblock_blocked_wait_thread_when_release_forever()
            {
                var limiter = new Limiter(0, 10, log);

                var waitTasks = new []
                {
                    new Task(() => limiter.WaitFree(), TaskCreationOptions.LongRunning),
                    new Task(() => limiter.WaitFree(), TaskCreationOptions.LongRunning),
                    new Task(() => limiter.WaitFree(), TaskCreationOptions.LongRunning)
                };

                Start(waitTasks);
                var whenAll = Task.WhenAll(waitTasks);
                var whenAny = Task.WhenAny(waitTasks);

                whenAll.Wait(200);
                whenAll.IsCompleted.Should().BeFalse();

                limiter.ReleaseForever();

                whenAny.Wait(100);
                whenAny.IsCompleted.Should().BeTrue();
                whenAll.Wait(100);
                whenAll.IsCompleted.Should().BeTrue();

                limiter.CurrentFreeNumber.Should().Be(10);
            }
Exemplo n.º 2
0
            public void Should_block_wait_thread_while_not_relese_enough_counter()
            {
                var limiter = new Limiter(0, 10);

                var waitTasks = new []
                {
                    new Task(() => limiter.WaitFree(), TaskCreationOptions.LongRunning),
                    new Task(() => limiter.WaitFree(), TaskCreationOptions.LongRunning),
                    new Task(() => limiter.WaitFree(), TaskCreationOptions.LongRunning)
                };

                Start(waitTasks);
                var whenAll = Task.WhenAll(waitTasks);
                var whenAny = Task.WhenAny(waitTasks);

                whenAll.Wait(200);
                whenAll.IsCompleted.Should().BeFalse();

                limiter.TryRelease(out _);

                whenAny.Wait(100);
                whenAny.IsCompleted.Should().BeTrue();
                whenAll.IsCompleted.Should().BeFalse();

                limiter.TryRelease(2, out _);
                whenAll.Wait(100);
                whenAll.IsCompleted.Should().BeTrue();

                limiter.CurrentFreeNumber.Should().Be(0);
            }
Exemplo n.º 3
0
            public void Should_not_allow_to_decreases_by_waits()
            {
                var limiter = new Limiter(0, 2);

                limiter.ReleaseForever();

                limiter.WaitFree();
                limiter.WaitFree();
                limiter.WaitFree();
                limiter.CurrentFreeNumber.Should().Be(2);
            }
Exemplo n.º 4
0
 private static void WaitFor(Limiter limiter, int times)
 {
     for (var i = 0; i < times; i++)
     {
         limiter.WaitFree();
     }
 }
Exemplo n.º 5
0
            public void Should_decrease_current_value()
            {
                var limiter = new Limiter(10, log: log);

                limiter.WaitFree();

                limiter.CurrentFreeNumber.Should().Be(9);
            }
Exemplo n.º 6
0
            public void Should_block_current_thread_when_no_free_values()
            {
                var limiter = new Limiter(0, log: log);

                var task = Task.Run(() => limiter.WaitFree());

                task.Wait(300);

                task.IsCanceled.Should().BeFalse();
            }
Exemplo n.º 7
0
            public void Should_wait_and_release_by_some_number_from_different_threads()
            {
                var limiter = new Limiter(0, 10, log);

                var waitTask = new Task(
                    () =>
                {
                    for (var i = 0; i < 100; i++)
                    {
                        limiter.WaitFree();
                    }
                },
                    TaskCreationOptions.LongRunning);

                var releaseTask = new Task(
                    () =>
                {
                    var released = 100;
                    var i        = 0;
                    while (released > 0)
                    {
                        var times = i++ % 3 + 1;
                        if (limiter.TryRelease(times, out _))
                        {
                            released -= times;
                        }
                        Thread.SpinWait(times * 3);
                    }
                },
                    TaskCreationOptions.LongRunning);

                Start(waitTask, releaseTask);
                var whenAll = Task.WhenAll(waitTask, releaseTask);

                whenAll.Wait(200);

                whenAll.IsCompleted.Should().BeTrue();

                limiter.CurrentFreeNumber.Should().BeLessOrEqualTo(10);
            }