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 | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 4 | #include "mocks/metric_listener_mock.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 5 | #include "mocks/sensor_mock.hpp" |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 6 | #include "params/metric_params.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 7 | #include "utils/conv_container.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 8 | #include "utils/conversion.hpp" |
| 9 | #include "utils/tstring.hpp" |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 10 | |
| 11 | #include <gmock/gmock.h> |
| 12 | |
| 13 | using namespace testing; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 14 | using namespace std::chrono_literals; |
| 15 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 16 | namespace tstring = utils::tstring; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 17 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 18 | constexpr Milliseconds systemTimestamp = 42ms; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 19 | |
| 20 | class TestMetric : public Test |
| 21 | { |
| 22 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 23 | TestMetric() |
| 24 | { |
| 25 | clockFake.steady.reset(); |
| 26 | clockFake.system.set(systemTimestamp); |
| 27 | } |
| 28 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 29 | static std::vector<std::shared_ptr<SensorMock>> |
| 30 | makeSensorMocks(size_t amount) |
| 31 | { |
| 32 | std::vector<std::shared_ptr<SensorMock>> result; |
| 33 | for (size_t i = 0; i < amount; ++i) |
| 34 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 35 | auto& metricMock = |
| 36 | result.emplace_back(std::make_shared<NiceMock<SensorMock>>()); |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 37 | ON_CALL(*metricMock, metadata()) |
| 38 | .WillByDefault(Return("metadata" + std::to_string(i))); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 39 | } |
| 40 | return result; |
| 41 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 42 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 43 | std::shared_ptr<Metric> makeSut(const MetricParams& p) |
| 44 | { |
| 45 | return std::make_shared<Metric>( |
| 46 | utils::convContainer<std::shared_ptr<interfaces::Sensor>>( |
| 47 | sensorMocks), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 48 | p.operationType(), p.id(), p.collectionTimeScope(), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 49 | p.collectionDuration(), std::move(clockFakePtr)); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 52 | MetricParams params = MetricParams() |
| 53 | .id("id") |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 54 | .operationType(OperationType::avg) |
| 55 | .collectionTimeScope(CollectionTimeScope::point) |
| 56 | .collectionDuration(CollectionDuration(0ms)); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 57 | std::vector<std::shared_ptr<SensorMock>> sensorMocks = makeSensorMocks(1u); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 58 | std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>(); |
| 59 | ClockFake& clockFake = *clockFakePtr; |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 60 | NiceMock<MetricListenerMock> listenerMock; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 61 | std::shared_ptr<Metric> sut; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | TEST_F(TestMetric, subscribesForSensorDuringInitialization) |
| 65 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 66 | sut = makeSut(params); |
| 67 | |
| 68 | EXPECT_CALL(*sensorMocks.front(), |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 69 | registerForUpdates(Truly([sut = sut.get()](const auto& a0) { |
| 70 | return a0.lock().get() == sut; |
| 71 | }))); |
| 72 | |
| 73 | sut->initialize(); |
| 74 | } |
| 75 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 76 | TEST_F(TestMetric, unsubscribesForSensorDuringDeinitialization) |
| 77 | { |
| 78 | sut = makeSut(params); |
| 79 | |
| 80 | EXPECT_CALL(*sensorMocks.front(), |
| 81 | unregisterFromUpdates(Truly([sut = sut.get()](const auto& a0) { |
| 82 | return a0.lock().get() == sut; |
| 83 | }))); |
| 84 | |
| 85 | sut->deinitialize(); |
| 86 | } |
| 87 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 88 | TEST_F(TestMetric, containsEmptyReadingAfterCreated) |
| 89 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 90 | sut = makeSut(params); |
| 91 | |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 92 | ASSERT_THAT(sut->getUpdatedReadings(), ElementsAre()); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 95 | TEST_F(TestMetric, |
| 96 | notifiesRegisteredListenersOnManualUpdateWhenMetricValueChanges) |
| 97 | { |
| 98 | sut = makeSut(params.collectionTimeScope(CollectionTimeScope::startup)); |
| 99 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2); |
| 100 | sut->registerForUpdates(listenerMock); |
| 101 | |
| 102 | EXPECT_CALL(listenerMock, metricUpdated()).Times(2); |
| 103 | |
| 104 | sut->updateReadings(Milliseconds{50u}); |
| 105 | sut->updateReadings(Milliseconds{100u}); |
| 106 | } |
| 107 | |
| 108 | TEST_F(TestMetric, |
| 109 | doesntNotifyRegisteredListenersOnManualUpdateWhenMetricValueDoesntChange) |
| 110 | { |
| 111 | sut = makeSut(params.collectionTimeScope(CollectionTimeScope::startup) |
| 112 | .operationType(OperationType::max)); |
| 113 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2); |
| 114 | sut->registerForUpdates(listenerMock); |
| 115 | |
| 116 | EXPECT_CALL(listenerMock, metricUpdated()).Times(0); |
| 117 | |
| 118 | sut->updateReadings(Milliseconds{50u}); |
| 119 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{180}, 11.); |
| 120 | sut->updateReadings(Milliseconds{100u}); |
| 121 | } |
| 122 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 123 | class TestMetricAfterInitialization : public TestMetric |
| 124 | { |
| 125 | public: |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 126 | void SetUp() override |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 127 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 128 | sut = makeSut(params); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 129 | sut->initialize(); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 130 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 131 | }; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 132 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 133 | TEST_F(TestMetricAfterInitialization, containsEmptyReading) |
| 134 | { |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 135 | ASSERT_THAT(sut->getUpdatedReadings(), ElementsAre()); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 138 | TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 139 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 140 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 141 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 142 | ASSERT_THAT( |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 143 | sut->getUpdatedReadings(), |
| 144 | ElementsAre(MetricValue{"id", "metadata0", 31.2, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 145 | std::chrono::duration_cast<Milliseconds>( |
| 146 | clockFake.system.timestamp()) |
| 147 | .count()})); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 150 | TEST_F(TestMetricAfterInitialization, |
| 151 | throwsWhenUpdateIsPerformedOnUnknownSensor) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 152 | { |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 153 | auto sensor = std::make_shared<StrictMock<SensorMock>>(); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 154 | EXPECT_THROW(sut->sensorUpdated(*sensor, Milliseconds{10}, 20.0), |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 155 | std::out_of_range); |
| 156 | } |
| 157 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 158 | TEST_F(TestMetricAfterInitialization, dumpsConfiguration) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 159 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 160 | namespace ts = utils::tstring; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 161 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 162 | ON_CALL(*sensorMocks.front(), id()) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 163 | .WillByDefault(Return(SensorMock::makeId("service1", "path1"))); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 164 | ON_CALL(*sensorMocks.front(), metadata()) |
| 165 | .WillByDefault(Return("metadata1")); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 166 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 167 | const auto conf = sut->dumpConfiguration(); |
| 168 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 169 | LabeledMetricParameters expected = {}; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 170 | expected.at_label<ts::Id>() = params.id(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 171 | expected.at_label<ts::OperationType>() = params.operationType(); |
| 172 | expected.at_label<ts::CollectionTimeScope>() = params.collectionTimeScope(); |
| 173 | expected.at_label<ts::CollectionDuration>() = params.collectionDuration(); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 174 | expected.at_label<ts::SensorPath>() = { |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 175 | LabeledSensorInfo("service1", "path1", "metadata1")}; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 176 | |
| 177 | EXPECT_THAT(conf, Eq(expected)); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 178 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 179 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 180 | TEST_F(TestMetricAfterInitialization, notifiesRegisteredListeners) |
| 181 | { |
| 182 | EXPECT_CALL(listenerMock, metricUpdated()); |
| 183 | |
| 184 | sut->registerForUpdates(listenerMock); |
| 185 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2); |
| 186 | } |
| 187 | |
| 188 | TEST_F(TestMetricAfterInitialization, |
| 189 | doesntNotifyRegisteredListenersWhenValueDoesntChange) |
| 190 | { |
| 191 | EXPECT_CALL(listenerMock, metricUpdated()); |
| 192 | |
| 193 | sut->registerForUpdates(listenerMock); |
| 194 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2); |
| 195 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{70}, 31.2); |
| 196 | } |
| 197 | |
| 198 | TEST_F(TestMetricAfterInitialization, doesntNotifyAfterUnRegisterListener) |
| 199 | { |
| 200 | EXPECT_CALL(listenerMock, metricUpdated()).Times(0); |
| 201 | |
| 202 | sut->registerForUpdates(listenerMock); |
| 203 | sut->unregisterFromUpdates(listenerMock); |
| 204 | sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2); |
| 205 | } |
| 206 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 207 | class TestMetricCalculationFunctions : |
| 208 | public TestMetric, |
| 209 | public WithParamInterface<MetricParams> |
| 210 | { |
| 211 | public: |
| 212 | void SetUp() override |
| 213 | { |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 214 | sut = makeSut(params.operationType(GetParam().operationType()) |
| 215 | .collectionTimeScope(GetParam().collectionTimeScope()) |
| 216 | .collectionDuration(GetParam().collectionDuration())); |
| 217 | } |
| 218 | |
| 219 | static std::vector<std::pair<Milliseconds, double>> defaultReadings() |
| 220 | { |
| 221 | std::vector<std::pair<Milliseconds, double>> ret; |
| 222 | ret.emplace_back(0ms, std::numeric_limits<double>::quiet_NaN()); |
| 223 | ret.emplace_back(10ms, 14.); |
| 224 | ret.emplace_back(1ms, 3.); |
| 225 | ret.emplace_back(5ms, 7.); |
| 226 | return ret; |
| 227 | } |
| 228 | }; |
| 229 | |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame] | 230 | MetricParams defaultCollectionFunctionParams() |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 231 | { |
| 232 | return MetricParams() |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 233 | .readings(TestMetricCalculationFunctions::defaultReadings()) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 234 | .expectedReading(systemTimestamp + 16ms, 7.0); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 237 | MetricParams defaultPointParams() |
| 238 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 239 | return defaultCollectionFunctionParams() |
| 240 | .collectionTimeScope(CollectionTimeScope::point) |
| 241 | .expectedIsTimerRequired(false); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | INSTANTIATE_TEST_SUITE_P( |
| 245 | TimeScopePointReturnsLastReading, TestMetricCalculationFunctions, |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame] | 246 | Values(defaultPointParams().operationType(OperationType::min), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 247 | defaultPointParams().operationType(OperationType::max), |
| 248 | defaultPointParams().operationType(OperationType::sum), |
| 249 | defaultPointParams().operationType(OperationType::avg))); |
| 250 | |
| 251 | MetricParams defaultMinParams() |
| 252 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame] | 253 | return defaultCollectionFunctionParams().operationType(OperationType::min); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | INSTANTIATE_TEST_SUITE_P( |
| 257 | ReturnsMinForGivenTimeScope, TestMetricCalculationFunctions, |
| 258 | Values(defaultMinParams() |
| 259 | .collectionTimeScope(CollectionTimeScope::interval) |
| 260 | .collectionDuration(CollectionDuration(100ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 261 | .expectedReading(systemTimestamp + 16ms, 3.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 262 | defaultMinParams() |
| 263 | .collectionTimeScope(CollectionTimeScope::interval) |
| 264 | .collectionDuration(CollectionDuration(3ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 265 | .expectedReading(systemTimestamp + 16ms, 7.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 266 | defaultMinParams() |
| 267 | .collectionTimeScope(CollectionTimeScope::startup) |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 268 | .expectedReading(systemTimestamp + 16ms, 3.0) |
| 269 | .expectedIsTimerRequired(false))); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 270 | |
| 271 | MetricParams defaultMaxParams() |
| 272 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame] | 273 | return defaultCollectionFunctionParams().operationType(OperationType::max); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | INSTANTIATE_TEST_SUITE_P( |
| 277 | ReturnsMaxForGivenTimeScope, TestMetricCalculationFunctions, |
| 278 | Values(defaultMaxParams() |
| 279 | .collectionTimeScope(CollectionTimeScope::interval) |
| 280 | .collectionDuration(CollectionDuration(100ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 281 | .expectedReading(systemTimestamp + 16ms, 14.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 282 | defaultMaxParams() |
| 283 | .collectionTimeScope(CollectionTimeScope::interval) |
| 284 | .collectionDuration(CollectionDuration(6ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 285 | .expectedReading(systemTimestamp + 16ms, 14.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 286 | defaultMaxParams() |
| 287 | .collectionTimeScope(CollectionTimeScope::interval) |
| 288 | .collectionDuration(CollectionDuration(5ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 289 | .expectedReading(systemTimestamp + 16ms, 7.0), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 290 | defaultMaxParams() |
| 291 | .collectionTimeScope(CollectionTimeScope::startup) |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 292 | .expectedReading(systemTimestamp + 16ms, 14.0) |
| 293 | .expectedIsTimerRequired(false))); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 294 | |
| 295 | MetricParams defaultSumParams() |
| 296 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame] | 297 | return defaultCollectionFunctionParams().operationType(OperationType::sum); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | INSTANTIATE_TEST_SUITE_P( |
| 301 | ReturnsSumForGivenTimeScope, TestMetricCalculationFunctions, |
| 302 | Values(defaultSumParams() |
| 303 | .collectionTimeScope(CollectionTimeScope::interval) |
| 304 | .collectionDuration(CollectionDuration(100ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 305 | .expectedReading(systemTimestamp + 16ms, |
| 306 | 14. * 0.01 + 3. * 0.001 + 7. * 0.005), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 307 | defaultSumParams() |
| 308 | .collectionTimeScope(CollectionTimeScope::interval) |
| 309 | .collectionDuration(CollectionDuration(8ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 310 | .expectedReading(systemTimestamp + 16ms, |
| 311 | 14. * 0.002 + 3. * 0.001 + 7 * 0.005), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 312 | defaultSumParams() |
| 313 | .collectionTimeScope(CollectionTimeScope::interval) |
| 314 | .collectionDuration(CollectionDuration(6ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 315 | .expectedReading(systemTimestamp + 16ms, 3. * 0.001 + 7 * 0.005), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 316 | defaultSumParams() |
| 317 | .collectionTimeScope(CollectionTimeScope::startup) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 318 | .expectedReading(systemTimestamp + 16ms, |
| 319 | 14. * 0.01 + 3. * 0.001 + 7 * 0.005))); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 320 | |
| 321 | MetricParams defaultAvgParams() |
| 322 | { |
Krzysztof Grobelny | 60fee07 | 2022-01-13 16:25:04 +0100 | [diff] [blame] | 323 | return defaultCollectionFunctionParams().operationType(OperationType::avg); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | INSTANTIATE_TEST_SUITE_P( |
| 327 | ReturnsAvgForGivenTimeScope, TestMetricCalculationFunctions, |
| 328 | Values(defaultAvgParams() |
| 329 | .collectionTimeScope(CollectionTimeScope::interval) |
| 330 | .collectionDuration(CollectionDuration(100ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 331 | .expectedReading(systemTimestamp + 16ms, |
| 332 | (14. * 10 + 3. * 1 + 7 * 5) / 16.), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 333 | defaultAvgParams() |
| 334 | .collectionTimeScope(CollectionTimeScope::interval) |
| 335 | .collectionDuration(CollectionDuration(8ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 336 | .expectedReading(systemTimestamp + 16ms, |
| 337 | (14. * 2 + 3. * 1 + 7 * 5) / 8.), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 338 | defaultAvgParams() |
| 339 | .collectionTimeScope(CollectionTimeScope::interval) |
| 340 | .collectionDuration(CollectionDuration(6ms)) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 341 | .expectedReading(systemTimestamp + 16ms, (3. * 1 + 7 * 5) / 6.), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 342 | defaultAvgParams() |
| 343 | .collectionTimeScope(CollectionTimeScope::startup) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 344 | .expectedReading(systemTimestamp + 16ms, |
| 345 | (14. * 10 + 3. * 1 + 7 * 5) / 16.))); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 346 | |
| 347 | TEST_P(TestMetricCalculationFunctions, calculatesReadingValue) |
| 348 | { |
| 349 | for (auto [timestamp, reading] : GetParam().readings()) |
| 350 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 351 | sut->sensorUpdated(*sensorMocks.front(), clockFake.steadyTimestamp(), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 352 | reading); |
| 353 | clockFake.advance(timestamp); |
| 354 | } |
| 355 | |
| 356 | const auto [expectedTimestamp, expectedReading] = |
| 357 | GetParam().expectedReading(); |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 358 | const auto readings = sut->getUpdatedReadings(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 359 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 360 | EXPECT_THAT(readings, |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 361 | ElementsAre(MetricValue{"id", "metadata0", expectedReading, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 362 | expectedTimestamp.count()})); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | TEST_P(TestMetricCalculationFunctions, |
| 366 | calculatedReadingValueWithIntermediateCalculations) |
| 367 | { |
| 368 | for (auto [timestamp, reading] : GetParam().readings()) |
| 369 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 370 | sut->sensorUpdated(*sensorMocks.front(), clockFake.steadyTimestamp(), |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 371 | reading); |
| 372 | clockFake.advance(timestamp); |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 373 | sut->getUpdatedReadings(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | const auto [expectedTimestamp, expectedReading] = |
| 377 | GetParam().expectedReading(); |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 378 | const auto readings = sut->getUpdatedReadings(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 379 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 380 | EXPECT_THAT(readings, |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 381 | ElementsAre(MetricValue{"id", "metadata0", expectedReading, |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 382 | expectedTimestamp.count()})); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 383 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 384 | |
| 385 | TEST_P(TestMetricCalculationFunctions, returnsIsTimerRequired) |
| 386 | { |
| 387 | EXPECT_THAT(sut->isTimerRequired(), |
| 388 | Eq(GetParam().expectedIsTimerRequired())); |
| 389 | } |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 390 | |
| 391 | class TestMetricWithMultipleSensors : public TestMetric |
| 392 | { |
| 393 | public: |
| 394 | TestMetricWithMultipleSensors() |
| 395 | { |
| 396 | sensorMocks = makeSensorMocks(7u); |
| 397 | |
| 398 | sut = makeSut(params); |
| 399 | sut->initialize(); |
| 400 | } |
| 401 | }; |
| 402 | |
| 403 | TEST_F(TestMetricWithMultipleSensors, readingsContainsAllReadingsInOrder) |
| 404 | { |
| 405 | for (size_t i = 0; i < sensorMocks.size(); ++i) |
| 406 | { |
| 407 | sut->sensorUpdated(*sensorMocks[i], Milliseconds{i + 100}, i + 10.0); |
| 408 | sut->getUpdatedReadings(); |
| 409 | } |
| 410 | |
| 411 | clockFake.system.set(Milliseconds{72}); |
| 412 | |
| 413 | EXPECT_THAT(sut->getUpdatedReadings(), |
| 414 | ElementsAre(MetricValue{"id", "metadata0", 10.0, 72}, |
| 415 | MetricValue{"id", "metadata1", 11.0, 72}, |
| 416 | MetricValue{"id", "metadata2", 12.0, 72}, |
| 417 | MetricValue{"id", "metadata3", 13.0, 72}, |
| 418 | MetricValue{"id", "metadata4", 14.0, 72}, |
| 419 | MetricValue{"id", "metadata5", 15.0, 72}, |
| 420 | MetricValue{"id", "metadata6", 16.0, 72})); |
| 421 | } |
| 422 | |
| 423 | TEST_F(TestMetricWithMultipleSensors, readingsContainOnlyAvailableSensors) |
| 424 | { |
| 425 | for (auto i : {5u, 3u, 6u, 0u}) |
| 426 | { |
| 427 | sut->sensorUpdated(*sensorMocks[i], Milliseconds{i + 100}, i + 10.0); |
| 428 | sut->getUpdatedReadings(); |
| 429 | } |
| 430 | |
| 431 | clockFake.system.set(Milliseconds{62}); |
| 432 | |
| 433 | EXPECT_THAT(sut->getUpdatedReadings(), |
| 434 | ElementsAre(MetricValue{"id", "metadata5", 15.0, 62}, |
| 435 | MetricValue{"id", "metadata3", 13.0, 62}, |
| 436 | MetricValue{"id", "metadata6", 16.0, 62}, |
| 437 | MetricValue{"id", "metadata0", 10.0, 62})); |
| 438 | } |
| 439 | |
| 440 | TEST_F(TestMetricWithMultipleSensors, readingsContainsAllReadingsOutOfOrder) |
| 441 | { |
| 442 | for (auto i : {6u, 5u, 3u, 4u, 0u, 2u, 1u}) |
| 443 | { |
| 444 | sut->sensorUpdated(*sensorMocks[i], Milliseconds{i + 100}, i + 10.0); |
| 445 | sut->getUpdatedReadings(); |
| 446 | } |
| 447 | |
| 448 | clockFake.system.set(Milliseconds{52}); |
| 449 | |
| 450 | EXPECT_THAT(sut->getUpdatedReadings(), |
| 451 | ElementsAre(MetricValue{"id", "metadata6", 16.0, 52}, |
| 452 | MetricValue{"id", "metadata5", 15.0, 52}, |
| 453 | MetricValue{"id", "metadata3", 13.0, 52}, |
| 454 | MetricValue{"id", "metadata4", 14.0, 52}, |
| 455 | MetricValue{"id", "metadata0", 10.0, 52}, |
| 456 | MetricValue{"id", "metadata2", 12.0, 52}, |
| 457 | MetricValue{"id", "metadata1", 11.0, 52})); |
| 458 | } |