mirror of
https://github.com/Evolution-X/hardware_interfaces
synced 2026-02-02 06:22:53 +00:00
Merge "Deflake WorkerThreadTest" into sc-dev am: 47264c7a2c
Original change: https://googleplex-android-review.googlesource.com/c/platform/hardware/interfaces/+/14926121 Change-Id: I4d3bff18e2e151cde75c8ff213fb8116cfb549a3
This commit is contained in:
@@ -73,24 +73,29 @@ TEST(WorkerThreadTest, TasksExecuteInOrder) {
|
||||
constexpr int NUM_TASKS = 10000;
|
||||
WorkerThread worker(NUM_TASKS);
|
||||
|
||||
std::mutex mut;
|
||||
std::condition_variable cv;
|
||||
bool finished = false;
|
||||
std::vector<int> results;
|
||||
|
||||
for (int i = 0; i < NUM_TASKS; ++i) {
|
||||
worker.schedule(Callable::from([&results, i] {
|
||||
worker.schedule(Callable::from([&mut, &results, i] {
|
||||
// Delay tasks differently to provoke races.
|
||||
std::this_thread::sleep_for(std::chrono::nanoseconds(100 - i % 100));
|
||||
// Unguarded write to results to provoke races.
|
||||
auto lock = std::lock_guard(mut);
|
||||
results.push_back(i);
|
||||
}));
|
||||
}
|
||||
|
||||
std::promise<void> promise;
|
||||
auto future = promise.get_future();
|
||||
|
||||
// Schedule a special task to signal when all of the tasks are finished.
|
||||
worker.schedule(
|
||||
Callable::from([promise = std::move(promise)]() mutable { promise.set_value(); }));
|
||||
future.wait();
|
||||
worker.schedule(Callable::from([&mut, &cv, &finished] {
|
||||
auto lock = std::lock_guard(mut);
|
||||
finished = true;
|
||||
cv.notify_one();
|
||||
}));
|
||||
|
||||
auto lock = std::unique_lock(mut);
|
||||
cv.wait(lock, [&finished] { return finished; });
|
||||
ASSERT_EQ(results.size(), NUM_TASKS);
|
||||
EXPECT_TRUE(std::is_sorted(results.begin(), results.end()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user