From ebc7858593c11472c3253bea8285ea823e8ca405 Mon Sep 17 00:00:00 2001 From: Jack <32422811+JckXia@users.noreply.github.com> Date: Fri, 17 Mar 2023 10:27:30 -0400 Subject: [PATCH] test: Update wait with a condition (#1297) * Fix flaky test causing failure in pipeline --- test/async_progress_worker.cc | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/test/async_progress_worker.cc b/test/async_progress_worker.cc index 3975bfbf4..7b14f6ca5 100644 --- a/test/async_progress_worker.cc +++ b/test/async_progress_worker.cc @@ -34,11 +34,16 @@ class TestWorker : public AsyncProgressWorker { SetError("test error"); } ProgressData data{0}; - std::unique_lock lock(_cvm); + for (int32_t idx = 0; idx < _times; idx++) { data.progress = idx; progress.Send(&data, 1); - _cv.wait(lock); + + { + std::unique_lock lk(_cvm); + _cv.wait(lk, [this] { return dataSent; }); + dataSent = false; + } } } @@ -48,7 +53,12 @@ class TestWorker : public AsyncProgressWorker { Number progress = Number::New(env, data->progress); _progress.MakeCallback(Receiver().Value(), {progress}); } - _cv.notify_one(); + + { + std::lock_guard lk(_cvm); + dataSent = true; + _cv.notify_one(); + } } private: @@ -59,6 +69,8 @@ class TestWorker : public AsyncProgressWorker { : AsyncProgressWorker(cb, resource_name, resource) { _progress.Reset(progress, 1); } + + bool dataSent = false; std::condition_variable _cv; std::mutex _cvm; int32_t _times;