blocking_queue_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
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 
18 
19 #include <memory>
20 #include <thread>
21 
24 #include "gtest/gtest.h"
25 
26 namespace cartographer {
27 namespace common {
28 namespace {
29 
30 TEST(BlockingQueueTest, testPushPeekPop) {
31  BlockingQueue<std::unique_ptr<int>> blocking_queue;
32  blocking_queue.Push(common::make_unique<int>(42));
33  ASSERT_EQ(1, blocking_queue.Size());
34  blocking_queue.Push(common::make_unique<int>(24));
35  ASSERT_EQ(2, blocking_queue.Size());
36  EXPECT_EQ(42, *blocking_queue.Peek<int>());
37  ASSERT_EQ(2, blocking_queue.Size());
38  EXPECT_EQ(42, *blocking_queue.Pop());
39  ASSERT_EQ(1, blocking_queue.Size());
40  EXPECT_EQ(24, *blocking_queue.Pop());
41  ASSERT_EQ(0, blocking_queue.Size());
42  EXPECT_EQ(nullptr, blocking_queue.Peek<int>());
43  ASSERT_EQ(0, blocking_queue.Size());
44 }
45 
46 TEST(BlockingQueueTest, testPushPopSharedPtr) {
47  BlockingQueue<std::shared_ptr<int>> blocking_queue;
48  blocking_queue.Push(std::make_shared<int>(42));
49  blocking_queue.Push(std::make_shared<int>(24));
50  EXPECT_EQ(42, *blocking_queue.Pop());
51  EXPECT_EQ(24, *blocking_queue.Pop());
52 }
53 
54 TEST(BlockingQueueTest, testPopWithTimeout) {
55  BlockingQueue<std::unique_ptr<int>> blocking_queue;
56  EXPECT_EQ(nullptr,
57  blocking_queue.PopWithTimeout(common::FromMilliseconds(150)));
58 }
59 
60 TEST(BlockingQueueTest, testPushWithTimeout) {
61  BlockingQueue<std::unique_ptr<int>> blocking_queue(1);
62  EXPECT_EQ(true,
63  blocking_queue.PushWithTimeout(common::make_unique<int>(42),
65  EXPECT_EQ(false,
66  blocking_queue.PushWithTimeout(common::make_unique<int>(15),
68  EXPECT_EQ(42, *blocking_queue.Pop());
69  EXPECT_EQ(0, blocking_queue.Size());
70 }
71 
72 TEST(BlockingQueueTest, testPushWithTimeoutInfinteQueue) {
73  BlockingQueue<std::unique_ptr<int>> blocking_queue;
74  EXPECT_EQ(true,
75  blocking_queue.PushWithTimeout(common::make_unique<int>(42),
77  EXPECT_EQ(true,
78  blocking_queue.PushWithTimeout(common::make_unique<int>(45),
80  EXPECT_EQ(42, *blocking_queue.Pop());
81  EXPECT_EQ(45, *blocking_queue.Pop());
82  EXPECT_EQ(0, blocking_queue.Size());
83 }
84 
85 TEST(BlockingQueueTest, testBlockingPop) {
86  BlockingQueue<std::unique_ptr<int>> blocking_queue;
87  ASSERT_EQ(0, blocking_queue.Size());
88 
89  int pop = 0;
90 
91  std::thread thread([&blocking_queue, &pop] { pop = *blocking_queue.Pop(); });
92 
93  std::this_thread::sleep_for(common::FromMilliseconds(100));
94  blocking_queue.Push(common::make_unique<int>(42));
95  thread.join();
96  ASSERT_EQ(0, blocking_queue.Size());
97  EXPECT_EQ(42, pop);
98 }
99 
100 TEST(BlockingQueueTest, testBlockingPopWithTimeout) {
101  BlockingQueue<std::unique_ptr<int>> blocking_queue;
102  ASSERT_EQ(0, blocking_queue.Size());
103 
104  int pop = 0;
105 
106  std::thread thread([&blocking_queue, &pop] {
107  pop = *blocking_queue.PopWithTimeout(common::FromMilliseconds(2500));
108  });
109 
110  std::this_thread::sleep_for(common::FromMilliseconds(100));
111  blocking_queue.Push(common::make_unique<int>(42));
112  thread.join();
113  ASSERT_EQ(0, blocking_queue.Size());
114  EXPECT_EQ(42, pop);
115 }
116 
117 } // namespace
118 } // namespace common
119 } // namespace cartographer
common::Duration FromMilliseconds(const int64 milliseconds)
Definition: time.cc:43
TEST(TrajectoryConnectivityStateTest, UnknownTrajectory)


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58