Fix flakiness in TaskRunnerImpl

PiperOrigin-RevId: 549119512
This commit is contained in:
Janusz Sobczak
2023-07-18 14:55:19 -07:00
committed by Copybara-Service
parent eec9c98a95
commit 281032ee78
2 changed files with 19 additions and 36 deletions
+7 -1
View File
@@ -33,7 +33,13 @@ TaskRunnerImpl::TaskRunnerImpl(uint32_t runner_count) {
}
}
TaskRunnerImpl::~TaskRunnerImpl() = default;
TaskRunnerImpl::~TaskRunnerImpl() {
{
absl::MutexLock lock(&mutex_);
timers_map_.clear();
}
executor_->Shutdown();
}
bool TaskRunnerImpl::PostTask(absl::AnyInvocable<void()> task) {
if (task) {
+12 -35
View File
@@ -91,19 +91,19 @@ TEST_P(BaseTaskRunnerImplTest, PostDelayedTask) {
latch.Await();
}
TEST_P(BaseTaskRunnerImplTest, PostTwoDelayedTask) {
TEST_P(BaseTaskRunnerImplTest, PostTwoDelayedTasks) {
TaskRunnerImpl task_runner{GetParam()};
std::atomic_bool first_task_started = false;
CountDownLatch latch(2);
// Run the first task
task_runner.PostDelayedTask(absl::Milliseconds(100), [&]() {
task_runner.PostDelayedTask(absl::Milliseconds(500), [&]() {
first_task_started = true;
latch.CountDown();
});
// Run the second task
task_runner.PostDelayedTask(absl::Milliseconds(50), [&]() {
task_runner.PostDelayedTask(absl::Milliseconds(1), [&]() {
EXPECT_FALSE(first_task_started);
latch.CountDown();
});
@@ -111,42 +111,19 @@ TEST_P(BaseTaskRunnerImplTest, PostTwoDelayedTask) {
latch.Await();
}
TEST_F(BaseTaskRunnerImplTest, PostTasksOnRunnerWithOneThread) {
TaskRunnerImpl task_runner{1};
std::atomic_int count = 0;
absl::Notification notification;
TEST_P(BaseTaskRunnerImplTest, PostMultipleTasks) {
TaskRunnerImpl task_runner(GetParam());
constexpr int kNumTasks = 10;
CountDownLatch latch(kNumTasks);
for (int i = 0; i < 10; i++) {
task_runner.PostTask([&count, &notification]() {
absl::SleepFor(absl::Milliseconds(100));
count++;
if (count == 10) {
notification.Notify();
}
for (int i = 0; i < kNumTasks; i++) {
task_runner.PostTask([&]() {
absl::SleepFor(absl::Milliseconds(10));
latch.CountDown();
});
}
notification.WaitForNotificationWithTimeout(absl::Milliseconds(1900));
EXPECT_EQ(count, 10);
}
TEST_F(BaseTaskRunnerImplTest, PostTasksOnRunnerWithMultipleThreads) {
TaskRunnerImpl task_runner{10};
std::atomic_int count = 0;
absl::Notification notification;
for (int i = 0; i < 10; i++) {
task_runner.PostTask([&count, &notification]() {
absl::SleepFor(absl::Milliseconds(100));
count++;
if (count == 10) {
notification.Notify();
}
});
}
notification.WaitForNotificationWithTimeout(absl::Milliseconds(190));
EXPECT_EQ(count, 10);
EXPECT_TRUE(latch.Await());
}
TEST_P(BaseTaskRunnerImplTest, PostEmptyTask) {