queue_monitor_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 #include <chrono>
17 #include <memory>
18 
19 #include <gtest/gtest.h>
20 #include <gmock/gmock.h>
21 
24 
25 using namespace Aws::DataFlow;
26 using namespace ::testing;
27 
29  public IObservedQueue<std::string>
30 {
31 public:
32  MOCK_METHOD0(clear, void (void));
33  MOCK_CONST_METHOD0(size, size_t (void));
34  MOCK_CONST_METHOD0(empty, bool (void));
35  MOCK_METHOD2(dequeue, bool (std::string& data,
36  const std::chrono::microseconds& duration));
37  MOCK_METHOD1(enqueue, bool (std::string& data));
38  MOCK_METHOD2(tryEnqueue,
39  bool (std::string& data,
40  const std::chrono::microseconds &duration));
41  inline bool enqueue(std::string&&) override {
42  return false;
43  }
44 
45  inline bool tryEnqueue(
46  std::string&& value,
47  const std::chrono::microseconds&) override
48  {
49  return enqueue(value);
50  }
51 
57  inline void setStatusMonitor(std::shared_ptr<StatusMonitor> status_monitor) override {
58  status_monitor_ = status_monitor;
59  }
60 
64  std::shared_ptr<StatusMonitor> status_monitor_;
65 };
66 
68  std::string& data,
69  std::string actual,
70  const std::chrono::microseconds&)
71 {
72  data = std::move(actual);
73  return true;
74 }
75 
76 using std::placeholders::_1;
77 using std::placeholders::_2;
78 
79 TEST(queue_demux_test, Sanity) {
80  ASSERT_TRUE(true);
81 }
82 
83 TEST(queue_demux_test, single_source_test)
84 {
85  auto observed_queue = std::make_shared<MockObservedQueue>();
86  std::shared_ptr<StatusMonitor> monitor;
87  std::string actual = "test_string";
88  EXPECT_CALL(*observed_queue, dequeue(_, _))
89  .WillOnce(Invoke([=](auto && arg1, auto && arg2) { return dequeueFunc(arg1, actual, arg2); }));
90  QueueMonitor<std::string> queue_monitor;
91  queue_monitor.addSource(observed_queue, PriorityOptions());
92  std::string data;
93  EXPECT_TRUE(queue_monitor.dequeue(data, std::chrono::microseconds(0)));
94  EXPECT_EQ(actual, data);
95 }
96 
97 TEST(queue_demux_test, multi_source_test)
98 {
99  QueueMonitor<std::string> queue_monitor;
100  auto low_priority_queue = std::make_shared<StrictMock<MockObservedQueue>>();
101  EXPECT_CALL(*low_priority_queue, dequeue(_, _))
102  .WillOnce(Invoke([=](auto && arg1, auto && arg2) { return dequeueFunc(arg1, "low_priority", arg2); }))
103  .WillRepeatedly(Return(false));
104  queue_monitor.addSource(low_priority_queue, PriorityOptions(LOWEST_PRIORITY));
105 
106  auto high_priority_observed_queue = std::make_shared<StrictMock<MockObservedQueue>>();
107  std::shared_ptr<StatusMonitor> monitor;
108  EXPECT_CALL(*high_priority_observed_queue, dequeue(_, _))
109  .WillOnce(Invoke([=](auto && arg1, auto && arg2) { return dequeueFunc(arg1, "high_priority", arg2); }))
110  .WillRepeatedly(Return(false));;
111  queue_monitor.addSource(high_priority_observed_queue, PriorityOptions(HIGHEST_PRIORITY));
112  std::string data;
113  EXPECT_TRUE(queue_monitor.dequeue(data, std::chrono::microseconds(0)));
114  EXPECT_EQ("high_priority", data);
115  EXPECT_TRUE(queue_monitor.dequeue(data, std::chrono::microseconds(0)));
116  EXPECT_EQ("low_priority", data);
117  EXPECT_FALSE(queue_monitor.dequeue(data, std::chrono::microseconds(0)));
118 }
119 
120 int main(int argc, char ** argv)
121 {
122  testing::InitGoogleMock(&argc, argv);
123  return RUN_ALL_TESTS();
124 }
bool dequeueFunc(std::string &data, std::string actual, const std::chrono::microseconds &)
bool enqueue(std::string &&) override
bool tryEnqueue(std::string &&value, const std::chrono::microseconds &) override
void setStatusMonitor(std::shared_ptr< StatusMonitor > status_monitor) override
void addSource(std::shared_ptr< IObservedQueue< T >>observed_queue, PriorityOptions priority_options) override
Definition: queue_monitor.h:51
TEST(queue_demux_test, Sanity)
int main(int argc, char **argv)
bool dequeue(T &data, const std::chrono::microseconds &duration) override
Definition: queue_monitor.h:67
std::shared_ptr< StatusMonitor > status_monitor_


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