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