blocking_queue_test.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/common/blocking_queue.h"
00018 
00019 #include <memory>
00020 #include <thread>
00021 
00022 #include "absl/memory/memory.h"
00023 #include "cartographer/common/time.h"
00024 #include "gtest/gtest.h"
00025 
00026 namespace cartographer {
00027 namespace common {
00028 namespace {
00029 
00030 TEST(BlockingQueueTest, testPushPeekPop) {
00031   BlockingQueue<std::unique_ptr<int>> blocking_queue;
00032   blocking_queue.Push(absl::make_unique<int>(42));
00033   ASSERT_EQ(1, blocking_queue.Size());
00034   blocking_queue.Push(absl::make_unique<int>(24));
00035   ASSERT_EQ(2, blocking_queue.Size());
00036   EXPECT_EQ(42, *blocking_queue.Peek<int>());
00037   ASSERT_EQ(2, blocking_queue.Size());
00038   EXPECT_EQ(42, *blocking_queue.Pop());
00039   ASSERT_EQ(1, blocking_queue.Size());
00040   EXPECT_EQ(24, *blocking_queue.Pop());
00041   ASSERT_EQ(0, blocking_queue.Size());
00042   EXPECT_EQ(nullptr, blocking_queue.Peek<int>());
00043   ASSERT_EQ(0, blocking_queue.Size());
00044 }
00045 
00046 TEST(BlockingQueueTest, testPushPopSharedPtr) {
00047   BlockingQueue<std::shared_ptr<int>> blocking_queue;
00048   blocking_queue.Push(std::make_shared<int>(42));
00049   blocking_queue.Push(std::make_shared<int>(24));
00050   EXPECT_EQ(42, *blocking_queue.Pop());
00051   EXPECT_EQ(24, *blocking_queue.Pop());
00052 }
00053 
00054 TEST(BlockingQueueTest, testPopWithTimeout) {
00055   BlockingQueue<std::unique_ptr<int>> blocking_queue;
00056   EXPECT_EQ(nullptr,
00057             blocking_queue.PopWithTimeout(common::FromMilliseconds(150)));
00058 }
00059 
00060 TEST(BlockingQueueTest, testPushWithTimeout) {
00061   BlockingQueue<std::unique_ptr<int>> blocking_queue(1);
00062   EXPECT_EQ(true,
00063             blocking_queue.PushWithTimeout(absl::make_unique<int>(42),
00064                                            common::FromMilliseconds(150)));
00065   EXPECT_EQ(false,
00066             blocking_queue.PushWithTimeout(absl::make_unique<int>(15),
00067                                            common::FromMilliseconds(150)));
00068   EXPECT_EQ(42, *blocking_queue.Pop());
00069   EXPECT_EQ(0, blocking_queue.Size());
00070 }
00071 
00072 TEST(BlockingQueueTest, testPushWithTimeoutInfinteQueue) {
00073   BlockingQueue<std::unique_ptr<int>> blocking_queue;
00074   EXPECT_EQ(true,
00075             blocking_queue.PushWithTimeout(absl::make_unique<int>(42),
00076                                            common::FromMilliseconds(150)));
00077   EXPECT_EQ(true,
00078             blocking_queue.PushWithTimeout(absl::make_unique<int>(45),
00079                                            common::FromMilliseconds(150)));
00080   EXPECT_EQ(42, *blocking_queue.Pop());
00081   EXPECT_EQ(45, *blocking_queue.Pop());
00082   EXPECT_EQ(0, blocking_queue.Size());
00083 }
00084 
00085 TEST(BlockingQueueTest, testBlockingPop) {
00086   BlockingQueue<std::unique_ptr<int>> blocking_queue;
00087   ASSERT_EQ(0, blocking_queue.Size());
00088 
00089   int pop = 0;
00090 
00091   std::thread thread([&blocking_queue, &pop] { pop = *blocking_queue.Pop(); });
00092 
00093   std::this_thread::sleep_for(common::FromMilliseconds(100));
00094   blocking_queue.Push(absl::make_unique<int>(42));
00095   thread.join();
00096   ASSERT_EQ(0, blocking_queue.Size());
00097   EXPECT_EQ(42, pop);
00098 }
00099 
00100 TEST(BlockingQueueTest, testBlockingPopWithTimeout) {
00101   BlockingQueue<std::unique_ptr<int>> blocking_queue;
00102   ASSERT_EQ(0, blocking_queue.Size());
00103 
00104   int pop = 0;
00105 
00106   std::thread thread([&blocking_queue, &pop] {
00107     pop = *blocking_queue.PopWithTimeout(common::FromMilliseconds(2500));
00108   });
00109 
00110   std::this_thread::sleep_for(common::FromMilliseconds(100));
00111   blocking_queue.Push(absl::make_unique<int>(42));
00112   thread.join();
00113   ASSERT_EQ(0, blocking_queue.Size());
00114   EXPECT_EQ(42, pop);
00115 }
00116 
00117 }  // namespace
00118 }  // namespace common
00119 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35