aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorHansong Zhang <hsz@google.com>2018-10-11 14:14:31 -0700
committerHansong Zhang <hsz@google.com>2018-10-11 14:16:51 -0700
commit466b51280b2c617d05a624eaee2091c7357e1b01 (patch)
tree7183c43381f6d66b405879c65b7f433069b97769 /common
parent311738f7ec386b71eba17650d7bbf8edbd46ca71 (diff)
downloadbt-466b51280b2c617d05a624eaee2091c7357e1b01.tar.gz
Timer: Fix unstable test
* Don't use future.wait_until() * For cancel_period_task, verify the counter won't change after timer is cancelled Test: run Timer test for 5000 times Bug: 116081383 Change-Id: Ibc99507e76180a878f417a0b4e7aba1b8adc7cb6
Diffstat (limited to 'common')
-rw-r--r--common/timer_unittest.cc18
1 files changed, 13 insertions, 5 deletions
diff --git a/common/timer_unittest.cc b/common/timer_unittest.cc
index 7d2bc978a..1c676cc0b 100644
--- a/common/timer_unittest.cc
+++ b/common/timer_unittest.cc
@@ -250,14 +250,22 @@ TEST_F(TimerTest, cancel_periodic_task) {
MessageLoopThread message_loop_thread(name);
message_loop_thread.StartUp();
uint32_t delay_ms = 5;
- uint32_t time_cancellation_ms = 3;
+ int num_tasks = 5;
+ auto future = promise_->get_future();
+
timer_->SchedulePeriodic(
message_loop_thread.GetWeakPtr(), FROM_HERE,
- base::Bind(&TimerTest::ShouldNotHappen, base::Unretained(this)),
+ base::Bind(&TimerTest::IncreaseTaskCounter, base::Unretained(this),
+ num_tasks, promise_),
base::TimeDelta::FromMilliseconds(delay_ms));
- std::this_thread::sleep_for(std::chrono::milliseconds(time_cancellation_ms));
+ future.wait();
timer_->CancelAndWait();
- std::this_thread::sleep_for(std::chrono::milliseconds(delay_error_ms));
+ std::this_thread::sleep_for(
+ std::chrono::milliseconds(delay_ms + delay_error_ms));
+ int counter = counter_;
+ std::this_thread::sleep_for(
+ std::chrono::milliseconds(delay_ms + delay_error_ms));
+ ASSERT_EQ(counter, counter_);
}
// Verify that if a task is being executed, then cancelling it is no-op
@@ -388,6 +396,6 @@ TEST_F(TimerTest, reschedule_task_when_firing_must_schedule_new_task) {
base::Bind(&TimerTest::GetName, base::Unretained(this),
&my_name, promise_),
base::TimeDelta::FromMilliseconds(delay_ms));
- future.wait_for(std::chrono::milliseconds(delay_ms + delay_error_ms));
+ future.get();
ASSERT_EQ(name, my_name);
}