Skip to content

Commit

Permalink
test: Add test cased for failed task cancellations (#1214)
Browse files Browse the repository at this point in the history
* test: Add test cased for failed task cancellations
  • Loading branch information
JckXia authored Oct 13, 2022
1 parent 793268c commit 257a52f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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

1 comment on commit 257a52f

@nepalauziamas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.