Skip to content

Commit e208aa0

Browse files
cortinicometa-codesync[bot]
authored andcommitted
Fix flaky TaskDispatchThreadTest/RunAsyncWithDelay timing test (#57041)
Summary: Pull Request resolved: #57041 Replace tight `sleep_for` margins with a `std::promise`/`std::future` synchronization pattern. The old test used `sleep_for(50ms)` and `sleep_for(70ms)` to bracket a 100ms delayed task, which failed on loaded CI machines when timers fired early or late (98.7% flakiness score). The new approach uses `sleep_for(20ms)` for the "not yet executed" check (well before the 200ms delay) and `future.wait_for(5s)` for the "completed" check, eliminating timing sensitivity entirely. Changelog: [Internal] Reviewed By: javache Differential Revision: D107232190 fbshipit-source-id: f30e9570e1136fe6f79c87b4aa9ec1d174c91a3a
1 parent f07c7cc commit e208aa0

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

packages/react-native/ReactCxxPlatform/react/threading/tests/TaskDispatchThreadTests.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <react/threading/TaskDispatchThread.h>
1010
#include <atomic>
1111
#include <chrono>
12+
#include <future>
1213
#include <thread>
1314

1415
namespace facebook::react {
@@ -52,11 +53,19 @@ TEST_F(TaskDispatchThreadTest, RunSyncExecutesTask) {
5253
// Test: runAsync with delay
5354
TEST_F(TaskDispatchThreadTest, RunAsyncWithDelay) {
5455
std::atomic<int> counter{0};
55-
dispatcher->runAsync([&] { counter++; }, std::chrono::milliseconds(100));
56-
std::this_thread::sleep_for(std::chrono::milliseconds(50));
57-
EXPECT_EQ(counter.load(), 0); // Not yet executed
58-
std::this_thread::sleep_for(std::chrono::milliseconds(70));
59-
EXPECT_EQ(counter.load(), 1); // Should be executed now
56+
std::promise<void> taskDone;
57+
auto future = taskDone.get_future();
58+
dispatcher->runAsync(
59+
[&] {
60+
counter++;
61+
taskDone.set_value();
62+
},
63+
std::chrono::milliseconds(200));
64+
std::this_thread::sleep_for(std::chrono::milliseconds(20));
65+
EXPECT_EQ(counter.load(), 0); // 20ms << 200ms, not yet executed
66+
ASSERT_EQ(
67+
future.wait_for(std::chrono::seconds(5)), std::future_status::ready);
68+
EXPECT_EQ(counter.load(), 1); // Task completed
6069
}
6170

6271
// Test: Multiple delayed tasks execute in order

0 commit comments

Comments
 (0)