|
| 1 | +/* Copyright 2015 MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Threading; |
| 18 | +using System.Threading.Tasks; |
| 19 | +using FluentAssertions; |
| 20 | +using NUnit.Framework; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Core.Misc |
| 23 | +{ |
| 24 | + [TestFixture] |
| 25 | + public class SemaphoreSlimRequestTests |
| 26 | + { |
| 27 | + // public methods |
| 28 | + [Test] |
| 29 | + public void constructor_should_initialize_instance_with_completed_task_when_semaphore_is_available() |
| 30 | + { |
| 31 | + var semaphore = new SemaphoreSlim(1); |
| 32 | + |
| 33 | + var result = new SemaphoreSlimRequest(semaphore, CancellationToken.None); |
| 34 | + |
| 35 | + result.Task.Status.Should().Be(TaskStatus.RanToCompletion); |
| 36 | + semaphore.CurrentCount.Should().Be(0); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void constructor_should_initialize_instance_with_incompleted_task_when_semaphore_is_not_available() |
| 41 | + { |
| 42 | + var semaphore = new SemaphoreSlim(1); |
| 43 | + semaphore.Wait(); |
| 44 | + |
| 45 | + var result = new SemaphoreSlimRequest(semaphore, CancellationToken.None); |
| 46 | + |
| 47 | + result.Task.IsCompleted.Should().BeFalse(); |
| 48 | + semaphore.CurrentCount.Should().Be(0); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public void constructor_should_throw_when_semaphore_is_null() |
| 53 | + { |
| 54 | + Action action = () => new SemaphoreSlimRequest(null, CancellationToken.None); |
| 55 | + |
| 56 | + action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("semaphore"); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public void Dispose_should_cancel_pending_request() |
| 61 | + { |
| 62 | + var semaphore = new SemaphoreSlim(1); |
| 63 | + semaphore.Wait(); |
| 64 | + var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None); |
| 65 | + |
| 66 | + subject.Dispose(); |
| 67 | + semaphore.Release(); |
| 68 | + |
| 69 | + subject.Task.Status.Should().Be(TaskStatus.Canceled); |
| 70 | + semaphore.CurrentCount.Should().Be(1); |
| 71 | + } |
| 72 | + |
| 73 | + [Test] |
| 74 | + public void Dispose_should_release_semaphore() |
| 75 | + { |
| 76 | + var semaphore = new SemaphoreSlim(1); |
| 77 | + var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None); |
| 78 | + |
| 79 | + subject.Dispose(); |
| 80 | + |
| 81 | + semaphore.CurrentCount.Should().Be(1); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public void Sempahore_should_not_be_released_when_cancellation_is_requested_after_semaphore_is_acquired() |
| 86 | + { |
| 87 | + var semaphore = new SemaphoreSlim(1); |
| 88 | + var cancellationTokenSource = new CancellationTokenSource(); |
| 89 | + var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token); |
| 90 | + |
| 91 | + cancellationTokenSource.Cancel(); |
| 92 | + |
| 93 | + semaphore.CurrentCount.Should().Be(0); |
| 94 | + } |
| 95 | + |
| 96 | + [Test] |
| 97 | + public void Task_should_be_cancelled_when_cancellationToken_requests_cancellation() |
| 98 | + { |
| 99 | + var semaphore = new SemaphoreSlim(1); |
| 100 | + var cancellationTokenSource = new CancellationTokenSource(); |
| 101 | + semaphore.Wait(); |
| 102 | + var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token); |
| 103 | + |
| 104 | + cancellationTokenSource.Cancel(); |
| 105 | + semaphore.Release(); |
| 106 | + |
| 107 | + subject.Task.Status.Should().Be(TaskStatus.Canceled); |
| 108 | + semaphore.CurrentCount.Should().Be(1); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void Task_should_be_completed_when_semaphore_becomes_available() |
| 113 | + { |
| 114 | + var semaphore = new SemaphoreSlim(1); |
| 115 | + semaphore.Wait(); |
| 116 | + var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None); |
| 117 | + |
| 118 | + semaphore.Release(); |
| 119 | + SpinWait.SpinUntil(() => subject.Task.IsCompleted, 100); |
| 120 | + |
| 121 | + subject.Task.Status.Should().Be(TaskStatus.RanToCompletion); |
| 122 | + semaphore.CurrentCount.Should().Be(0); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments