Beispiel #1
0
        public void ShouldNotLeaveWaitersInQueueIfOperationIsCancelledAfterCallingRelease()
        {
            var @lock = new FifoSemaphore(1);

            @lock.Acquire(CancellationToken.None);

            try
            {
                var cts = new CancellationTokenSource();

                cts.Cancel();

                Assert.Throws <OperationCanceledException>(() => @lock.Acquire(cts.Token));

                Assert.Empty(@lock._waitQueue);

                cts = new CancellationTokenSource();

                @lock.ForTestingPurposesOnly().JustBeforeAddingToWaitQueue += () => cts.Cancel();

                Assert.Throws <OperationCanceledException>(() => @lock.Acquire(cts.Token));

                Assert.Equal(1, @lock._waitQueue.Count);

                Assert.True(@lock._waitQueue.First().IsCancelled);
            }
            finally
            {
                @lock.Release();
            }

            Assert.Empty(@lock._waitQueue);
        }
        public void ShouldNotLeaveWaitersInQueueIfOperationIsCancelled()
        {
            var @lock = new FifoSemaphore(1);

            @lock.Acquire(CancellationToken.None);

            var cts = new CancellationTokenSource();

            cts.Cancel();

            Assert.Throws <OperationCanceledException>(() => @lock.Acquire(cts.Token));

            Assert.Empty(@lock._waitQueue);

            cts = new CancellationTokenSource();

            @lock.ForTestingPurposesOnly().JustBeforeAddingToWaitQueue += () => cts.Cancel();

            Assert.Throws <OperationCanceledException>(() => @lock.Acquire(cts.Token));

            Assert.Empty(@lock._waitQueue);
        }