From b69f4360ea2e671b3bbbf4e8df70471a3e113b44 Mon Sep 17 00:00:00 2001 From: Jay Zhuang Date: Mon, 7 Feb 2022 08:31:27 -0800 Subject: [PATCH] Fix flaky test EnvPosixTestWithParam.RunMany (#9502) Summary: Thread-pool pops a thread function and then run the function, which may cause thread-pool is empty but the last function is still running. Pull Request resolved: https://github.com/facebook/rocksdb/pull/9502 Test Plan: `gtest-parallel ./env_test --gtest_filter=DefaultEnvWithoutDirectIO/EnvPosixTestWithParam.RunMany/0 -r 10000 -w 1000` Reviewed By: ajkr Differential Revision: D34011184 Pulled By: jay-zhuang fbshipit-source-id: 8c38bef155205bef96fd1c988dcc643a6b2ac270 --- env/env_test.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/env/env_test.cc b/env/env_test.cc index 081fb225d80..8be7b821f1c 100644 --- a/env/env_test.cc +++ b/env/env_test.cc @@ -459,6 +459,14 @@ TEST_P(EnvPosixTestWithParam, RunMany) { env_->Schedule(&CB::Run, &cb2); env_->Schedule(&CB::Run, &cb3); env_->Schedule(&CB::Run, &cb4); + // thread-pool pops a thread function and then run the function, which may + // cause threadpool is empty but the last function is still running. Add a + // dummy function at the end, to make sure the last callback is finished + // before threadpool is empty. + struct DummyCB { + static void Run(void*) {} + }; + env_->Schedule(&DummyCB::Run, nullptr); WaitThreadPoolsEmpty(); ASSERT_EQ(4, last_id.load(std::memory_order_acquire));