From 218bca7ff4718bd17b94c910e8a390b5ce4bf2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Cervi=C3=B1o?= Date: Tue, 12 Feb 2019 13:14:51 +0100 Subject: [PATCH] Fix exceptions due to too high marker rates (#1359) --- erizo/src/erizo/stats/StatNode.cpp | 4 ++++ erizo/src/test/stats/MovingIntervalRateStatTest.cpp | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/erizo/src/erizo/stats/StatNode.cpp b/erizo/src/erizo/stats/StatNode.cpp index 9152b85655..00b58b3eb1 100644 --- a/erizo/src/erizo/stats/StatNode.cpp +++ b/erizo/src/erizo/stats/StatNode.cpp @@ -211,6 +211,10 @@ uint64_t MovingIntervalRateStat::calculateRateForInterval(uint64_t interval_to_c last_value_part_in_interval = 0; added_intervals++; } + // Didn't pass enough time to know the rate + if (now_ms == interval_start_time) { + return 0; + } double rate = static_cast (total_sum) / (now_ms - interval_start_time); return (rate * 1000 * scale_); } diff --git a/erizo/src/test/stats/MovingIntervalRateStatTest.cpp b/erizo/src/test/stats/MovingIntervalRateStatTest.cpp index 9b44230fb3..d165d39fd6 100644 --- a/erizo/src/test/stats/MovingIntervalRateStatTest.cpp +++ b/erizo/src/test/stats/MovingIntervalRateStatTest.cpp @@ -35,6 +35,9 @@ class MovingIntervalRateStatTest : public ::testing::Test { void advanceClockMs(int time_ms) { clock->advanceTime(std::chrono::milliseconds(time_ms)); } + void advanceClockUs(int time_us) { + clock->advanceTime(std::chrono::microseconds(time_us)); + } virtual void SetUp() { } @@ -53,6 +56,11 @@ TEST_F(MovingIntervalRateStatTest, shouldCalculateAverageForLessThanWindowSize) EXPECT_EQ(moving_interval_stat.value(), uint((100 + 110 + 120)/3)); } +TEST_F(MovingIntervalRateStatTest, shouldReturnZeroIfNotEnoughTimePassed) { + moving_interval_stat+=1; + advanceClockUs(1); + EXPECT_EQ(moving_interval_stat.value(), 0u); +} TEST_F(MovingIntervalRateStatTest, shouldReturnAverageOfWindowSize) { const int kTotalSamples = 10;