Skip to content

Commit

Permalink
remove race from asyncprogressqueueworker
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Jul 27, 2020
1 parent 5ba003c commit 5a26d3e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
2 changes: 0 additions & 2 deletions test/asyncprogressqueueworker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class TestWorker : public AsyncProgressQueueWorker<ProgressData> {
static void CancelWork(const CallbackInfo& info) {
auto wrap = info[0].As<Napi::External<TestWorker>>();
auto worker = wrap.Data();
// We cannot cancel a worker if it got started. So we have to do a quick cancel.
worker->Queue();
worker->Cancel();
}

Expand Down
33 changes: 14 additions & 19 deletions test/asyncprogressqueueworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = test(require(`./build/${buildType}/binding.node`))
async function test({ asyncprogressqueueworker }) {
await success(asyncprogressqueueworker);
await fail(asyncprogressqueueworker);
await cancel(asyncprogressqueueworker);
cancel(asyncprogressqueueworker);
}

function success(binding) {
Expand Down Expand Up @@ -49,22 +49,17 @@ function fail(binding) {
}

function cancel(binding) {
return new Promise((resolve, reject) => {
// make sure the work we are going to cancel will not be
// able to start by using all the threads in the pool.
for (let i = 0; i < os.cpus().length; ++i) {
const worker = binding.createWork(-1, () => {}, () => {});
binding.queueWork(worker);
}
const worker = binding.createWork(-1,
() => {
assert.fail('unexpected callback');
},
() => {
assert.fail('unexpected progress report');
}
);
binding.cancelWork(worker);
resolve();
});
// We cannot cancel work once it gets started, so we have no choice here but
// to create the work and then cancel it without queueing it. Thus we cannot
// reliably test create -> queue -> cancel.
//
// We could queue it and immediately cancel, and we could even attempt to
// saturate the thread pool with work before we do that but that would still
// be racy and, on some platforms and under certain circumstances the work
// might still get queued and therefore be unable to be cancelled, resulting
// in this test being flaky.
const worker = binding.createWork(-1,
() => reject(new Error('unexpected callback')),
() => reject(new Error('unexpected progress report')));
binding.cancelWork(worker);
}

0 comments on commit 5a26d3e

Please sign in to comment.