health_metric_collector_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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
23 #include <ros/ros.h>
24 
25 #include <vector>
26 
27 
28 namespace ros_monitoring_msgs {
29 
31 {
32 public:
33  MOCK_METHOD0(Publish, void());
34 
35  MOCK_METHOD2(AddDimension, void(const std::string &, const std::string &));
36 
37  MOCK_CONST_METHOD0(CreateMetric, MetricData());
38 
39  MOCK_METHOD1(AddMetric, void(MetricData));
40 };
41 
43 {
44 public:
46  MOCK_METHOD0(Collect, void());
47 };
48 
49 } // namespace ros_monitoring_msgs
50 
51 
52 using namespace ros_monitoring_msgs;
53 using ::testing::Return;
54 
55 
56 TEST(CollectorSuite, Child)
57 {
59 
60  std::vector<MetricCollectorInterface *> collectors;
61  MockMetricCollector mc(mg);
62  collectors.push_back(&mc);
63 
64  // start metrics collection
65  CollectAndPublish f(mg, collectors);
66 
67  MetricData md;
68  ON_CALL(mg, CreateMetric()).WillByDefault(Return(md));
69  EXPECT_CALL(mg, Publish()).Times(1);
70  EXPECT_CALL(mc, Collect()).Times(1);
71 
72  ros::TimerEvent event;
73  f(event);
74 }
75 
76 TEST(CollectorSuite, sysinfo)
77 {
79  MetricData md;
80  ON_CALL(mg, CreateMetric()).WillByDefault(Return(md));
81 
82  EXPECT_CALL(mg, AddMetric(testing::_)).Times(4);
83  SysInfoCollector sys_collector(mg);
84  sys_collector.Collect();
85 }
86 
87 TEST(CollectorSuite, cpu_usage_0)
88 {
90  MetricData md;
91  ON_CALL(mg, CreateMetric()).WillByDefault(Return(md));
92 
93  EXPECT_CALL(mg, AddMetric(testing::_)).Times(0);
94 
95  CPUMetricCollector cpu_collector(mg);
96  cpu_collector.Collect();
97 }
98 
99 TEST(CollectorSuite, cpu_usage_1)
100 {
102  MetricData md;
103  ON_CALL(mg, CreateMetric()).WillByDefault(Return(md));
104 
105  EXPECT_CALL(mg, AddMetric(testing::_)).Times(testing::AtLeast(1));
106 
107  CPUMetricCollector cpu_collector(mg);
108  cpu_collector.Collect();
109  cpu_collector.Collect();
110 }
111 
112 int main(int argc, char ** argv)
113 {
114  testing::InitGoogleTest(&argc, argv);
115  ros::init(argc, argv, "test_collector");
116 
117  return RUN_ALL_TESTS();
118 }
f
virtual void AddDimension(const std::string &name, const std::string &value)=0
add global dimension (applies to all metrics).
MOCK_METHOD1(AddMetric, void(MetricData))
virtual MetricData CreateMetric() const =0
create a metric.
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
TEST(CollectorSuite, Child)
virtual void Publish()=0
publishes all metrics and then discards them.
void Collect() overridefinal
callback for collecting metrics.
MOCK_METHOD2(AddDimension, void(const std::string &, const std::string &))
void Collect() overridefinal
activates metrics collection.
MOCK_CONST_METHOD0(CreateMetric, MetricData())
Interface for a metrics collector.
collects metrics from sysinfo.
activates collectors and then publishes metrics.
virtual void AddMetric(MetricData md)=0
add a metric to list of metrics to be published.


health_metric_collector
Author(s): AWS RoboMaker
autogenerated on Fri Mar 5 2021 03:32:41