Skip to content

Commit ebc7858

Browse files
authored
test: Update wait with a condition (#1297)
* Fix flaky test causing failure in pipeline
1 parent 0b53d88 commit ebc7858

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

test/async_progress_worker.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
3434
SetError("test error");
3535
}
3636
ProgressData data{0};
37-
std::unique_lock<std::mutex> lock(_cvm);
37+
3838
for (int32_t idx = 0; idx < _times; idx++) {
3939
data.progress = idx;
4040
progress.Send(&data, 1);
41-
_cv.wait(lock);
41+
42+
{
43+
std::unique_lock<std::mutex> lk(_cvm);
44+
_cv.wait(lk, [this] { return dataSent; });
45+
dataSent = false;
46+
}
4247
}
4348
}
4449

@@ -48,7 +53,12 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
4853
Number progress = Number::New(env, data->progress);
4954
_progress.MakeCallback(Receiver().Value(), {progress});
5055
}
51-
_cv.notify_one();
56+
57+
{
58+
std::lock_guard<std::mutex> lk(_cvm);
59+
dataSent = true;
60+
_cv.notify_one();
61+
}
5262
}
5363

5464
private:
@@ -59,6 +69,8 @@ class TestWorker : public AsyncProgressWorker<ProgressData> {
5969
: AsyncProgressWorker(cb, resource_name, resource) {
6070
_progress.Reset(progress, 1);
6171
}
72+
73+
bool dataSent = false;
6274
std::condition_variable _cv;
6375
std::mutex _cvm;
6476
int32_t _times;

0 commit comments

Comments
 (0)