You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

142 lines
3.8 KiB

  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using MQTTnet.Internal;
  6. namespace MQTTnet.Tests
  7. {
  8. [TestClass]
  9. public class AsyncQueue_Tests
  10. {
  11. [TestMethod]
  12. public async Task Preserve_Order()
  13. {
  14. var queue = new AsyncQueue<string>();
  15. queue.Enqueue("1");
  16. queue.Enqueue("2");
  17. queue.Enqueue("3");
  18. Assert.AreEqual("1", (await queue.TryDequeueAsync(CancellationToken.None)).Item);
  19. Assert.AreEqual("2", (await queue.TryDequeueAsync(CancellationToken.None)).Item);
  20. Assert.AreEqual("3", (await queue.TryDequeueAsync(CancellationToken.None)).Item);
  21. }
  22. [TestMethod]
  23. public void Count()
  24. {
  25. var queue = new AsyncQueue<string>();
  26. queue.Enqueue("1");
  27. Assert.AreEqual(1, queue.Count);
  28. queue.Enqueue("2");
  29. Assert.AreEqual(2, queue.Count);
  30. queue.Enqueue("3");
  31. Assert.AreEqual(3, queue.Count);
  32. }
  33. [TestMethod]
  34. public async Task Cancellation()
  35. {
  36. var queue = new AsyncQueue<int>();
  37. bool success;
  38. using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3)))
  39. {
  40. success = (await queue.TryDequeueAsync(cancellationTokenSource.Token)).IsSuccess;
  41. }
  42. Assert.AreEqual(false, success);
  43. }
  44. [TestMethod]
  45. public async Task Process_Async()
  46. {
  47. var queue = new AsyncQueue<int>();
  48. var sum = 0;
  49. var worker = Task.Run(async () =>
  50. {
  51. while (sum < 6)
  52. {
  53. sum += (await queue.TryDequeueAsync(CancellationToken.None)).Item;
  54. }
  55. });
  56. queue.Enqueue(1);
  57. await Task.Delay(500);
  58. queue.Enqueue(2);
  59. await Task.Delay(500);
  60. queue.Enqueue(3);
  61. await Task.Delay(500);
  62. Assert.AreEqual(6, sum);
  63. Assert.AreEqual(TaskStatus.RanToCompletion, worker.Status);
  64. }
  65. [TestMethod]
  66. public async Task Process_Async_With_Initial_Delay()
  67. {
  68. var queue = new AsyncQueue<int>();
  69. var sum = 0;
  70. var worker = Task.Run(async () =>
  71. {
  72. while (sum < 6)
  73. {
  74. sum += (await queue.TryDequeueAsync(CancellationToken.None)).Item;
  75. }
  76. });
  77. // This line is the diff to test _Process_Async_
  78. await Task.Delay(500);
  79. queue.Enqueue(1);
  80. await Task.Delay(500);
  81. queue.Enqueue(2);
  82. await Task.Delay(500);
  83. queue.Enqueue(3);
  84. await Task.Delay(500);
  85. Assert.AreEqual(6, sum);
  86. Assert.AreEqual(TaskStatus.RanToCompletion, worker.Status);
  87. }
  88. [TestMethod]
  89. public void Dequeue_Sync()
  90. {
  91. var queue = new AsyncQueue<string>();
  92. queue.Enqueue("1");
  93. queue.Enqueue("2");
  94. queue.Enqueue("3");
  95. Assert.AreEqual("1", queue.TryDequeue().Item);
  96. Assert.AreEqual("2", queue.TryDequeue().Item);
  97. Assert.AreEqual("3", queue.TryDequeue().Item);
  98. }
  99. [TestMethod]
  100. public void Clear()
  101. {
  102. var queue = new AsyncQueue<string>();
  103. queue.Enqueue("1");
  104. queue.Enqueue("2");
  105. queue.Enqueue("3");
  106. queue.Clear();
  107. Assert.AreEqual(0, queue.Count);
  108. queue.Enqueue("4");
  109. Assert.AreEqual(1, queue.Count);
  110. Assert.AreEqual("4", queue.TryDequeue().Item);
  111. }
  112. }
  113. }