runnable_service_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 
20 #include <chrono>
21 #include <thread>
23 
28 {
29 public:
31  has_worked_ = false;
32  };
33 
34  ~HardWorker() override = default;
35 
36  bool shutdown() override {
37  std::unique_lock <std::mutex> lck(this->test_mtx);
38  this->test_cv.notify_all(); // stop blocking in the work thread
40  }
41 
42  void work() override {
43  this->has_worked_ = true;
44 
45  // manually wait for shutdown
46  std::unique_lock <std::mutex> lck(this->test_mtx);
47  this->test_cv.wait(lck);
48  }
49 
50  bool getHasWorked() {
51  return this->has_worked_;
52  }
53 private:
55  std::condition_variable test_cv;
56  mutable std::mutex test_mtx;
57 };
58 
62 class RunnableServiceTest : public ::testing::Test {
63 public:
64  void SetUp() override
65  {
66  hard_worker = std::make_shared<HardWorker>();
67  EXPECT_EQ(ServiceState::CREATED, hard_worker->getState());
68  EXPECT_FALSE(hard_worker->isRunning());
69  }
70 
71  void TearDown() override
72  {
73  hard_worker->shutdown();
74  hard_worker->join();
75  EXPECT_EQ(ServiceState::SHUTDOWN, hard_worker->getState());
76  EXPECT_FALSE(hard_worker->isRunning());
77  hard_worker.reset();
78  }
79 
80 protected:
81  std::shared_ptr<HardWorker> hard_worker;
82 };
83 
85  ASSERT_TRUE(true);
86 }
87 
92  EXPECT_EQ(false, hard_worker->getHasWorked());
93  EXPECT_EQ(false, hard_worker->isRunning());
94 
95  // start the worker
96  EXPECT_EQ(true, hard_worker->start());
97  EXPECT_EQ(ServiceState::STARTED, hard_worker->getState());
98  // expect false on subsequent start
99  EXPECT_EQ(false, hard_worker->start());
100 
101  // wait to make sure the thread was started
102  std::this_thread::sleep_for(std::chrono::milliseconds(200));
103 
104  EXPECT_EQ(true, hard_worker->isRunning());
105  EXPECT_EQ(ServiceState::STARTED, hard_worker->getState());
106 
107  EXPECT_EQ(true, hard_worker->shutdown());
108  EXPECT_EQ(false, hard_worker->shutdown());
109 
110  hard_worker->waitForShutdown(std::chrono::milliseconds(1000)); // wait with timeout so we don't block other tests
111 
112  // did we at least work?
113  EXPECT_EQ(true, hard_worker->getHasWorked());
114 }
std::condition_variable test_cv
bool shutdown() override
void work() override
~HardWorker() override=default
std::shared_ptr< HardWorker > hard_worker
bool shutdown() override
Definition: service.h:150
TEST_F(RunnableServiceTest, Sanity)


dataflow_lite
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:22