MCPcopy Create free account
hub / github.com/nodejs/node-addon-api / MalignWorker

Class MalignWorker

test/async_progress_worker.cc:232–300  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

230};
231
232class MalignWorker : public AsyncProgressWorker<ProgressData> {
233 public:
234 static void DoWork(const CallbackInfo& info) {
235 Function cb = info[0].As<Function>();
236 Function progress = info[1].As<Function>();
237
238 MalignWorker* worker =
239 new MalignWorker(cb, progress, "TestResource", Object::New(info.Env()));
240 worker->Queue();
241 }
242
243 protected:
244 void Execute(const ExecutionProgress& progress) override {
245 {
246 std::unique_lock<std::mutex> lock(_cvm);
247 // Testing a nullptr send is acceptable.
248 progress.Send(nullptr, 0);
249 _cv.wait(lock, [this] { return _test_case_count == 1; });
250 }
251 {
252 std::unique_lock<std::mutex> lock(_cvm);
253 progress.Signal();
254 _cv.wait(lock, [this] { return _test_case_count == 2; });
255 }
256 // Testing busy looping on send doesn't trigger unexpected empty data
257 // OnProgress call.
258 for (size_t i = 0; i < 1000000; i++) {
259 ProgressData data{0};
260 progress.Send(&data, 1);
261 }
262 }
263
264 void OnProgress(const ProgressData* /* data */, size_t count) override {
265 Napi::Env env = Env();
266 {
267 std::lock_guard<std::mutex> lock(_cvm);
268 _test_case_count++;
269 }
270 bool error = false;
271 Napi::String reason = Napi::String::New(env, "No error");
272 if (_test_case_count <= 2 && count != 0) {
273 error = true;
274 reason =
275 Napi::String::New(env, "expect 0 count of data on 1st and 2nd call");
276 }
277 if (_test_case_count > 2 && count != 1) {
278 error = true;
279 reason = Napi::String::New(
280 env, "expect 1 count of data on non-1st and non-2nd call");
281 }
282 _progress.MakeCallback(Receiver().Value(),
283 {Napi::Boolean::New(env, error), reason});
284 _cv.notify_one();
285 }
286
287 private:
288 MalignWorker(Function cb,
289 Function progress,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected