From: Thomas Markwalder Date: Mon, 30 Jun 2014 14:47:53 +0000 (-0400) Subject: [master] Fixed unit tests for asiolink::IntervalTimer X-Git-Tag: trac3473_base~14 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=04c5bb5be429b54b1afee4eb3582242b8d33e8a4;p=thirdparty%2Fkea.git [master] Fixed unit tests for asiolink::IntervalTimer The unittests IntervalTimerTest.intervalModeTest and IntervalTimerTest.timerReuseTest were failing under NetBSD VM. They have been restructured to be less susceptible to timing. --- diff --git a/src/lib/asiolink/tests/interval_timer_unittest.cc b/src/lib/asiolink/tests/interval_timer_unittest.cc index 234f43c3c0..b47b40382f 100644 --- a/src/lib/asiolink/tests/interval_timer_unittest.cc +++ b/src/lib/asiolink/tests/interval_timer_unittest.cc @@ -304,25 +304,32 @@ TEST_F(IntervalTimerTest, overwriteIntervalTimer) { TEST_F(IntervalTimerTest, intervalModeTest) { // Create a timer to control the duration of the test. IntervalTimer test_timer(io_service_); - test_timer.setup(TimerCallBack(this), 550); + test_timer.setup(TimerCallBack(this), 2000); // Create an timer which automatically reschedules itself. Use the // accumulator callback to increment local counter for it. int repeater_count = 0; IntervalTimer repeater(io_service_); - repeater.setup(TimerCallBackAccumulator(this, repeater_count), 100); + repeater.setup(TimerCallBackAccumulator(this, repeater_count), 10); // Create a one-shot timer. Use the accumulator callback to increment // local counter variable for it. int one_shot_count = 0; IntervalTimer one_shot(io_service_); - one_shot.setup(TimerCallBackAccumulator(this, one_shot_count), 100, + one_shot.setup(TimerCallBackAccumulator(this, one_shot_count), 10, IntervalTimer::ONE_SHOT); - // Run until the test_timer expires. - io_service_.run(); + // As long as service runs at least one event handler, loop until + // we've hit our goals. It won't return zero unless is out of + // work or the the service has been stopped by the test timer. + int cnt = 0; + while (((cnt = io_service_.get_io_service().run_one()) > 0) + && (repeater_count < 5)); + + // If cnt is zero, then something went wrong. + EXPECT_TRUE(cnt > 0); - // Verify the repeating timer repeated and the one-shot did not. + // The loop stopped make sure it was for the right reason. EXPECT_EQ(repeater_count, 5); EXPECT_EQ(one_shot_count, 1); } @@ -331,14 +338,14 @@ TEST_F(IntervalTimerTest, intervalModeTest) { TEST_F(IntervalTimerTest, timerReuseTest) { // Create a timer to control the duration of the test. IntervalTimer test_timer(io_service_); - test_timer.setup(TimerCallBack(this), 550); + test_timer.setup(TimerCallBack(this), 2000); // Create a one-shot timer. Use the accumulator callback to increment // local counter variable for it. int one_shot_count = 0; IntervalTimer one_shot(io_service_); TimerCallBackAccumulator callback(this, one_shot_count); - one_shot.setup(callback, 100, IntervalTimer::ONE_SHOT); + one_shot.setup(callback, 10, IntervalTimer::ONE_SHOT); // Run until a single event handler executes. This should be our // one-shot expiring. @@ -348,7 +355,7 @@ TEST_F(IntervalTimerTest, timerReuseTest) { ASSERT_EQ(one_shot_count, 1); // Setup the one-shot to go again. - one_shot.setup(callback, 100, IntervalTimer::ONE_SHOT); + one_shot.setup(callback, 10, IntervalTimer::ONE_SHOT); // Run until a single event handler executes. This should be our // one-shot expiring. @@ -358,10 +365,17 @@ TEST_F(IntervalTimerTest, timerReuseTest) { ASSERT_EQ(one_shot_count, 2); // Setup the timer to be repeating. - one_shot.setup(callback, 100, IntervalTimer::REPEATING); + one_shot.setup(callback, 10, IntervalTimer::REPEATING); - // Run until the test_timer expires. - io_service_.run(); + // As long as service runs at least one event handler, loop until + // we've hit our goals. It won't return zero unless is out of + // work or the the service has been stopped by the test timer. + int cnt = 0; + while ((cnt = io_service_.get_io_service().run_one()) + && (one_shot_count < 4)); + + // If cnt is zero, then something went wrong. + EXPECT_TRUE(cnt > 0); // Verify the timer repeated. EXPECT_GE(one_shot_count, 4);