From 0328ead58ad657cdefc7907e043787678fb58333 Mon Sep 17 00:00:00 2001 From: JckXia Date: Thu, 16 Mar 2023 18:46:09 -0400 Subject: [PATCH] test: Update wait with a condition --- 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;