publisher_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 <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 
20 #include <thread>
21 #include <string>
22 
26 class SimpleTestPublisher : public Publisher<std::string>
27 {
28 public:
29 
31  ~SimpleTestPublisher() override = default;
32 
33  Aws::DataFlow::UploadStatus publishData(std::string &data) override {
34  std::this_thread::sleep_for(std::chrono::milliseconds(500));
35  last_data = data;
37  }
38 
39  void setShouldSucceed(bool nv) {
40  should_succeed_ = nv;
41  }
42 
43  std::string last_data;
45 };
46 
50 class PublisherTest : public ::testing::Test {
51 public:
52 
53  void SetUp() override {
54 
55  test_publisher = std::make_shared<SimpleTestPublisher>();
56 
57  EXPECT_EQ(PublisherState::UNKNOWN, test_publisher->getPublisherState());
58  EXPECT_EQ(ServiceState::CREATED, test_publisher->getState());
59  EXPECT_EQ(0, test_publisher->getPublishAttempts());
60  EXPECT_EQ(0, test_publisher->getPublishSuccesses());
61  EXPECT_EQ(0.0f, test_publisher->getPublishSuccessPercentage());
62  EXPECT_EQ(std::chrono::milliseconds(0), test_publisher->getLastPublishDuration());
63  }
64 
65  void TearDown() override {
66  test_publisher.reset();
67  }
68 protected:
69  std::shared_ptr<SimpleTestPublisher> test_publisher;
70 };
71 
76  ASSERT_TRUE(true);
77 }
78 
82 TEST_F(PublisherTest, TestPublishFailNotStarted) {
83 
84  std::string data("They're taking the hobbits to Isengard!");
85  auto status = test_publisher->attemptPublish(data);
86 
87  EXPECT_EQ(Aws::DataFlow::UploadStatus::FAIL, status);
88  EXPECT_FALSE(test_publisher->canPublish());
89  EXPECT_EQ(0, test_publisher->getPublishAttempts());
90  EXPECT_EQ(std::chrono::milliseconds(0), test_publisher->getLastPublishDuration());
91  EXPECT_EQ(PublisherState::UNKNOWN, test_publisher->getPublisherState());
92 }
93 
97 TEST_F(PublisherTest, TestPublishSuccessWhenStarted) {
98 
99  std::string data("They're taking the hobbits to Isengard!");
100 
101  bool b = test_publisher->start();
102  EXPECT_TRUE(b);
103  EXPECT_TRUE(test_publisher->canPublish());
104 
105  auto status = test_publisher->attemptPublish(data);
106  EXPECT_EQ(Aws::DataFlow::UploadStatus::SUCCESS, status);
107  EXPECT_EQ(1, test_publisher->getPublishAttempts());
108  EXPECT_EQ(1, test_publisher->getPublishSuccesses());
109  EXPECT_EQ(100.0f, test_publisher->getPublishSuccessPercentage());
110  EXPECT_LT(std::chrono::milliseconds(0), test_publisher->getLastPublishDuration());
111  EXPECT_EQ(PublisherState::CONNECTED, test_publisher->getPublisherState());
112 }
113 
117 TEST_F(PublisherTest, TestPublishFailure) {
118 
119  std::string data("They're taking the hobbits to Isengard!");
120 
121  bool b = test_publisher->start();
122  EXPECT_TRUE(b);
123  EXPECT_TRUE(test_publisher->canPublish());
124 
125  auto status = test_publisher->attemptPublish(data);
126 
127  EXPECT_EQ(Aws::DataFlow::UploadStatus::SUCCESS, status);
128  EXPECT_EQ(1, test_publisher->getPublishAttempts());
129  EXPECT_EQ(1, test_publisher->getPublishSuccesses());
130  EXPECT_EQ(100.0f, test_publisher->getPublishSuccessPercentage());
131  EXPECT_LT(std::chrono::milliseconds(0), test_publisher->getLastPublishDuration());
132  EXPECT_EQ(PublisherState::CONNECTED, test_publisher->getPublisherState());
133 
134  test_publisher->setShouldSucceed(false);
135  status = test_publisher->attemptPublish(data);
136 
137  EXPECT_EQ(Aws::DataFlow::UploadStatus::FAIL, status);
138  EXPECT_EQ(2, test_publisher->getPublishAttempts());
139  EXPECT_EQ(1, test_publisher->getPublishSuccesses());
140  EXPECT_EQ(50.0f, test_publisher->getPublishSuccessPercentage());
141  EXPECT_LT(std::chrono::milliseconds(0), test_publisher->getLastPublishDuration());
142  EXPECT_EQ(PublisherState::NOT_CONNECTED, test_publisher->getPublisherState());
143 }
144 
149 TEST_F(PublisherTest, TestPublisherShutdown) {
150 
151  std::string data("They're taking the hobbits to Isengard!");
152 
153  bool b = test_publisher->start();
154  EXPECT_TRUE(b);
155  EXPECT_TRUE(test_publisher->canPublish());
156  EXPECT_EQ(ServiceState::STARTED, test_publisher->getState());
157 
158  std::thread pub_thread(&SimpleTestPublisher::attemptPublish, test_publisher, std::ref(data));
159 
160  // let the attemptPublish thread start and hold the lock
161  std::this_thread::sleep_for(std::chrono::milliseconds(200));
162  b = test_publisher->shutdown();
163 
164  EXPECT_TRUE(b);
165  EXPECT_EQ(data, test_publisher->last_data);
166  EXPECT_EQ(ServiceState::SHUTDOWN, test_publisher->getState());
167  EXPECT_EQ(PublisherState::UNKNOWN, test_publisher->getPublisherState());
168 
169  // try to publish again but expect fast fail
170  auto status = test_publisher->attemptPublish(data);
171 
172  EXPECT_EQ(Aws::DataFlow::UploadStatus::FAIL, status);
173  EXPECT_LT(std::chrono::milliseconds(0), test_publisher->getLastPublishDuration());
174  EXPECT_FALSE(test_publisher->canPublish());
175 
176  pub_thread.join();
177 }
void setShouldSucceed(bool nv)
UploadStatus
Definition: task.h:33
~SimpleTestPublisher() override=default
std::shared_ptr< SimpleTestPublisher > test_publisher
Aws::DataFlow::UploadStatus attemptPublish(std::string &data) override
Definition: publisher.h:75
void TearDown() override
Aws::DataFlow::UploadStatus publishData(std::string &data) override
void SetUp() override
TEST_F(PublisherTest, Sanity)


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