Exemple #1
0
        public void Wait()
        {
            var semaphore = new AsyncSemaphore(1, 1, true);

            semaphore.Wait();
            var acquiredLock = semaphore.Wait(1, 0);

            Assert.False(acquiredLock);
        }
Exemple #2
0
        public void ReleaseAll()
        {
            var semaphore = new AsyncSemaphore(0, 10, true);

            var acquiredWhenNoneAvailable = semaphore.Wait(10, 0);

            semaphore.ReleaseAll();
            var acquiredWhenAllAvailable = semaphore.Wait(10, 0);

            Assert.False(acquiredWhenNoneAvailable);
            Assert.True(acquiredWhenAllAvailable);
        }
Exemple #3
0
        public IExchange DeclareExchange(IAdvancedBus advancedBus, string exchangeName, string exchangeType)
        {
            IExchange exchange;

            if (exchanges.TryGetValue(exchangeName, out exchange))
            {
                return(exchange);
            }
            semaphore.Wait();
            try
            {
                if (exchanges.TryGetValue(exchangeName, out exchange))
                {
                    return(exchange);
                }
                var param = new ExchangeDeclareParam(exchangeName, exchangeType);
                exchange = advancedBus.ExchangeDeclare(param);
                exchanges[exchangeName] = exchange;
                return(exchange);
            }
            finally
            {
                semaphore.Release();
            }
        }
Exemple #4
0
        public void Wait_Random()
        {
            var semaphore = new AsyncSemaphore(1000, 1000, true);

            var random     = new Random(1234);
            var amountLeft = 1000;

            while (amountLeft > 0)
            {
                var maxToAcquire    = amountLeft / 2;
                var amountToAcquire = random.Next(1, maxToAcquire > 1 ? maxToAcquire : 1);
                Assert.True(semaphore.Wait(amountToAcquire, 0));
                amountLeft -= amountToAcquire;
            }
            semaphore.Release(1000);
            Assert.True(semaphore.Wait(1000, 0));
        }
Exemple #5
0
    public void CanBeUsedAsQueueGate()
    {
        var semaphore = new AsyncSemaphore(initialCount: 0);

        semaphore.Release();
        semaphore.Release();
        semaphore.Release();

        var cancellationToken = CancelAfter(TimeSpan.FromSeconds(1));

        semaphore.Wait(cancellationToken);
        semaphore.Wait(cancellationToken);
        semaphore.Wait(cancellationToken);

        var exception = Assert.Throws <TaskCanceledException>(() => semaphore.Wait(cancellationToken));

        Console.WriteLine(exception);
    }
Exemple #6
0
        public void Wait_Release()
        {
            var semaphore = new AsyncSemaphore(1, 1, true);

            for (var i = 0; i < 1000; ++i)
            {
                semaphore.Wait();
                semaphore.Release();
            }
        }
Exemple #7
0
        public void Wait_CancellationToken_Success()
        {
            var semaphore = new AsyncSemaphore(0, 1);

            using (var cancellationToken = new CancellationTokenSource(100))
            {
                Assert.Throws <OperationCanceledException>(()
                                                           => semaphore.Wait(cancellationToken.Token));
            }
        }
Exemple #8
0
        public void Wait_Timeout()
        {
            var startTime         = Environment.TickCount;
            var semaphore         = new AsyncSemaphore(0, 1);
            var acquiredSemaphore = semaphore.Wait(1, 250);
            var timeWaited        = Environment.TickCount - startTime;

            Assert.False(acquiredSemaphore);
            Assert.True(timeWaited >= 90);
        }
        public void TestWaitRelease()
        {
            var semaphore = new AsyncSemaphore(1);

            Assert.AreEqual(1, semaphore.Available);
            semaphore.Wait();
            Assert.AreEqual(0, semaphore.Available);
            semaphore.Release();
            Assert.AreEqual(1, semaphore.Available);
        }
Exemple #10
0
        public void Wait_CancellationToken()
        {
            var semaphore = new AsyncSemaphore(0, 1);
            var start     = Environment.TickCount;

            using (var cancellationToken = new CancellationTokenSource(100))
            {
                Assert.Throws <OperationCanceledException>(()
                                                           => semaphore.Wait(cancellationToken.Token));
                Assert.True(Environment.TickCount - start >= 90);
            }
        }
        public void WhenClientRelease_SemaphoreShouldNotMakeOneMoreClientWaitedAsync()
        {
            var sut = new AsyncSemaphore(1);

            sut.Wait();
            var task = sut.WaitAsync();

            ConcurrentAssert.EnsureThatTaskIsNeverCompleted(task);

            sut.Release();

            ConcurrentAssert.EnsureThatTaskIsCompleted(task);
        }
        public void Acquire()
        {
            var ct = new CancellationTokenSource(TimeSpan.FromMinutes(3)).Token;

            try
            {
                _semaphore.Wait(ct);
            }
            catch (TaskCanceledException)
            {
                throw new TimeoutException("Possible deadlock.");
            }
        }
Exemple #13
0
        public void CurrentCount_MaxCount()
        {
            var semaphore = new AsyncSemaphore(14, 18, true);

            Assert.Equal(18, semaphore.MaxCount);
            Assert.Equal(14, semaphore.CurrentCount);

            semaphore.Release(2);
            Assert.Equal(16, semaphore.CurrentCount);

            semaphore.Release();
            semaphore.Release();
            Assert.Equal(semaphore.MaxCount, semaphore.CurrentCount);

            semaphore.Wait(18);
            Assert.Equal(0, semaphore.CurrentCount);
        }
        public int AsyncSemaphore_Sync()
        {
            int count = 0;

            for (int i = 0; i < PER_TEST; i++)
            {
                _asyncSemaphore.Wait();
                try
                {
                    count++;
                }
                finally
                { // to make useful comparison
                    _asyncSemaphore.Release();
                }
            }
            return(count.AssertIs(PER_TEST));
        }
Exemple #15
0
        public async Task MultipleSynchronousAcquire_Fair()
        {
            var semaphore       = new AsyncSemaphore(0, 1, true);
            var completionTasks = Enumerable.Range(0, 10).Select(_ =>
            {
                return(new TaskCompletionSource <bool>());
            }).ToList();

            var acquireTasks = new List <Task>();

            for (var i = 0; i < 10; ++i)
            {
                var index       = i;
                var acquireTask = Task.Run(() =>
                {
                    semaphore.Wait();
                    completionTasks[index].SetResult(true);
                });
                await Task.Delay(100);

                while (acquireTask.Status != TaskStatus.Running)
                {
                    await Task.Delay(100);
                }
            }

            Assert.True(completionTasks.All(t => t.Task.Status != TaskStatus.RanToCompletion));

            for (var i = 0; i < 10; ++i)
            {
                for (var finished = 0; finished < i; ++finished)
                {
                    Assert.True(completionTasks[finished].Task.IsCompletedSuccessfully);
                }
                for (var running = i; running < 10; ++running)
                {
                    Assert.NotEqual(TaskStatus.RanToCompletion, completionTasks[running].Task.Status);
                }
                semaphore.Release();
                await Task.Delay(100);
            }

            Assert.True(completionTasks.All(t => t.Task.IsCompletedSuccessfully));
        }