test_metric.cpp (3a1c297a36bcd78d33ee45c603cb1b46e4619f49) test_metric.cpp (cff70c14ef8cadb7fffd0cd41e06b972fa240e56)
1#include "fakes/clock_fake.hpp"
2#include "helpers.hpp"
3#include "metric.hpp"
4#include "mocks/metric_listener_mock.hpp"
5#include "mocks/sensor_mock.hpp"
6#include "params/metric_params.hpp"
7#include "utils/conv_container.hpp"
8#include "utils/conversion.hpp"

--- 31 unchanged lines hidden (view full) ---

40 return result;
41 }
42
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),
1#include "fakes/clock_fake.hpp"
2#include "helpers.hpp"
3#include "metric.hpp"
4#include "mocks/metric_listener_mock.hpp"
5#include "mocks/sensor_mock.hpp"
6#include "params/metric_params.hpp"
7#include "utils/conv_container.hpp"
8#include "utils/conversion.hpp"

--- 31 unchanged lines hidden (view full) ---

40 return result;
41 }
42
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),
48 p.operationType(), p.id(), p.collectionTimeScope(),
49 p.collectionDuration(), std::move(clockFakePtr));
48 p.operationType(), p.collectionTimeScope(), p.collectionDuration(),
49 std::move(clockFakePtr));
50 }
51
52 MetricParams params = MetricParams()
50 }
51
52 MetricParams params = MetricParams()
53 .id("id")
54 .operationType(OperationType::avg)
55 .collectionTimeScope(CollectionTimeScope::point)
56 .collectionDuration(CollectionDuration(0ms));
57 std::vector<std::shared_ptr<SensorMock>> sensorMocks = makeSensorMocks(1u);
58 std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>();
59 ClockFake& clockFake = *clockFakePtr;
60 NiceMock<MetricListenerMock> listenerMock;
61 std::shared_ptr<Metric> sut;

--- 74 unchanged lines hidden (view full) ---

136}
137
138TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate)
139{
140 sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2);
141
142 ASSERT_THAT(
143 sut->getUpdatedReadings(),
53 .operationType(OperationType::avg)
54 .collectionTimeScope(CollectionTimeScope::point)
55 .collectionDuration(CollectionDuration(0ms));
56 std::vector<std::shared_ptr<SensorMock>> sensorMocks = makeSensorMocks(1u);
57 std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>();
58 ClockFake& clockFake = *clockFakePtr;
59 NiceMock<MetricListenerMock> listenerMock;
60 std::shared_ptr<Metric> sut;

--- 74 unchanged lines hidden (view full) ---

135}
136
137TEST_F(TestMetricAfterInitialization, updatesMetricValuesOnSensorUpdate)
138{
139 sut->sensorUpdated(*sensorMocks.front(), Milliseconds{18}, 31.2);
140
141 ASSERT_THAT(
142 sut->getUpdatedReadings(),
144 ElementsAre(MetricValue{"id", "metadata0", 31.2,
143 ElementsAre(MetricValue{"metadata0", 31.2,
145 std::chrono::duration_cast<Milliseconds>(
146 clockFake.system.timestamp())
147 .count()}));
148}
149
150TEST_F(TestMetricAfterInitialization,
151 throwsWhenUpdateIsPerformedOnUnknownSensor)
152{

--- 9 unchanged lines hidden (view full) ---

162 ON_CALL(*sensorMocks.front(), id())
163 .WillByDefault(Return(SensorMock::makeId("service1", "path1")));
164 ON_CALL(*sensorMocks.front(), metadata())
165 .WillByDefault(Return("metadata1"));
166
167 const auto conf = sut->dumpConfiguration();
168
169 LabeledMetricParameters expected = {};
144 std::chrono::duration_cast<Milliseconds>(
145 clockFake.system.timestamp())
146 .count()}));
147}
148
149TEST_F(TestMetricAfterInitialization,
150 throwsWhenUpdateIsPerformedOnUnknownSensor)
151{

--- 9 unchanged lines hidden (view full) ---

161 ON_CALL(*sensorMocks.front(), id())
162 .WillByDefault(Return(SensorMock::makeId("service1", "path1")));
163 ON_CALL(*sensorMocks.front(), metadata())
164 .WillByDefault(Return("metadata1"));
165
166 const auto conf = sut->dumpConfiguration();
167
168 LabeledMetricParameters expected = {};
170 expected.at_label<ts::Id>() = params.id();
171 expected.at_label<ts::OperationType>() = params.operationType();
172 expected.at_label<ts::CollectionTimeScope>() = params.collectionTimeScope();
173 expected.at_label<ts::CollectionDuration>() = params.collectionDuration();
174 expected.at_label<ts::SensorPath>() = {
175 LabeledSensorInfo("service1", "path1", "metadata1")};
176
177 EXPECT_THAT(conf, Eq(expected));
178}

--- 173 unchanged lines hidden (view full) ---

352 reading);
353 clockFake.advance(timestamp);
354 }
355
356 const auto [expectedTimestamp,
357 expectedReading] = GetParam().expectedReading();
358 const auto readings = sut->getUpdatedReadings();
359
169 expected.at_label<ts::OperationType>() = params.operationType();
170 expected.at_label<ts::CollectionTimeScope>() = params.collectionTimeScope();
171 expected.at_label<ts::CollectionDuration>() = params.collectionDuration();
172 expected.at_label<ts::SensorPath>() = {
173 LabeledSensorInfo("service1", "path1", "metadata1")};
174
175 EXPECT_THAT(conf, Eq(expected));
176}

--- 173 unchanged lines hidden (view full) ---

350 reading);
351 clockFake.advance(timestamp);
352 }
353
354 const auto [expectedTimestamp,
355 expectedReading] = GetParam().expectedReading();
356 const auto readings = sut->getUpdatedReadings();
357
360 EXPECT_THAT(readings,
361 ElementsAre(MetricValue{"id", "metadata0", expectedReading,
362 expectedTimestamp.count()}));
358 EXPECT_THAT(readings, ElementsAre(MetricValue{"metadata0", expectedReading,
359 expectedTimestamp.count()}));
363}
364
365TEST_P(TestMetricCalculationFunctions,
366 calculatedReadingValueWithIntermediateCalculations)
367{
368 for (auto [timestamp, reading] : GetParam().readings())
369 {
370 sut->sensorUpdated(*sensorMocks.front(), clockFake.steadyTimestamp(),
371 reading);
372 clockFake.advance(timestamp);
373 sut->getUpdatedReadings();
374 }
375
376 const auto [expectedTimestamp,
377 expectedReading] = GetParam().expectedReading();
378 const auto readings = sut->getUpdatedReadings();
379
360}
361
362TEST_P(TestMetricCalculationFunctions,
363 calculatedReadingValueWithIntermediateCalculations)
364{
365 for (auto [timestamp, reading] : GetParam().readings())
366 {
367 sut->sensorUpdated(*sensorMocks.front(), clockFake.steadyTimestamp(),
368 reading);
369 clockFake.advance(timestamp);
370 sut->getUpdatedReadings();
371 }
372
373 const auto [expectedTimestamp,
374 expectedReading] = GetParam().expectedReading();
375 const auto readings = sut->getUpdatedReadings();
376
380 EXPECT_THAT(readings,
381 ElementsAre(MetricValue{"id", "metadata0", expectedReading,
382 expectedTimestamp.count()}));
377 EXPECT_THAT(readings, ElementsAre(MetricValue{"metadata0", expectedReading,
378 expectedTimestamp.count()}));
383}
384
385TEST_P(TestMetricCalculationFunctions, returnsIsTimerRequired)
386{
387 EXPECT_THAT(sut->isTimerRequired(),
388 Eq(GetParam().expectedIsTimerRequired()));
389}
390

--- 15 unchanged lines hidden (view full) ---

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(),
379}
380
381TEST_P(TestMetricCalculationFunctions, returnsIsTimerRequired)
382{
383 EXPECT_THAT(sut->isTimerRequired(),
384 Eq(GetParam().expectedIsTimerRequired()));
385}
386

--- 15 unchanged lines hidden (view full) ---

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(),
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}));
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}));
421}
422
423TEST_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(),
417}
418
419TEST_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(),
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}));
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}));
438}
439
440TEST_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(),
434}
435
436TEST_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(),
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}));
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}));
458}
454}