Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

109 linhas
3.0 KiB

  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using MQTTnet.Internal;
  5. namespace MQTTnet.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. }