2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-01 22:45:18 +00:00

[#3270] Make Perfmon addSampleAndClear UT reliable

src/hooks/dhcp/perfmon/tests/monitored_duration_unittests.cc
    TEST(MonitoredDuration, addSampleAndClear) - reworked to remove
    timing sensitivity
This commit is contained in:
Thomas Markwalder
2024-02-27 11:50:13 -05:00
parent c25bc2b72b
commit c3bbf66a14

View File

@@ -334,39 +334,54 @@ TEST(MonitoredDuration, addSampleAndClear) {
EXPECT_FALSE(mond->getCurrentInterval()); EXPECT_FALSE(mond->getCurrentInterval());
EXPECT_FALSE(mond->getPreviousInterval()); EXPECT_FALSE(mond->getPreviousInterval());
// Iterate over a 60ms period, adding a 10ms sample to a duration auto two_ms = milliseconds(2);
// on each pass. Sleep for 10ms in between iterations. bool should_report = false;
DurationDataIntervalPtr original_interval;
DurationDataIntervalPtr current_interval; // Add a sample and verify nothing to report and that it creates a
DurationDataIntervalPtr previous_interval; // current interval.
auto ten_ms = milliseconds(10); ASSERT_NO_THROW(should_report = mond->addSample(two_ms));
bool should_report; EXPECT_FALSE(should_report);
for (int i = 0; i < 6; ++i) { DurationDataIntervalPtr current_interval = mond->getCurrentInterval();
ASSERT_NO_THROW(should_report = mond->addSample(ten_ms));
current_interval = mond->getCurrentInterval();
ASSERT_TRUE(current_interval); ASSERT_TRUE(current_interval);
switch(i) {
case 0:
// First pass, we should only have a current interval, // First pass, we should only have a current interval,
// nothing to report, one occurrence and a total duration of 10ms. // nothing to report, one occurrence and a total duration of 10ms.
original_interval = current_interval;
EXPECT_FALSE(mond->getPreviousInterval()); EXPECT_FALSE(mond->getPreviousInterval());
EXPECT_FALSE(should_report); EXPECT_FALSE(should_report);
EXPECT_EQ(current_interval->getOccurrences(), 1); EXPECT_EQ(current_interval->getOccurrences(), 1);
EXPECT_EQ(current_interval->getTotalDuration(), ten_ms); EXPECT_EQ(current_interval->getTotalDuration(), two_ms);
break;
default: // Save a copy of the current interval pointer.
// On passes that occur during the duration interval, we should DurationDataIntervalPtr original_interval = current_interval;
// still only have a current interval and nothing to report.
// Current interval occurrences and total duration should be increasing. // Add 4 two ms samples during the current interval.
DurationDataIntervalPtr previous_interval;
for (int i = 1; i < 5; ++i) {
// Add a two ms sample, it should return false as its not
// time to report.
ASSERT_NO_THROW(should_report = mond->addSample(two_ms));
EXPECT_FALSE(should_report);
current_interval = mond->getCurrentInterval();
ASSERT_TRUE(current_interval);
// Make sure the current interval hasn't been replaced and we
// have no previous interval.
EXPECT_EQ(current_interval, original_interval); EXPECT_EQ(current_interval, original_interval);
EXPECT_FALSE(mond->getPreviousInterval()); EXPECT_FALSE(mond->getPreviousInterval());
EXPECT_FALSE(should_report); // Verify the sample was added.
EXPECT_EQ(current_interval->getOccurrences(), (i + 1)); EXPECT_EQ(current_interval->getOccurrences(), (i + 1));
EXPECT_EQ(current_interval->getTotalDuration(), (ten_ms * (i + 1))); EXPECT_EQ(current_interval->getTotalDuration(), (two_ms * (i + 1)));
break; }
case 5:
// On the last pass we should have crossed the interval boundary. // Sleep til past the end of interval
usleep(60 * 1000);
// Add another sample.
ASSERT_NO_THROW(should_report = mond->addSample(two_ms));
current_interval = mond->getCurrentInterval();
ASSERT_TRUE(current_interval);
// We should have crossed the interval boundary.
// Previous interval should be equal to the original interval and // Previous interval should be equal to the original interval and
// should_report should be true. The new current interval should // should_report should be true. The new current interval should
// have 1 occurrence and a total of 10ms. // have 1 occurrence and a total of 10ms.
@@ -375,13 +390,7 @@ TEST(MonitoredDuration, addSampleAndClear) {
EXPECT_EQ(previous_interval, original_interval); EXPECT_EQ(previous_interval, original_interval);
EXPECT_TRUE(should_report); EXPECT_TRUE(should_report);
EXPECT_EQ(current_interval->getOccurrences(), 1); EXPECT_EQ(current_interval->getOccurrences(), 1);
EXPECT_EQ(current_interval->getTotalDuration(), ten_ms); EXPECT_EQ(current_interval->getTotalDuration(), two_ms);
break;
}
// Sleep for 10ms.
usleep(10000);
}
// Verify that clear wipes the intervals. // Verify that clear wipes the intervals.
ASSERT_NO_THROW_LOG(mond->clear()); ASSERT_NO_THROW_LOG(mond->clear());