1 /*
2  * Copyright 2019 The Android Open Source Project
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 
17 #include "common/blocking_queue.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <thread>
22 
23 namespace bluetooth {
24 namespace common {
25 namespace {
26 class BlockingQueueTest : public ::testing::Test {
27  protected:
SetUp()28   void SetUp() override {
29     EXPECT_TRUE(queue_.empty());
30   }
31 
32   // Postcondition for each test case: clear the blocking queue
TearDown()33   void TearDown() override {
34     EXPECT_TRUE(queue_.empty());
35   }
36 
37   BlockingQueue<int> queue_;
38 };
39 
TEST_F(BlockingQueueTest,initial_empty)40 TEST_F(BlockingQueueTest, initial_empty) {
41   EXPECT_TRUE(queue_.empty());
42 }
43 
TEST_F(BlockingQueueTest,same_thread_push_and_pop)44 TEST_F(BlockingQueueTest, same_thread_push_and_pop) {
45   int data = 1;
46   queue_.push(data);
47   EXPECT_FALSE(queue_.empty());
48   EXPECT_EQ(queue_.take(), data);
49   EXPECT_TRUE(queue_.empty());
50 }
51 
TEST_F(BlockingQueueTest,same_thread_push_and_pop_sequential)52 TEST_F(BlockingQueueTest, same_thread_push_and_pop_sequential) {
53   for (int data = 0; data < 10; data++) {
54     queue_.push(data);
55     EXPECT_FALSE(queue_.empty());
56     EXPECT_EQ(queue_.take(), data);
57     EXPECT_TRUE(queue_.empty());
58   }
59 }
60 
TEST_F(BlockingQueueTest,same_thread_push_and_pop_batch)61 TEST_F(BlockingQueueTest, same_thread_push_and_pop_batch) {
62   for (int data = 0; data < 10; data++) {
63     queue_.push(data);
64   }
65   EXPECT_FALSE(queue_.empty());
66   for (int data = 0; data < 10; data++) {
67     EXPECT_EQ(queue_.take(), data);
68   }
69   EXPECT_TRUE(queue_.empty());
70 }
71 
TEST_F(BlockingQueueTest,clear_queue)72 TEST_F(BlockingQueueTest, clear_queue) {
73   for (int data = 0; data < 10; data++) {
74     queue_.push(data);
75   }
76   EXPECT_FALSE(queue_.empty());
77   queue_.clear();
78   EXPECT_TRUE(queue_.empty());
79 }
80 
TEST_F(BlockingQueueTest,wait_for_non_empty)81 TEST_F(BlockingQueueTest, wait_for_non_empty) {
82   int data = 1;
83   std::thread waiter_thread([this, data] { EXPECT_EQ(queue_.take(), data); });
84   queue_.push(data);
85   waiter_thread.join();
86   EXPECT_TRUE(queue_.empty());
87 }
88 
TEST_F(BlockingQueueTest,wait_to_take_fail)89 TEST_F(BlockingQueueTest, wait_to_take_fail) {
90   EXPECT_FALSE(queue_.wait_to_take(std::chrono::milliseconds(3)));
91 }
92 
TEST_F(BlockingQueueTest,wait_to_take_after_non_empty)93 TEST_F(BlockingQueueTest, wait_to_take_after_non_empty) {
94   int data = 1;
95   queue_.push(data);
96   EXPECT_TRUE(queue_.wait_to_take(std::chrono::milliseconds(3)));
97   queue_.clear();
98 }
99 
TEST_F(BlockingQueueTest,wait_to_take_before_non_empty)100 TEST_F(BlockingQueueTest, wait_to_take_before_non_empty) {
101   int data = 1;
102   std::thread waiter_thread([this] { EXPECT_TRUE(queue_.wait_to_take(std::chrono::milliseconds(3))); });
103   queue_.push(data);
104   waiter_thread.join();
105   queue_.clear();
106 }
107 
TEST_F(BlockingQueueTest,wait_for_non_empty_batch)108 TEST_F(BlockingQueueTest, wait_for_non_empty_batch) {
109   std::thread waiter_thread([this] {
110     for (int data = 0; data < 10; data++) {
111       EXPECT_EQ(queue_.take(), data);
112     }
113   });
114   for (int data = 0; data < 10; data++) {
115     queue_.push(data);
116   }
117   waiter_thread.join();
118   EXPECT_TRUE(queue_.empty());
119 }
120 
121 class VectorBlockingQueueTest : public ::testing::Test {
122  protected:
SetUp()123   void SetUp() override {
124     EXPECT_TRUE(queue_.empty());
125   }
126 
127   // Postcondition for each test case: clear the blocking queue
TearDown()128   void TearDown() override {
129     EXPECT_TRUE(queue_.empty());
130   }
131 
132   BlockingQueue<std::vector<uint8_t>> queue_;
133 };
134 
TEST_F(VectorBlockingQueueTest,same_thread_push_and_pop)135 TEST_F(VectorBlockingQueueTest, same_thread_push_and_pop) {
136   std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6};
137   queue_.push(data);
138   EXPECT_FALSE(queue_.empty());
139   EXPECT_EQ(queue_.take(), data);
140   EXPECT_TRUE(queue_.empty());
141 }
142 
143 }  // namespace
144 }  // namespace common
145 }  // namespace bluetooth
146