PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Misc/SemaphoreSlimRequestTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 125 lines | 86 code | 24 blank | 15 comment | 0 complexity | e1688d9ea1ab16d43747f693621244be MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2015-present 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. using System;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using FluentAssertions;
  19. using Xunit;
  20. namespace MongoDB.Driver.Core.Misc
  21. {
  22. public class SemaphoreSlimRequestTests
  23. {
  24. // public methods
  25. [Fact]
  26. public void constructor_should_initialize_instance_with_completed_task_when_semaphore_is_available()
  27. {
  28. var semaphore = new SemaphoreSlim(1);
  29. var result = new SemaphoreSlimRequest(semaphore, CancellationToken.None);
  30. result.Task.Status.Should().Be(TaskStatus.RanToCompletion);
  31. semaphore.CurrentCount.Should().Be(0);
  32. }
  33. [Fact]
  34. public void constructor_should_initialize_instance_with_incompleted_task_when_semaphore_is_not_available()
  35. {
  36. var semaphore = new SemaphoreSlim(1);
  37. semaphore.Wait();
  38. var result = new SemaphoreSlimRequest(semaphore, CancellationToken.None);
  39. result.Task.IsCompleted.Should().BeFalse();
  40. semaphore.CurrentCount.Should().Be(0);
  41. }
  42. [Fact]
  43. public void constructor_should_throw_when_semaphore_is_null()
  44. {
  45. Action action = () => new SemaphoreSlimRequest(null, CancellationToken.None);
  46. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("semaphore");
  47. }
  48. [Fact]
  49. public void Dispose_should_cancel_pending_request()
  50. {
  51. var semaphore = new SemaphoreSlim(1);
  52. semaphore.Wait();
  53. var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None);
  54. subject.Dispose();
  55. semaphore.Release();
  56. subject.Task.Status.Should().Be(TaskStatus.Canceled);
  57. semaphore.CurrentCount.Should().Be(1);
  58. }
  59. [Fact]
  60. public void Dispose_should_release_semaphore()
  61. {
  62. var semaphore = new SemaphoreSlim(1);
  63. var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None);
  64. subject.Dispose();
  65. semaphore.CurrentCount.Should().Be(1);
  66. }
  67. [Fact]
  68. public void Sempahore_should_not_be_released_when_cancellation_is_requested_after_semaphore_is_acquired()
  69. {
  70. var semaphore = new SemaphoreSlim(1);
  71. var cancellationTokenSource = new CancellationTokenSource();
  72. var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token);
  73. cancellationTokenSource.Cancel();
  74. semaphore.CurrentCount.Should().Be(0);
  75. }
  76. [Fact]
  77. public void Task_should_be_cancelled_when_cancellationToken_requests_cancellation()
  78. {
  79. var semaphore = new SemaphoreSlim(1);
  80. var cancellationTokenSource = new CancellationTokenSource();
  81. semaphore.Wait();
  82. var subject = new SemaphoreSlimRequest(semaphore, cancellationTokenSource.Token);
  83. cancellationTokenSource.Cancel();
  84. SpinWait.SpinUntil(() => subject.Task.IsCompleted, TimeSpan.FromSeconds(5)).Should().BeTrue();
  85. semaphore.Release();
  86. subject.Task.Status.Should().Be(TaskStatus.Canceled);
  87. semaphore.CurrentCount.Should().Be(1);
  88. }
  89. [Fact]
  90. public void Task_should_be_completed_when_semaphore_becomes_available()
  91. {
  92. var semaphore = new SemaphoreSlim(1);
  93. semaphore.Wait();
  94. var subject = new SemaphoreSlimRequest(semaphore, CancellationToken.None);
  95. semaphore.Release();
  96. SpinWait.SpinUntil(() => subject.Task.IsCompleted, TimeSpan.FromSeconds(5)).Should().BeTrue();
  97. subject.Task.Status.Should().Be(TaskStatus.RanToCompletion);
  98. semaphore.CurrentCount.Should().Be(0);
  99. }
  100. }
  101. }