2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/PriorityMPMCQueue.h>
18 #include <folly/portability/GTest.h>
20 using namespace folly;
22 TEST(PriorityMPMCQueue, BasicOps) {
23 // With just one priority, this should behave like a normal MPMCQueue
24 PriorityMPMCQueue<size_t> queue(1, 10);
25 EXPECT_TRUE(queue.isEmpty());
26 EXPECT_EQ(1, queue.getNumPriorities());
31 EXPECT_FALSE(queue.isEmpty());
32 EXPECT_EQ(2, queue.size());
33 EXPECT_EQ(2, queue.sizeGuess());
38 EXPECT_FALSE(queue.isEmpty());
39 EXPECT_EQ(1, queue.size());
40 EXPECT_EQ(1, queue.sizeGuess());
44 EXPECT_TRUE(queue.isEmpty());
45 EXPECT_EQ(0, queue.size());
46 EXPECT_EQ(0, queue.sizeGuess());
49 TEST(PriorityMPMCQueue, TestPriorities) {
50 PriorityMPMCQueue<size_t> queue(3, 10);
51 EXPECT_TRUE(queue.isEmpty());
52 EXPECT_EQ(3, queue.getNumPriorities());
54 // This should go to the lowpri queue, as we only
56 queue.writeWithPriority(5, 50);
57 // unqualified writes should be mid-pri
59 queue.writeWithPriority(6, 2);
60 queue.writeWithPriority(1, 0);
62 queue.writeWithPriority(2, 0);
64 EXPECT_FALSE(queue.isEmpty());
65 EXPECT_EQ(6, queue.size());
66 EXPECT_EQ(6, queue.sizeGuess());
69 for (int i = 1; i <= 6; i++) {
72 EXPECT_EQ(6 - i, queue.size());
73 EXPECT_EQ(6 - i, queue.sizeGuess());