Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

BlockingQueueTests.cs 3.0 KiB

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