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: Add test cased for failed task cancellations #1214

Merged
merged 2 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
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
57 changes: 57 additions & 0 deletions test/async_worker.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
#include "assert.h"
#include "napi.h"
Expand Down Expand Up @@ -117,6 +119,58 @@ class EchoWorker : public AsyncWorker {
std::string echo;
};

class FailCancelWorker : public AsyncWorker {
private:
bool taskIsRunning = false;
std::mutex mu;
std::condition_variable taskStartingCv;
void NotifyJSThreadTaskHasStarted() {
{
std::lock_guard<std::mutex> lk(mu);
taskIsRunning = true;
taskStartingCv.notify_one();
}
}

public:
FailCancelWorker(Function& cb) : AsyncWorker(cb) {}
~FailCancelWorker() {}

void WaitForWorkerTaskToStart() {
std::unique_lock<std::mutex> lk(mu);
taskStartingCv.wait(lk, [this] { return taskIsRunning; });
taskIsRunning = false;
}

static void DoCancel(const CallbackInfo& info) {
Function cb = info[0].As<Function>();

FailCancelWorker* cancelWorker = new FailCancelWorker(cb);
cancelWorker->Queue();
cancelWorker->WaitForWorkerTaskToStart();

#ifdef NAPI_CPP_EXCEPTIONS
try {
cancelWorker->Cancel();
} catch (Napi::Error& e) {
Napi::Error::New(info.Env(), "Unable to cancel async worker tasks")
.ThrowAsJavaScriptException();
}
#else
cancelWorker->Cancel();
#endif
}

void Execute() override {
NotifyJSThreadTaskHasStarted();
std::this_thread::sleep_for(std::chrono::seconds(1));
}

void OnOK() override {}

void OnError(const Error&) override {}
};

class CancelWorker : public AsyncWorker {
public:
CancelWorker(Function& cb) : AsyncWorker(cb) {}
Expand Down Expand Up @@ -174,5 +228,8 @@ Object InitAsyncWorker(Env env) {
exports["doWorkWithResult"] =
Function::New(env, TestWorkerWithResult::DoWork);
exports["tryCancelQueuedWork"] = Function::New(env, CancelWorker::DoWork);

exports["expectCancelToFail"] =
Function::New(env, FailCancelWorker::DoCancel);
return exports;
}
9 changes: 9 additions & 0 deletions test/async_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ async function test (binding) {
const libUvThreadCount = Number(process.env.UV_THREADPOOL_SIZE || 4);
binding.asyncworker.tryCancelQueuedWork(() => {}, 'echoString', libUvThreadCount);

let taskFailed = false;
try {
binding.asyncworker.expectCancelToFail(() => {});
} catch (e) {
taskFailed = true;
}

assert.equal(taskFailed, true, 'We expect task cancellation to fail');

if (!checkAsyncHooks()) {
await new Promise((resolve) => {
binding.asyncworker.doWork(true, {}, function (e) {
Expand Down