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.

BlockingQueue_Tests.cs 3.7 KiB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MQTTnet.Internal;
  3. using System;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace MQTTnet.Tests
  7. {
  8. [TestClass]
  9. public class BlockingQueue_Tests
  10. {
  11. [TestMethod]
  12. public void Preserve_Order()
  13. {
  14. var queue = new BlockingQueue<string>();
  15. queue.Enqueue("a");
  16. queue.Enqueue("b");
  17. queue.Enqueue("c");
  18. Assert.AreEqual(3, queue.Count);
  19. Assert.AreEqual("a", queue.Dequeue());
  20. Assert.AreEqual("b", queue.Dequeue());
  21. Assert.AreEqual("c", queue.Dequeue());
  22. }
  23. [TestMethod]
  24. public void Remove_First_Items()
  25. {
  26. var queue = new BlockingQueue<string>();
  27. queue.Enqueue("a");
  28. queue.Enqueue("b");
  29. queue.Enqueue("c");
  30. Assert.AreEqual("a", queue.RemoveFirst());
  31. Assert.AreEqual("b", queue.RemoveFirst());
  32. Assert.AreEqual(1, queue.Count);
  33. Assert.AreEqual("c", queue.Dequeue());
  34. }
  35. [TestMethod]
  36. public void Clear_Items()
  37. {
  38. var queue = new BlockingQueue<string>();
  39. queue.Enqueue("a");
  40. queue.Enqueue("b");
  41. queue.Enqueue("c");
  42. Assert.AreEqual(3, queue.Count);
  43. queue.Clear();
  44. Assert.AreEqual(0, queue.Count);
  45. }
  46. [TestMethod]
  47. public async Task Wait_For_Item()
  48. {
  49. var queue = new BlockingQueue<string>();
  50. string item = null;
  51. #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  52. Task.Run(() =>
  53. {
  54. item = queue.Dequeue();
  55. });
  56. #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  57. await Task.Delay(100);
  58. Assert.AreEqual(0, queue.Count);
  59. Assert.AreEqual(null, item);
  60. queue.Enqueue("x");
  61. await Task.Delay(100);
  62. Assert.AreEqual("x", item);
  63. Assert.AreEqual(0, queue.Count);
  64. }
  65. [TestMethod]
  66. public void Wait_For_Items()
  67. {
  68. var number = 0;
  69. var queue = new BlockingQueue<int>();
  70. #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  71. Task.Run(async () =>
  72. {
  73. while (true)
  74. {
  75. queue.Enqueue(1);
  76. await Task.Delay(100);
  77. }
  78. });
  79. #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  80. while (number < 50)
  81. {
  82. queue.Dequeue();
  83. Interlocked.Increment(ref number);
  84. }
  85. }
  86. [TestMethod]
  87. [ExpectedException(typeof(OperationCanceledException))]
  88. public void Use_Disposed_Queue()
  89. {
  90. var queue = new BlockingQueue<int>();
  91. #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  92. Task.Run(() =>
  93. {
  94. Thread.Sleep(1000);
  95. queue.Dispose();
  96. });
  97. #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
  98. queue.Dequeue(new CancellationTokenSource(TimeSpan.FromSeconds(2)).Token);
  99. }
  100. }
  101. }