1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_metric/metric_service_nanopb.h"
16
17 #include "gtest/gtest.h"
18 #include "pw_log/log.h"
19 #include "pw_rpc/nanopb/test_method_context.h"
20
21 namespace pw::metric {
22 namespace {
23
24 #define MetricMethodContext \
25 PW_NANOPB_TEST_METHOD_CONTEXT(MetricService, Get, 4, 256)
26
TEST(MetricService,EmptyGroupAndNoMetrics)27 TEST(MetricService, EmptyGroupAndNoMetrics) {
28 // Empty root group.
29 PW_METRIC_GROUP(root, "/");
30
31 // Run the RPC and ensure it completes.
32 MetricMethodContext context(root.metrics(), root.children());
33 context.call({});
34 EXPECT_TRUE(context.done());
35 EXPECT_EQ(OkStatus(), context.status());
36
37 // No metrics should be in the response.
38 EXPECT_EQ(0u, context.responses().size());
39 }
40
TEST(MetricService,FlatMetricsNoGroupsOneResponseOnly)41 TEST(MetricService, FlatMetricsNoGroupsOneResponseOnly) {
42 // Set up a one-group suite of metrics.
43 PW_METRIC_GROUP(root, "/");
44 PW_METRIC(root, a, "a", 1.0);
45 PW_METRIC(root, b, "b", 1.0);
46 PW_METRIC(root, c, "c", 1.0);
47 PW_METRIC(root, d, "d", 1.0);
48 PW_METRIC(root, e, "e", 1.0);
49
50 // Run the RPC and ensure it completes.
51 MetricMethodContext context(root.metrics(), root.children());
52 context.call({});
53 EXPECT_TRUE(context.done());
54 EXPECT_EQ(OkStatus(), context.status());
55
56 // All of the responses should have fit in one proto.
57 EXPECT_EQ(1u, context.responses().size());
58 EXPECT_EQ(5, context.responses()[0].metrics_count);
59 }
60
TEST(MetricService,NestedGroupsButOnlyOneBatch)61 TEST(MetricService, NestedGroupsButOnlyOneBatch) {
62 // Set up a nested group of metrics that will fit in the default batch (10).
63 PW_METRIC_GROUP(root, "/");
64 PW_METRIC(root, a, "a", 1.0);
65 PW_METRIC(root, b, "b", 1.0);
66 PW_METRIC(root, c, "c", 1.0);
67
68 PW_METRIC_GROUP(inner, "inner");
69 PW_METRIC(inner, x, "x", 1.0);
70 PW_METRIC(inner, y, "y", 1.0);
71 PW_METRIC(inner, z, "z", 1.0);
72
73 root.Add(inner);
74
75 // Run the RPC and ensure it completes.
76 MetricMethodContext context(root.metrics(), root.children());
77 context.call({});
78 EXPECT_TRUE(context.done());
79 EXPECT_EQ(OkStatus(), context.status());
80
81 // All of the responses should fit in one proto.
82 EXPECT_EQ(1u, context.responses().size());
83 EXPECT_EQ(6, context.responses()[0].metrics_count);
84 }
85
TEST(MetricService,NestedGroupsWithBatches)86 TEST(MetricService, NestedGroupsWithBatches) {
87 // Set up a nested group of metrics that will not fit in a single batch.
88 PW_METRIC_GROUP(root, "/");
89 PW_METRIC(root, a, "a", 1u);
90 PW_METRIC(root, d, "d", 2u);
91 PW_METRIC(root, f, "f", 3u);
92
93 PW_METRIC_GROUP(inner_1, "inner1");
94 PW_METRIC(inner_1, x, "x", 4u);
95 PW_METRIC(inner_1, y, "y", 5u);
96 PW_METRIC(inner_1, z, "z", 6u);
97
98 PW_METRIC_GROUP(inner_2, "inner2");
99 PW_METRIC(inner_2, p, "p", 7u);
100 PW_METRIC(inner_2, q, "q", 8u);
101 PW_METRIC(inner_2, r, "r", 9u);
102 PW_METRIC(inner_2, s, "s", 10u); // Note: Max # per response is 10.
103 PW_METRIC(inner_2, t, "s", 11u);
104 PW_METRIC(inner_2, u, "s", 12u);
105
106 root.Add(inner_1);
107 root.Add(inner_2);
108
109 // Run the RPC and ensure it completes.
110 MetricMethodContext context(root.metrics(), root.children());
111 context.call({});
112 EXPECT_TRUE(context.done());
113 EXPECT_EQ(OkStatus(), context.status());
114
115 // The response had to be split into two parts; check that they have the
116 // appropriate sizes.
117 EXPECT_EQ(2u, context.responses().size());
118 EXPECT_EQ(10, context.responses()[0].metrics_count);
119 EXPECT_EQ(2, context.responses()[1].metrics_count);
120
121 // The metrics are the numbers 1..12; sum them and compare.
122 uint32_t metric_sum = 0;
123 for (const auto& response : context.responses()) {
124 for (unsigned i = 0; i < response.metrics_count; ++i) {
125 metric_sum += response.metrics[i].value.as_int;
126 }
127 }
128 EXPECT_EQ(78u, metric_sum);
129
130 // TODO(keir): Properly check all the fields.
131 }
132
TokenPathsMatch(uint32_t expected_token_path[5],const pw_metric_proto_Metric & metric)133 bool TokenPathsMatch(uint32_t expected_token_path[5],
134 const pw_metric_proto_Metric& metric) {
135 // Calculate length of expected token & compare.
136 int expected_length = 0;
137 while (expected_token_path[expected_length]) {
138 expected_length++;
139 }
140 if (expected_length != metric.token_path_count) {
141 return false;
142 }
143
144 // Lengths match; so search the tokens themselves.
145 for (int i = 0; i < expected_length; ++i) {
146 if (expected_token_path[i] != metric.token_path[i]) {
147 return false;
148 }
149 }
150 return true;
151 }
152
TEST(MetricService,TokenPaths)153 TEST(MetricService, TokenPaths) {
154 // Set up a nested group of metrics that will not fit in a single batch.
155 PW_METRIC_GROUP(root, "/");
156 PW_METRIC(root, a, "a", 1u);
157
158 PW_METRIC_GROUP(inner_1, "inner1");
159 PW_METRIC(inner_1, x, "x", 4u);
160 PW_METRIC(inner_1, z, "z", 6u);
161
162 PW_METRIC_GROUP(inner_2, "inner2");
163 PW_METRIC(inner_2, p, "p", 7u);
164 PW_METRIC(inner_2, u, "s", 12u);
165
166 root.Add(inner_1);
167 root.Add(inner_2);
168
169 // Run the RPC and ensure it completes.
170 MetricMethodContext context(root.metrics(), root.children());
171 context.call({});
172 EXPECT_TRUE(context.done());
173 EXPECT_EQ(OkStatus(), context.status());
174
175 // The metrics should fit in one batch.
176 EXPECT_EQ(1u, context.responses().size());
177 EXPECT_EQ(5, context.responses()[0].metrics_count);
178
179 // Declare the token paths we expect to find.
180 // Note: This depends on the token variables from the PW_METRIC*() macros.
181 uint32_t expected_token_paths[5][5] = {
182 {a_token, 0u},
183 {inner_1_token, x_token, 0u},
184 {inner_1_token, z_token, 0u},
185 {inner_2_token, p_token, 0u},
186 {inner_2_token, u_token, 0u},
187 };
188
189 // For each expected token, search through all returned metrics to find it.
190 // The search is necessary since there is no guarantee of metric ordering.
191 for (auto& expected_token_path : expected_token_paths) {
192 int found_matches = 0;
193 // Note: There should only be 1 response.
194 for (const auto& response : context.responses()) {
195 for (unsigned m = 0; m < response.metrics_count; ++m) {
196 if (TokenPathsMatch(expected_token_path, response.metrics[m])) {
197 found_matches++;
198 }
199 }
200 }
201 EXPECT_EQ(found_matches, 1);
202 }
203 }
204
205 } // namespace
206 } // namespace pw::metric
207