Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Update wait with a condition #1297

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions test/async_progress_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
SetError("test error");
}
ProgressData data{0};
std::unique_lock<std::mutex> lock(_cvm);

for (int32_t idx = 0; idx < _times; idx++) {
data.progress = idx;
progress.Send(&data, 1);
_cv.wait(lock);

{
std::unique_lock<std::mutex> lk(_cvm);
_cv.wait(lk, [this] { return dataSent; });
dataSent = false;
}
}
}

Expand All @@ -48,7 +53,12 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
Number progress = Number::New(env, data->progress);
_progress.MakeCallback(Receiver().Value(), {progress});
}
_cv.notify_one();

{
std::lock_guard<std::mutex> lk(_cvm);
dataSent = true;
_cv.notify_one();
}
}

private:
Expand All @@ -59,6 +69,8 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
: AsyncProgressWorker(cb, resource_name, resource) {
_progress.Reset(progress, 1);
}

bool dataSent = false;
std::condition_variable _cv;
std::mutex _cvm;
int32_t _times;
Expand Down