metric_publisher_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 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 <aws/core/Aws.h>
17 #include <aws/monitoring/model/MetricDatum.h>
18 
19 #include <gtest/gtest.h>
20 
23 
25 
26 using namespace Aws::CloudWatchMetrics;
27 
28 constexpr int WAIT_TIME =
29  2000; // the amount of time (ms) to wait for publisher thread to do its work
30 
32 {
33 public:
35  uint32_t send_metrics_call_count{};
36  std::string last_metric_namespace;
38 
39  MockCloudWatchFacade() { reset(); }
40 
41  void reset()
42  {
43  this->last_metric_namespace = "";
44  this->send_metrics_ret_val = Utils::CloudWatchMetricsStatus::SUCCESS;
45  this->send_metrics_call_count = 0;
46  }
47 
49  const std::string & metric_namespace,
50  MetricDatumCollection & metrics) override
51  {
52  this->last_metric_namespace = metric_namespace;
53  this->last_metrics = metrics;
54  this->send_metrics_call_count++;
55  return this->send_metrics_ret_val;
56  }
57 };
58 
59 /* This test requires a fixture to init and shutdown the SDK or else it causes seg faults when
60  * trying to construct a CloudWatch client. All tests must use the TEST_F function with this fixutre
61  * name.
62  */
63 class TestMetricPublisherFixture : public ::testing::Test
64 {
65 protected:
66 
68  Aws::SDKOptions options_;
69  std::shared_ptr<MockCloudWatchFacade> mock_cw_;
70  std::shared_ptr<MetricPublisher> publisher_;
71 
72  void SetUp() override
73  {
74  mock_cw_ = std::make_shared<MockCloudWatchFacade>();
75  std::shared_ptr<Utils::CloudWatchMetricsFacade> cw = mock_cw_;
76  publisher_ = std::make_shared<MetricPublisher>("test_namespace", cw);
77  metrics_list_.emplace_back();
78  metrics_list_.emplace_back();
79  EXPECT_FALSE(metrics_list_.empty());
80  EXPECT_TRUE(publisher_->start());
81  }
82 
83  void TearDown() override
84  {
85  EXPECT_TRUE(publisher_->shutdown());
86  }
87 };
88 
90  ASSERT_TRUE(true);
91 }
92 
93 TEST_F(TestMetricPublisherFixture, TestLogPublisher_PublishLogs_ReturnsFalseWhenEmpty)
94 {
95  //empty list
96  MetricDatumCollection test_list;
97  EXPECT_EQ(Aws::DataFlow::UploadStatus::INVALID_DATA, publisher_->attemptPublish(test_list));
98  EXPECT_EQ(PublisherState::NOT_CONNECTED, publisher_->getPublisherState());
99  EXPECT_EQ(0u, mock_cw_->send_metrics_call_count);
100 }
101 
102 TEST_F(TestMetricPublisherFixture, TestMetricPublisher_PublishMetrics_ReturnsSuccessWhenListIngested)
103 {
104  EXPECT_EQ(Aws::DataFlow::UploadStatus::SUCCESS, publisher_->attemptPublish(metrics_list_));
105  EXPECT_EQ(PublisherState::CONNECTED, publisher_->getPublisherState());
106  EXPECT_EQ(1u, mock_cw_->send_metrics_call_count);
107 }
108 
109 TEST_F(TestMetricPublisherFixture, TestMetricPublisher_MultiplePublishes)
110 {
111 
112  EXPECT_EQ(Aws::DataFlow::UploadStatus::SUCCESS, publisher_->attemptPublish(metrics_list_));
113  EXPECT_EQ(PublisherState::CONNECTED, publisher_->getPublisherState());
114  EXPECT_EQ(1u, mock_cw_->send_metrics_call_count);
115 
116  EXPECT_EQ(Aws::DataFlow::UploadStatus::SUCCESS, publisher_->attemptPublish(metrics_list_));
117  EXPECT_EQ(PublisherState::CONNECTED, publisher_->getPublisherState());
118  EXPECT_EQ(2u, mock_cw_->send_metrics_call_count);
119 }
std::list< Aws::CloudWatch::Model::MetricDatum > MetricDatumCollection
Definition: definitions.h:23
constexpr int WAIT_TIME
TEST_F(TestMetricPublisherFixture, Sanity)
MetricDatumCollection metrics_list_
MetricDatumCollection last_metrics
Utils::CloudWatchMetricsStatus send_metrics_ret_val
This class is a simple Facade over the CloudWatch client. This class is a very small abstraction over...
std::shared_ptr< MetricPublisher > publisher_
std::shared_ptr< MockCloudWatchFacade > mock_cw_
Utils::CloudWatchMetricsStatus SendMetricsToCloudWatch(const std::string &metric_namespace, MetricDatumCollection &metrics) override
Sends a list of metrics to CloudWatch Used to send a list of metrics to CloudWatch.


cloudwatch_metrics_common
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:25