1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
#include "napi.h"
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
#if (NAPI_VERSION > 3)
using namespace Napi;
namespace {
struct ProgressData {
size_t progress;
};
class TestWorkerWithNoCb : public AsyncProgressWorker<ProgressData> {
public:
static void DoWork(const CallbackInfo& info) {
switch (info.Length()) {
case 1: {
Function cb = info[0].As<Function>();
TestWorkerWithNoCb* worker = new TestWorkerWithNoCb(info.Env(), cb);
worker->Queue();
} break;
case 2: {
std::string resName = info[0].As<String>();
Function cb = info[1].As<Function>();
TestWorkerWithNoCb* worker =
new TestWorkerWithNoCb(info.Env(), resName.c_str(), cb);
worker->Queue();
} break;
case 3: {
std::string resName = info[0].As<String>();
Object resObject = info[1].As<Object>();
Function cb = info[2].As<Function>();
TestWorkerWithNoCb* worker =
new TestWorkerWithNoCb(info.Env(), resName.c_str(), resObject, cb);
worker->Queue();
} break;
default:
break;
}
}
protected:
void Execute(const ExecutionProgress& progress) override {
ProgressData data{1};
progress.Send(&data, 1);
}
void OnProgress(const ProgressData*, size_t /* count */) override {
_cb.Call({});
}
private:
TestWorkerWithNoCb(Napi::Env env, Function cb) : AsyncProgressWorker(env) {
_cb.Reset(cb, 1);
}
TestWorkerWithNoCb(Napi::Env env, const char* resourceName, Function cb)
: AsyncProgressWorker(env, resourceName) {
_cb.Reset(cb, 1);
}
TestWorkerWithNoCb(Napi::Env env,
const char* resourceName,
const Object& resourceObject,
Function cb)
: AsyncProgressWorker(env, resourceName, resourceObject) {
_cb.Reset(cb, 1);
}
FunctionReference _cb;
};
class TestWorkerWithRecv : public AsyncProgressWorker<ProgressData> {
public:
static void DoWork(const CallbackInfo& info) {
switch (info.Length()) {
case 2: {
Object recv = info[0].As<Object>();
Function cb = info[1].As<Function>();
TestWorkerWithRecv* worker = new TestWorkerWithRecv(recv, cb);
worker->Queue();
} break;
case 3: {
Object recv = info[0].As<Object>();
Function cb = info[1].As<Function>();
std::string resName = info[2].As<String>();
TestWorkerWithRecv* worker =
new TestWorkerWithRecv(recv, cb, resName.c_str());
worker->Queue();
} break;
case 4: {
Object recv = info[0].As<Object>();
Function cb = info[1].As<Function>();
std::string resName = info[2].As<String>();
Object resObject = info[3].As<Object>();
TestWorkerWithRecv* worker =
new TestWorkerWithRecv(recv, cb, resName.c_str(), resObject);
worker->Queue();
} break;
default:
break;
}
}
protected:
void Execute(const ExecutionProgress&) override {}
void OnProgress(const ProgressData*, size_t /* count */) override {}
private:
TestWorkerWithRecv(const Object& recv, const Function& cb)
: AsyncProgressWorker(recv, cb) {}
TestWorkerWithRecv(const Object& recv,
const Function& cb,
const char* resourceName)
: AsyncProgressWorker(recv, cb, resourceName) {}
TestWorkerWithRecv(const Object& recv,
const Function& cb,
const char* resourceName,
const Object& resourceObject)
: AsyncProgressWorker(recv, cb, resourceName, resourceObject) {}
};
class TestWorkerWithCb : public AsyncProgressWorker<ProgressData> {
public:
static void DoWork(const CallbackInfo& info) {
switch (info.Length()) {
case 1: {
Function cb = info[0].As<Function>();
TestWorkerWithCb* worker = new TestWorkerWithCb(cb);
worker->Queue();
} break;
case 2: {
Function cb = info[0].As<Function>();
std::string asyncResName = info[1].As<String>();
TestWorkerWithCb* worker =
new TestWorkerWithCb(cb, asyncResName.c_str());
worker->Queue();
} break;
default:
break;
}
}
protected:
void Execute(const ExecutionProgress&) override {}
void OnProgress(const ProgressData*, size_t /* count */) override {}
private:
TestWorkerWithCb(Function cb) : AsyncProgressWorker(cb) {}
TestWorkerWithCb(Function cb, const char* res_name)
: AsyncProgressWorker(cb, res_name) {}
};
class TestWorker : public AsyncProgressWorker<ProgressData> {
public:
static void DoWork(const CallbackInfo& info) {
int32_t times = info[0].As<Number>().Int32Value();
Function cb = info[1].As<Function>();
Function progress = info[2].As<Function>();
TestWorker* worker =
new TestWorker(cb, progress, "TestResource", Object::New(info.Env()));
worker->_times = times;
worker->Queue();
}
protected:
void Execute(const ExecutionProgress& progress) override {
if (_times < 0) {
SetError("test error");
}
ProgressData data{0};
for (int32_t idx = 0; idx < _times; idx++) {
data.progress = idx;
progress.Send(&data, 1);
{
std::unique_lock<std::mutex> lk(_cvm);
_cv.wait(lk, [this] { return dataSent; });
dataSent = false;
}
}
}
void OnProgress(const ProgressData* data, size_t /* count */) override {
Napi::Env env = Env();
if (!_progress.IsEmpty()) {
Number progress = Number::New(env, data->progress);
_progress.MakeCallback(Receiver().Value(), {progress});
}
{
std::lock_guard<std::mutex> lk(_cvm);
dataSent = true;
_cv.notify_one();
}
}
private:
TestWorker(Function cb,
Function progress,
const char* resource_name,
const Object& resource)
: AsyncProgressWorker(cb, resource_name, resource) {
_progress.Reset(progress, 1);
}
bool dataSent = false;
std::condition_variable _cv;
std::mutex _cvm;
int32_t _times;
FunctionReference _progress;
};
class MalignWorker : public AsyncProgressWorker<ProgressData> {
public:
static void DoWork(const CallbackInfo& info) {
Function cb = info[0].As<Function>();
Function progress = info[1].As<Function>();
MalignWorker* worker =
new MalignWorker(cb, progress, "TestResource", Object::New(info.Env()));
worker->Queue();
}
protected:
void Execute(const ExecutionProgress& progress) override {
{
std::unique_lock<std::mutex> lock(_cvm);
// Testing a nullptr send is acceptable.
progress.Send(nullptr, 0);
_cv.wait(lock, [this] { return _test_case_count == 1; });
}
{
std::unique_lock<std::mutex> lock(_cvm);
progress.Signal();
_cv.wait(lock, [this] { return _test_case_count == 2; });
}
// Testing busy looping on send doesn't trigger unexpected empty data
// OnProgress call.
for (size_t i = 0; i < 1000000; i++) {
ProgressData data{0};
progress.Send(&data, 1);
}
}
void OnProgress(const ProgressData* /* data */, size_t count) override {
Napi::Env env = Env();
{
std::lock_guard<std::mutex> lock(_cvm);
_test_case_count++;
}
bool error = false;
Napi::String reason = Napi::String::New(env, "No error");
if (_test_case_count <= 2 && count != 0) {
error = true;
reason =
Napi::String::New(env, "expect 0 count of data on 1st and 2nd call");
}
if (_test_case_count > 2 && count != 1) {
error = true;
reason = Napi::String::New(
env, "expect 1 count of data on non-1st and non-2nd call");
}
_progress.MakeCallback(Receiver().Value(),
{Napi::Boolean::New(env, error), reason});
_cv.notify_one();
}
private:
MalignWorker(Function cb,
Function progress,
const char* resource_name,
const Object& resource)
: AsyncProgressWorker(cb, resource_name, resource) {
_progress.Reset(progress, 1);
}
size_t _test_case_count = 0;
std::condition_variable _cv;
std::mutex _cvm;
FunctionReference _progress;
};
// Calling a Signal after a SendProgress should not clear progress data
class SignalAfterProgressTestWorker : public AsyncProgressWorker<ProgressData> {
public:
static void DoWork(const CallbackInfo& info) {
Function cb = info[0].As<Function>();
Function progress = info[1].As<Function>();
SignalAfterProgressTestWorker* worker = new SignalAfterProgressTestWorker(
cb, progress, "TestResource", Object::New(info.Env()));
worker->Queue();
}
protected:
void Execute(const ExecutionProgress& progress) override {
ProgressData data{0};
progress.Send(&data, 1);
progress.Signal();
}
void OnProgress(const ProgressData* /* data */, size_t count) override {
Napi::Env env = Env();
bool error = false;
Napi::String reason = Napi::String::New(env, "No error");
if (count != 1) {
error = true;
reason = Napi::String::New(env, "expect 1 count of data");
}
_progress.MakeCallback(Receiver().Value(),
{Napi::Boolean::New(env, error), reason});
}
private:
SignalAfterProgressTestWorker(Function cb,
Function progress,
const char* resource_name,
const Object& resource)
: AsyncProgressWorker(cb, resource_name, resource) {
_progress.Reset(progress, 1);
}
FunctionReference _progress;
};
} // namespace
Object InitAsyncProgressWorker(Env env) {
Object exports = Object::New(env);
exports["doWork"] = Function::New(env, TestWorker::DoWork);
exports["doMalignTest"] = Function::New(env, MalignWorker::DoWork);
exports["doSignalAfterProgressTest"] =
Function::New(env, SignalAfterProgressTestWorker::DoWork);
exports["runWorkerNoCb"] = Function::New(env, TestWorkerWithNoCb::DoWork);
exports["runWorkerWithRecv"] = Function::New(env, TestWorkerWithRecv::DoWork);
exports["runWorkerWithCb"] = Function::New(env, TestWorkerWithCb::DoWork);
return exports;
}
#endif
|