Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 1 | #include "fakes/clock_fake.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 2 | #include "helpers.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 3 | #include "metric.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 4 | #include "mocks/sensor_mock.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 5 | #include "params/metric_params.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 6 | #include "utils/conv_container.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 7 | #include "utils/conversion.hpp" |
| 8 | #include "utils/tstring.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 9 | |
| 10 | #include <gmock/gmock.h> |
| 11 | |
| 12 | using namespace testing; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 13 | using namespace std::chrono_literals; |
| 14 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 15 | namespace tstring = utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 16 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 17 | constexpr Milliseconds systemTimestamp = 42ms; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 18 | |
| 19 | class TestMetric : public Test |
| 20 | { |
| 21 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 22 | TestMetric() |
| 23 | { |
| 24 | clockFake.steady.reset(); |
| 25 | clockFake.system.set(systemTimestamp); |
| 26 | } |
| 27 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 28 | static std::vector<std::shared_ptr<SensorMock>> |
| 29 | makeSensorMocks(size_t amount) |
| 30 | { |
| 31 | std::vector<std::shared_ptr<SensorMock>> result; |
| 32 | for (size_t i = 0; i < amount; ++i) |
| 33 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 34 | auto& metricMock = |
| 35 | result.emplace_back(std::make_shared<NiceMock<SensorMock>>()); |
| 36 | ON_CALL(*metricMock, metadata()).WillByDefault(Return("metadata")); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 37 | } |
| 38 | return result; |
| 39 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 40 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 41 | std::shared_ptr<Metric> makeSut(const MetricParams& p) |
| 42 | { |
| 43 | return std::make_shared<Metric>( |
| 44 | utils::convContainer<std::shared_ptr<interfaces::Sensor>>( |
| 45 | sensorMocks), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 46 | p.operationType(), p.id(), p.collectionTimeScope(), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 47 | p.collectionDuration(), std::move(clockFakePtr)); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 50 | MetricParams params = MetricParams() |
| 51 | .id("id") |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 52 | .operationType(OperationType::avg) |
| 53 | .collectionTimeScope(CollectionTimeScope::point) |
| 54 | .collectionDuration(CollectionDuration(0ms)); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 55 | std::vector<std::shared_ptr<SensorMock>> sensorMocks = makeSensorMocks(1u); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 56 | std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>(); |
| 57 | ClockFake& clockFake = *clockFakePtr; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 58 | std::shared_ptr<Metric> sut; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TEST_F(TestMetric, subscribesForSensorDuringInitialization) |
| 62 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 63 | sut = makeSut(params); |
| 64 | |
| 65 | EXPECT_CALL(*sensorMocks.front(), |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 66 | registerForUpdates(Truly([sut = sut.get()](const auto& a0) { |
| 67 | return a0.lock().get() == sut; |
| 68 | }))); |
| 69 | |
| 70 | sut->initialize(); |
| 71 | } |
| 72 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 73 | TEST_F(TestMetric, unsubscribesForSensorDuringDeinitialization) |
| 74 | { |
| 75 | sut = makeSut(params); |
| 76 | |
| 77 | EXPECT_CALL(*sensorMocks.front(), |
| 78 | unregisterFromUpdates(Truly([sut = sut.get()](const auto& a0) { |
| 79 | return a0.lock().get() == sut; |
| 80 | }))); |
| 81 | |
| 82 | sut->deinitialize(); |
| 83 | } |
| 84 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 85 | TEST_F(TestMetric, containsEmptyReadingAfterCreated) |
| 86 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 87 | sut = makeSut(params); |
| 88 | |
| 89 | ASSERT_THAT(sut->getReadings(), |
| 90 | ElementsAre(MetricValue({"id", "metadata", 0., 0u}))); |
| 91 | } |
| 92 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 93 | class TestMetricAfterInitialization : public TestMetric |
| 94 | { |
| 95 | public: |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 96 | void SetUp() override |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 97 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 98 | sut = makeSut(params); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 99 | sut->initialize(); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 100 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 101 | }; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 102 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 103 | TEST_F(TestMetricAfterInitialization, containsEmptyReading) |
| 104 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 105 | ASSERT_THAT(sut->getReadings(), |
| 106 | ElementsAre(MetricValue({"id", "metadata", 0., 0u}))); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 109 | TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 110 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 111 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 112 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 113 | ASSERT_THAT( |
| 114 | sut->getReadings(), |
| 115 | ElementsAre(MetricValue{"id", "metadata", 31.2, |
| 116 | std::chrono::duration_cast<Milliseconds>( |
| 117 | clockFake.system.timestamp()) |
| 118 | .count()})); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 121 | TEST_F(TestMetricAfterInitialization, |
| 122 | throwsWhenUpdateIsPerformedOnUnknownSensor) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 123 | { |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 124 | auto sensor = std::make_shared<StrictMock<SensorMock>>(); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 125 | EXPECT_THROW(sut->sensorUpdated(*sensor, Milliseconds{10}), |
| 126 | std::out_of_range); |
| 127 | EXPECT_THROW(sut->sensorUpdated(*sensor, Milliseconds{10}, 20.0), |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 128 | std::out_of_range); |
| 129 | } |
| 130 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 131 | TEST_F(TestMetricAfterInitialization, dumpsConfiguration) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 132 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 133 | namespace ts = utils::tstring; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 134 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 135 | ON_CALL(*sensorMocks.front(), id()) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 136 | .WillByDefault(Return(SensorMock::makeId("service1", "path1"))); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 137 | ON_CALL(*sensorMocks.front(), metadata()) |
| 138 | .WillByDefault(Return("metadata1")); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 139 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 140 | const auto conf = sut->dumpConfiguration(); |
| 141 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 142 | LabeledMetricParameters expected = {}; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 143 | expected.at_label<ts::Id>() = params.id(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 144 | expected.at_label<ts::OperationType>() = params.operationType(); |
| 145 | expected.at_label<ts::CollectionTimeScope>() = params.collectionTimeScope(); |
| 146 | expected.at_label<ts::CollectionDuration>() = params.collectionDuration(); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 147 | expected.at_label<ts::SensorPath>() = { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 148 | LabeledSensorInfo("service1", "path1", "metadata1")}; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 149 | |
| 150 | EXPECT_THAT(conf, Eq(expected)); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 151 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 152 | |
| 153 | class TestMetricCalculationFunctions : |
| 154 | public TestMetric, |
| 155 | public WithParamInterface<MetricParams> |
| 156 | { |
| 157 | public: |
| 158 | void SetUp() override |
| 159 | { |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 160 | sut = makeSut(params.operationType(GetParam().operationType()) |
| 161 | .collectionTimeScope(GetParam().collectionTimeScope()) |
| 162 | .collectionDuration(GetParam().collectionDuration())); |
| 163 | } |
| 164 | |
| 165 | static std::vector<std::pair<Milliseconds, double>> defaultReadings() |
| 166 | { |
| 167 | std::vector<std::pair<Milliseconds, double>> ret; |
| 168 | ret.emplace_back(0ms, std::numeric_limits<double>::quiet_NaN()); |
| 169 | ret.emplace_back(10ms, 14.); |
| 170 | ret.emplace_back(1ms, 3.); |
| 171 | ret.emplace_back(5ms, 7.); |
| 172 | return ret; |
| 173 | } |
| 174 | }; |
| 175 | |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame^] | 176 | MetricParams defaultCollectionFunctionParams() |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 177 | { |
| 178 | return MetricParams() |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 179 | .readings(TestMetricCalculationFunctions::defaultReadings()) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 180 | .expectedReading(systemTimestamp + 16ms, 7.0); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 183 | MetricParams defaultPointParams() |
| 184 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame^] | 185 | return defaultCollectionFunctionParams().collectionTimeScope( |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 186 | CollectionTimeScope::point); |
| 187 | } |
| 188 | |
| 189 | INSTANTIATE_TEST_SUITE_P( |
| 190 | TimeScopePointReturnsLastReading, TestMetricCalculationFunctions, |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame^] | 191 | Values(defaultPointParams().operationType(OperationType::min), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 192 | defaultPointParams().operationType(OperationType::max), |
| 193 | defaultPointParams().operationType(OperationType::sum), |
| 194 | defaultPointParams().operationType(OperationType::avg))); |
| 195 | |
| 196 | MetricParams defaultMinParams() |
| 197 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame^] | 198 | return defaultCollectionFunctionParams().operationType(OperationType::min); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | INSTANTIATE_TEST_SUITE_P( |
| 202 | ReturnsMinForGivenTimeScope, TestMetricCalculationFunctions, |
| 203 | Values(defaultMinParams() |
| 204 | .collectionTimeScope(CollectionTimeScope::interval) |
| 205 | .collectionDuration(CollectionDuration(100ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 206 | .expectedReading(systemTimestamp + 16ms, 3.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 207 | defaultMinParams() |
| 208 | .collectionTimeScope(CollectionTimeScope::interval) |
| 209 | .collectionDuration(CollectionDuration(3ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 210 | .expectedReading(systemTimestamp + 16ms, 7.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 211 | defaultMinParams() |
| 212 | .collectionTimeScope(CollectionTimeScope::startup) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 213 | .expectedReading(systemTimestamp + 16ms, 3.0))); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 214 | |
| 215 | MetricParams defaultMaxParams() |
| 216 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame^] | 217 | return defaultCollectionFunctionParams().operationType(OperationType::max); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | INSTANTIATE_TEST_SUITE_P( |
| 221 | ReturnsMaxForGivenTimeScope, TestMetricCalculationFunctions, |
| 222 | Values(defaultMaxParams() |
| 223 | .collectionTimeScope(CollectionTimeScope::interval) |
| 224 | .collectionDuration(CollectionDuration(100ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 225 | .expectedReading(systemTimestamp + 16ms, 14.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 226 | defaultMaxParams() |
| 227 | .collectionTimeScope(CollectionTimeScope::interval) |
| 228 | .collectionDuration(CollectionDuration(6ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 229 | .expectedReading(systemTimestamp + 16ms, 14.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 230 | defaultMaxParams() |
| 231 | .collectionTimeScope(CollectionTimeScope::interval) |
| 232 | .collectionDuration(CollectionDuration(5ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 233 | .expectedReading(systemTimestamp + 16ms, 7.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 234 | defaultMaxParams() |
| 235 | .collectionTimeScope(CollectionTimeScope::startup) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 236 | .expectedReading(systemTimestamp + 16ms, 14.0))); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 237 | |
| 238 | MetricParams defaultSumParams() |
| 239 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame^] | 240 | return defaultCollectionFunctionParams().operationType(OperationType::sum); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | INSTANTIATE_TEST_SUITE_P( |
| 244 | ReturnsSumForGivenTimeScope, TestMetricCalculationFunctions, |
| 245 | Values(defaultSumParams() |
| 246 | .collectionTimeScope(CollectionTimeScope::interval) |
| 247 | .collectionDuration(CollectionDuration(100ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 248 | .expectedReading(systemTimestamp + 16ms, |
| 249 | 14. * 0.01 + 3. * 0.001 + 7. * 0.005), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 250 | defaultSumParams() |
| 251 | .collectionTimeScope(CollectionTimeScope::interval) |
| 252 | .collectionDuration(CollectionDuration(8ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 253 | .expectedReading(systemTimestamp + 16ms, |
| 254 | 14. * 0.002 + 3. * 0.001 + 7 * 0.005), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 255 | defaultSumParams() |
| 256 | .collectionTimeScope(CollectionTimeScope::interval) |
| 257 | .collectionDuration(CollectionDuration(6ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 258 | .expectedReading(systemTimestamp + 16ms, 3. * 0.001 + 7 * 0.005), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 259 | defaultSumParams() |
| 260 | .collectionTimeScope(CollectionTimeScope::startup) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 261 | .expectedReading(systemTimestamp + 16ms, |
| 262 | 14. * 0.01 + 3. * 0.001 + 7 * 0.005))); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 263 | |
| 264 | MetricParams defaultAvgParams() |
| 265 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame^] | 266 | return defaultCollectionFunctionParams().operationType(OperationType::avg); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | INSTANTIATE_TEST_SUITE_P( |
| 270 | ReturnsAvgForGivenTimeScope, TestMetricCalculationFunctions, |
| 271 | Values(defaultAvgParams() |
| 272 | .collectionTimeScope(CollectionTimeScope::interval) |
| 273 | .collectionDuration(CollectionDuration(100ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 274 | .expectedReading(systemTimestamp + 16ms, |
| 275 | (14. * 10 + 3. * 1 + 7 * 5) / 16.), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 276 | defaultAvgParams() |
| 277 | .collectionTimeScope(CollectionTimeScope::interval) |
| 278 | .collectionDuration(CollectionDuration(8ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 279 | .expectedReading(systemTimestamp + 16ms, |
| 280 | (14. * 2 + 3. * 1 + 7 * 5) / 8.), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 281 | defaultAvgParams() |
| 282 | .collectionTimeScope(CollectionTimeScope::interval) |
| 283 | .collectionDuration(CollectionDuration(6ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 284 | .expectedReading(systemTimestamp + 16ms, (3. * 1 + 7 * 5) / 6.), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 285 | defaultAvgParams() |
| 286 | .collectionTimeScope(CollectionTimeScope::startup) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 287 | .expectedReading(systemTimestamp + 16ms, |
| 288 | (14. * 10 + 3. * 1 + 7 * 5) / 16.))); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 289 | |
| 290 | TEST_P(TestMetricCalculationFunctions, calculatesReadingValue) |
| 291 | { |
| 292 | for (auto [timestamp, reading] : GetParam().readings()) |
| 293 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 294 | sut->sensorUpdated(*sensorMocks.front(), clockFake.steadyTimestamp(), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 295 | reading); |
| 296 | clockFake.advance(timestamp); |
| 297 | } |
| 298 | |
| 299 | const auto [expectedTimestamp, expectedReading] = |
| 300 | GetParam().expectedReading(); |
| 301 | const auto readings = sut->getReadings(); |
| 302 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 303 | EXPECT_THAT(readings, |
| 304 | ElementsAre(MetricValue{"id", "metadata", expectedReading, |
| 305 | expectedTimestamp.count()})); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | TEST_P(TestMetricCalculationFunctions, |
| 309 | calculatedReadingValueWithIntermediateCalculations) |
| 310 | { |
| 311 | for (auto [timestamp, reading] : GetParam().readings()) |
| 312 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 313 | sut->sensorUpdated(*sensorMocks.front(), clockFake.steadyTimestamp(), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 314 | reading); |
| 315 | clockFake.advance(timestamp); |
| 316 | sut->getReadings(); |
| 317 | } |
| 318 | |
| 319 | const auto [expectedTimestamp, expectedReading] = |
| 320 | GetParam().expectedReading(); |
| 321 | const auto readings = sut->getReadings(); |
| 322 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 323 | EXPECT_THAT(readings, |
| 324 | ElementsAre(MetricValue{"id", "metadata", expectedReading, |
| 325 | expectedTimestamp.count()})); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 326 | } |