PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Async/AsyncQueueTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 99 lines | 72 code | 13 blank | 14 comment | 1 complexity | 650964d155dae773bbc7f44ae66dbda5 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-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.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using FluentAssertions;
  19. using MongoDB.Driver.Core.Async;
  20. using Xunit;
  21. namespace MongoDB.Driver.Core.Async
  22. {
  23. public class AsyncQueueTests
  24. {
  25. [Theory]
  26. [InlineData(0)]
  27. [InlineData(1)]
  28. [InlineData(2)]
  29. public void DequeueAll_should_dequeue_all_items(int n)
  30. {
  31. var subject = new AsyncQueue<int>();
  32. for (var i = 0; i < n; i++)
  33. {
  34. subject.Enqueue(i);
  35. }
  36. var count = subject.DequeueAll().Count();
  37. count.Should().Be(n);
  38. }
  39. [Fact]
  40. public void Items_should_be_dequeued_in_the_order_they_were_enqueued()
  41. {
  42. var subject = new AsyncQueue<int>();
  43. subject.Enqueue(10);
  44. subject.Enqueue(11);
  45. var result = subject.DequeueAsync(CancellationToken.None);
  46. var result2 = subject.DequeueAsync(CancellationToken.None);
  47. result.Result.Should().Be(10);
  48. result2.Result.Should().Be(11);
  49. }
  50. [Fact]
  51. public void Items_should_be_dequeued_in_the_order_they_were_enqueued_2()
  52. {
  53. var subject = new AsyncQueue<int>();
  54. var result = subject.DequeueAsync(CancellationToken.None);
  55. var result2 = subject.DequeueAsync(CancellationToken.None);
  56. subject.Enqueue(10);
  57. subject.Enqueue(11);
  58. result.Result.Should().Be(10);
  59. result2.Result.Should().Be(11);
  60. }
  61. [Fact]
  62. public void Dequeue_should_return_an_uncompleted_task_when_no_items_exist()
  63. {
  64. var subject = new AsyncQueue<int>();
  65. var result = subject.DequeueAsync(CancellationToken.None);
  66. result.IsCompleted.Should().BeFalse();
  67. }
  68. [Fact]
  69. public void Dequeue_should_complete_when_an_item_is_added_to_the_queue()
  70. {
  71. var subject = new AsyncQueue<int>();
  72. var result = subject.DequeueAsync(CancellationToken.None);
  73. subject.Enqueue(10);
  74. result.Result.Should().Be(10);
  75. }
  76. [Fact]
  77. public void Dequeue_should_cancel_when_cancellation_is_requested()
  78. {
  79. var cancellationTokenSource = new CancellationTokenSource();
  80. var subject = new AsyncQueue<int>();
  81. var result = subject.DequeueAsync(cancellationTokenSource.Token);
  82. cancellationTokenSource.Cancel();
  83. Assert.Throws<TaskCanceledException>(() => result.GetAwaiter().GetResult());
  84. }
  85. }
  86. }