Пример #1
0
        public void constructor_should_initialize_instance_with_completed_task_when_semaphore_is_available()
        {
            var semaphore = new SemaphoreSlim(1);

            var result = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            result.Task.Status.Should().Be(TaskStatus.RanToCompletion);
            semaphore.CurrentCount.Should().Be(0);
        }
        public void constructor_should_initialize_instance_with_completed_task_when_semaphore_is_available()
        {
            var semaphore = new SemaphoreSlim(1);

            var result = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            result.Task.Status.Should().Be(TaskStatus.RanToCompletion);
            semaphore.CurrentCount.Should().Be(0);
        }
Пример #3
0
        public void Dispose_should_release_semaphore()
        {
            var semaphore = new SemaphoreSlim(1);
            var subject   = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            subject.Dispose();

            semaphore.CurrentCount.Should().Be(1);
        }
        public void Dispose_should_release_semaphore()
        {
            var semaphore = new SemaphoreSlim(1);
            var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            subject.Dispose();

            semaphore.CurrentCount.Should().Be(1);
        }
Пример #5
0
        public void Sempahore_should_not_be_released_when_cancellation_is_requested_after_semaphore_is_acquired()
        {
            var semaphore = new SemaphoreSlim(1);
            var cancellationTokenSource = new CancellationTokenSource();
            var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();

            semaphore.CurrentCount.Should().Be(0);
        }
        public void constructor_should_initialize_instance_with_incompleted_task_when_semaphore_is_not_available()
        {
            var semaphore = new SemaphoreSlim(1);
            semaphore.Wait();

            var result = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            result.Task.IsCompleted.Should().BeFalse();
            semaphore.CurrentCount.Should().Be(0);
        }
Пример #7
0
        public void constructor_should_initialize_instance_with_incompleted_task_when_semaphore_is_not_available()
        {
            var semaphore = new SemaphoreSlim(1);

            semaphore.Wait();

            var result = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            result.Task.IsCompleted.Should().BeFalse();
            semaphore.CurrentCount.Should().Be(0);
        }
        public void Dispose_should_cancel_pending_request()
        {
            var semaphore = new SemaphoreSlim(1);
            semaphore.Wait();
            var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            subject.Dispose();
            semaphore.Release();

            subject.Task.Status.Should().Be(TaskStatus.Canceled);
            semaphore.CurrentCount.Should().Be(1);
        }
Пример #9
0
        public void Dispose_should_cancel_pending_request()
        {
            var semaphore = new SemaphoreSlim(1);

            semaphore.Wait();
            var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            subject.Dispose();
            semaphore.Release();

            subject.Task.Status.Should().Be(TaskStatus.Canceled);
            semaphore.CurrentCount.Should().Be(1);
        }
Пример #10
0
        public void Task_should_be_completed_when_semaphore_becomes_available()
        {
            var semaphore = new SemaphoreSlim(1);

            semaphore.Wait();
            var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            semaphore.Release();
            SpinWait.SpinUntil(() => subject.Task.IsCompleted, 1000).Should().BeTrue();

            subject.Task.Status.Should().Be(TaskStatus.RanToCompletion);
            semaphore.CurrentCount.Should().Be(0);
        }
Пример #11
0
        public void Task_should_be_cancelled_when_cancellationToken_requests_cancellation()
        {
            var semaphore = new SemaphoreSlim(1);
            var cancellationTokenSource = new CancellationTokenSource();

            semaphore.Wait();
            var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();
            semaphore.Release();

            subject.Task.Status.Should().Be(TaskStatus.Canceled);
            semaphore.CurrentCount.Should().Be(1);
        }
        public void Task_should_be_cancelled_when_cancellationToken_requests_cancellation()
        {
            var semaphore = new SemaphoreSlim(1);
            var cancellationTokenSource = new CancellationTokenSource();

            semaphore.Wait();
            var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();
            SpinWait.SpinUntil(() => subject.Task.IsCompleted, TimeSpan.FromSeconds(5)).Should().BeTrue();
            semaphore.Release();

            subject.Task.Status.Should().Be(TaskStatus.Canceled);
            semaphore.CurrentCount.Should().Be(1);
        }
        public void Task_should_be_cancelled_when_cancellationToken_requests_cancellation()
        {
            var semaphore = new SemaphoreSlim(1);
            var cancellationTokenSource = new CancellationTokenSource();
            semaphore.Wait();
            var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();
            SpinWait.SpinUntil(() => subject.Task.IsCompleted, TimeSpan.FromSeconds(5)).Should().BeTrue();
            semaphore.Release();

            subject.Task.Status.Should().Be(TaskStatus.Canceled);
            semaphore.CurrentCount.Should().Be(1);
        }
        private async Task<IByteBuffer> ReceiveBufferAsync(int responseTo, CancellationToken cancellationToken)
        {
            using (var receiveLockRequest = new SemaphoreSlimRequest(_receiveLock, cancellationToken))
            {
                var messageTask = _dropbox.GetMessageAsync(responseTo);
                try
                {
                    await Task.WhenAny(messageTask, receiveLockRequest.Task).ConfigureAwait(false);
                    if (messageTask.IsCompleted)
                    {
                        return _dropbox.RemoveMessage(responseTo);
                    }

                    receiveLockRequest.Task.GetAwaiter().GetResult(); // propagate exceptions
                    while (true)
                    {
                        var buffer = await ReceiveBufferAsync().ConfigureAwait(false);
                        _dropbox.AddMessage(buffer);

                        if (messageTask.IsCompleted)
                        {
                            return _dropbox.RemoveMessage(responseTo);
                        }

                        cancellationToken.ThrowIfCancellationRequested();
                    }
                }
                catch
                {
                    var ignored = messageTask.ContinueWith(
                        t => { _dropbox.RemoveMessage(responseTo).Dispose(); },
                        TaskContinuationOptions.OnlyOnRanToCompletion);
                    throw;
                }
            }
        }
        public void Task_should_be_cancelled_when_cancellationToken_requests_cancellation()
        {
            var semaphore = new SemaphoreSlim(1);
            var cancellationTokenSource = new CancellationTokenSource();
            semaphore.Wait();
            var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();
            semaphore.Release();

            subject.Task.Status.Should().Be(TaskStatus.Canceled);
            semaphore.CurrentCount.Should().Be(1);
        }
        public void Sempahore_should_not_be_released_when_cancellation_is_requested_after_semaphore_is_acquired()
        {
            var semaphore = new SemaphoreSlim(1);
            var cancellationTokenSource = new CancellationTokenSource();
            var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();

            semaphore.CurrentCount.Should().Be(0);
        }
        public void Task_should_be_completed_when_semaphore_becomes_available()
        {
            var semaphore = new SemaphoreSlim(1);
            semaphore.Wait();
            var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None);

            semaphore.Release();
            SpinWait.SpinUntil(() => subject.Task.IsCompleted, 100);

            subject.Task.Status.Should().Be(TaskStatus.RanToCompletion);
            semaphore.CurrentCount.Should().Be(0);
        }