Skip to content

Commit

Permalink
test: Update wait with a condition (#1297)
Browse files Browse the repository at this point in the history
* Fix flaky test causing failure in pipeline
  • Loading branch information
JckXia committed Mar 17, 2023
1 parent 0b53d88 commit ebc7858
Showing 1 changed file with 15 additions and 3 deletions.
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

0 comments on commit ebc7858

Please sign in to comment.