Skip to content

Commit

Permalink
test: deflake timer test, not completely (envoyproxy#12642)
Browse files Browse the repository at this point in the history
Signed-off-by: Lizan Zhou <lizan@tetrate.io>
  • Loading branch information
lizan committed Aug 15, 2020
1 parent 2dcf20f commit 22c921a
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions test/common/event/dispatcher_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Envoy {
namespace Event {
namespace {

static void onWatcherReady(evwatch*, const evwatch_prepare_cb_info*, void* arg) {
void onWatcherReady(evwatch*, const evwatch_prepare_cb_info*, void* arg) {
// `arg` contains the ReadyWatcher passed in from evwatch_prepare_new.
auto watcher = static_cast<ReadyWatcher*>(arg);
watcher->ready();
Expand Down Expand Up @@ -480,8 +480,7 @@ class TimerImplTest : public testing::TestWithParam<bool> {
{{"envoy.reloadable_features.activate_timers_next_event_loop",
activateTimersNextEventLoop() ? "true" : "false"}});
// Watch for dispatcher prepare events.
evwatch_prepare_new(&static_cast<DispatcherImpl*>(dispatcher_.get())->base(), onWatcherReady,
&prepare_watcher_);
evwatch_prepare_new(&libevent_base_, onWatcherReady, &prepare_watcher_);
}

bool activateTimersNextEventLoop() { return GetParam(); }
Expand Down Expand Up @@ -553,7 +552,7 @@ TEST_P(TimerImplTest, ChangeTimerBackwardsBeforeRun) {
timer1->enableTimer(std::chrono::milliseconds(4));

// Advance time by 5ms so timers above all trigger in the same loop iteration.
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));

// Expect watcher3 to trigger first because the deadlines for timers 1 and 2 was moved backwards.
InSequence s;
Expand All @@ -576,16 +575,17 @@ TEST_P(TimerImplTest, ChangeTimerForwardsToZeroBeforeRun) {
timer1->enableTimer(std::chrono::milliseconds(0));

// Advance time by 5ms so timers above all trigger in the same loop iteration.
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));

// Expect watcher1 to trigger first because timer1's deadline was moved forward.
InSequence s;
EXPECT_CALL(prepare_watcher_, ready());
if (activateTimersNextEventLoop()) {
// Expect watcher1 to trigger first because timer1's deadline was moved forward.
InSequence s;
EXPECT_CALL(prepare_watcher_, ready());
EXPECT_CALL(watcher1, ready());
EXPECT_CALL(watcher2, ready());
} else {
// Timers execute in the wrong order.
EXPECT_CALL(prepare_watcher_, ready());
EXPECT_CALL(watcher2, ready());
EXPECT_CALL(watcher1, ready());
}
Expand All @@ -604,7 +604,7 @@ TEST_P(TimerImplTest, ChangeTimerForwardsToNonZeroBeforeRun) {
timer1->enableTimer(std::chrono::milliseconds(1));

// Advance time by 5ms so timers above all trigger in the same loop iteration.
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));

// Expect watcher1 to trigger first because timer1's deadline was moved forward.
InSequence s;
Expand Down Expand Up @@ -671,7 +671,7 @@ TEST_P(TimerImplTest, TimerOrdering) {
timer3->enableTimer(std::chrono::milliseconds(2));

// Advance time by 5ms so timers above all trigger in the same loop iteration.
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));

EXPECT_TRUE(timer1->enabled());
EXPECT_TRUE(timer2->enabled());
Expand Down Expand Up @@ -705,7 +705,7 @@ TEST_P(TimerImplTest, TimerOrderAndDisableAlarm) {
timer3->enableTimer(std::chrono::milliseconds(2));

// Advance time by 5ms so timers above all trigger in the same loop iteration.
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));

EXPECT_TRUE(timer1->enabled());
EXPECT_TRUE(timer2->enabled());
Expand Down Expand Up @@ -746,7 +746,7 @@ TEST_P(TimerImplTest, TimerOrderDisableAndReschedule) {
timer4->enableTimer(std::chrono::milliseconds(3));

// Advance time by 5ms so timers above all trigger in the same loop iteration.
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));

EXPECT_TRUE(timer1->enabled());
EXPECT_TRUE(timer2->enabled());
Expand All @@ -764,7 +764,7 @@ TEST_P(TimerImplTest, TimerOrderDisableAndReschedule) {
// that timers 2 and 3 are picked up by the same loop iteration. Without the sleep the two
// timers could execute in different loop iterations.
EXPECT_CALL(prepare_watcher_, ready()).WillOnce(testing::InvokeWithoutArgs([&]() {
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));
}));
EXPECT_CALL(watcher2, ready());
EXPECT_CALL(watcher3, ready());
Expand All @@ -773,7 +773,7 @@ TEST_P(TimerImplTest, TimerOrderDisableAndReschedule) {
EXPECT_CALL(watcher2, ready());
// Sleep in prepare cb to avoid flakiness if epoll_wait returns before the timer timeout.
EXPECT_CALL(prepare_watcher_, ready()).WillOnce(testing::InvokeWithoutArgs([&]() {
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));
}));
EXPECT_CALL(watcher3, ready());
}
Expand Down Expand Up @@ -805,7 +805,7 @@ TEST_P(TimerImplTest, TimerOrderAndReschedule) {
timer4->enableTimer(std::chrono::milliseconds(3));

// Advance time by 5ms so timers above all trigger in the same loop iteration.
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));

EXPECT_TRUE(timer1->enabled());
EXPECT_TRUE(timer2->enabled());
Expand All @@ -824,7 +824,7 @@ TEST_P(TimerImplTest, TimerOrderAndReschedule) {
// that timers 2 and 3 are picked up by the same loop iteration. Without the sleep the two
// timers could execute in different loop iterations.
EXPECT_CALL(prepare_watcher_, ready()).WillOnce(testing::InvokeWithoutArgs([&]() {
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));
}));
EXPECT_CALL(watcher2, ready());
EXPECT_CALL(watcher3, ready());
Expand All @@ -833,7 +833,7 @@ TEST_P(TimerImplTest, TimerOrderAndReschedule) {
EXPECT_CALL(watcher4, ready());
// Sleep in prepare cb to avoid flakiness if epoll_wait returns before the timer timeout.
EXPECT_CALL(prepare_watcher_, ready()).WillOnce(testing::InvokeWithoutArgs([&]() {
advanceLibeventTime(absl::Milliseconds(5));
advanceLibeventTime(absl::Milliseconds(10));
}));
EXPECT_CALL(watcher3, ready());
}
Expand Down

0 comments on commit 22c921a

Please sign in to comment.