PriorityMPMCQueueTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/PriorityMPMCQueue.h>
  17. #include <folly/portability/GTest.h>
  18. using namespace folly;
  19. TEST(PriorityMPMCQueue, BasicOps) {
  20. // With just one priority, this should behave like a normal MPMCQueue
  21. PriorityMPMCQueue<size_t> queue(1, 10);
  22. EXPECT_TRUE(queue.isEmpty());
  23. EXPECT_EQ(1, queue.getNumPriorities());
  24. queue.write(9);
  25. queue.write(8);
  26. EXPECT_FALSE(queue.isEmpty());
  27. EXPECT_EQ(2, queue.size());
  28. EXPECT_EQ(2, queue.sizeGuess());
  29. size_t item;
  30. queue.read(item);
  31. EXPECT_EQ(9, item);
  32. EXPECT_FALSE(queue.isEmpty());
  33. EXPECT_EQ(1, queue.size());
  34. EXPECT_EQ(1, queue.sizeGuess());
  35. queue.read(item);
  36. EXPECT_EQ(8, item);
  37. EXPECT_TRUE(queue.isEmpty());
  38. EXPECT_EQ(0, queue.size());
  39. EXPECT_EQ(0, queue.sizeGuess());
  40. }
  41. TEST(PriorityMPMCQueue, TestPriorities) {
  42. PriorityMPMCQueue<size_t> queue(3, 10);
  43. EXPECT_TRUE(queue.isEmpty());
  44. EXPECT_EQ(3, queue.getNumPriorities());
  45. // This should go to the lowpri queue, as we only
  46. // have 3 priorities
  47. queue.writeWithPriority(5, 50);
  48. // unqualified writes should be mid-pri
  49. queue.write(3);
  50. queue.writeWithPriority(6, 2);
  51. queue.writeWithPriority(1, 0);
  52. queue.write(4);
  53. queue.writeWithPriority(2, 0);
  54. EXPECT_FALSE(queue.isEmpty());
  55. EXPECT_EQ(6, queue.size());
  56. EXPECT_EQ(6, queue.sizeGuess());
  57. size_t item;
  58. for (int i = 1; i <= 6; i++) {
  59. queue.read(item);
  60. EXPECT_EQ(i, item);
  61. EXPECT_EQ(6 - i, queue.size());
  62. EXPECT_EQ(6 - i, queue.sizeGuess());
  63. }
  64. }
  65. TEST(PriorityMPMCQueue, TestReadWithPriority) {
  66. PriorityMPMCQueue<size_t> queue(3, 10);
  67. EXPECT_TRUE(queue.isEmpty());
  68. EXPECT_EQ(3, queue.getNumPriorities());
  69. queue.writeWithPriority(2, 2);
  70. queue.writeWithPriority(1, 1);
  71. queue.writeWithPriority(0, 0);
  72. EXPECT_FALSE(queue.isEmpty());
  73. EXPECT_EQ(3, queue.size());
  74. EXPECT_EQ(3, queue.sizeGuess());
  75. size_t item;
  76. for (int i = 0; i < 3; i++) {
  77. EXPECT_TRUE(queue.readWithPriority(item, i));
  78. EXPECT_EQ(i, item);
  79. EXPECT_FALSE(queue.readWithPriority(item, i));
  80. }
  81. }
  82. TEST(PriorityMPMCQueue, TestWriteWithPriorityAndTimeout) {
  83. PriorityMPMCQueue<size_t> queue(5, 1);
  84. EXPECT_TRUE(queue.isEmpty());
  85. EXPECT_EQ(5, queue.getNumPriorities());
  86. const auto timeout = std::chrono::milliseconds{30};
  87. for (int i = 0; i < 5; i++) {
  88. auto time_before = std::chrono::steady_clock::now();
  89. EXPECT_TRUE(queue.writeWithPriority(i, i, timeout));
  90. auto time_after = std::chrono::steady_clock::now();
  91. EXPECT_LE(time_after - time_before, timeout);
  92. }
  93. // check writeWithPriority will wait for at least timeout if the queue is
  94. // full.
  95. auto time_before = std::chrono::steady_clock::now();
  96. EXPECT_FALSE(queue.writeWithPriority(5, 0, timeout));
  97. auto time_after = std::chrono::steady_clock::now();
  98. EXPECT_GE(time_after - time_before, timeout);
  99. EXPECT_FALSE(queue.isEmpty());
  100. EXPECT_EQ(5, queue.size());
  101. EXPECT_EQ(5, queue.sizeGuess());
  102. size_t item;
  103. for (int i = 0; i < 5; i++) {
  104. queue.read(item);
  105. EXPECT_EQ(i, item);
  106. EXPECT_EQ(4 - i, queue.size());
  107. EXPECT_EQ(4 - i, queue.sizeGuess());
  108. }
  109. }