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

Function test

test/async_worker.js:65–264  ·  view source on GitHub ↗
(binding)

Source from the content-addressed store, hash-verified

63}
64
65async function test (binding) {
66 const libUvThreadCount = Number(process.env.UV_THREADPOOL_SIZE || 4);
67 binding.asyncworker.tryCancelQueuedWork(() => {}, 'echoString', libUvThreadCount);
68
69 let taskFailed = false;
70 try {
71 binding.asyncworker.expectCancelToFail(() => {});
72 } catch (e) {
73 taskFailed = true;
74 }
75
76 assert.equal(taskFailed, true, 'We expect task cancellation to fail');
77
78 if (!checkAsyncHooks()) {
79 binding.asyncworker.expectCustomAllocWorkerToDealloc(() => {});
80
81 await new Promise((resolve) => {
82 const obj = { data: 'test data' };
83 binding.asyncworker.doWorkRecv(obj, function (e) {
84 assert.strictEqual(typeof e, 'undefined');
85 assert.strictEqual(typeof this, 'object');
86 assert.strictEqual(this.data, 'test data');
87 resolve();
88 });
89 });
90
91 await new Promise((resolve) => {
92 binding.asyncworker.doWork(true, null, function (e) {
93 assert.strictEqual(typeof e, 'undefined');
94 assert.strictEqual(typeof this, 'object');
95 assert.strictEqual(this.data, 'test data');
96 resolve();
97 }, 'test data');
98 });
99
100 await new Promise((resolve) => {
101 binding.asyncworker.doWork(false, {}, function (e) {
102 assert.ok(e instanceof Error);
103 assert.strictEqual(e.message, 'test error');
104 assert.strictEqual(typeof this, 'object');
105 assert.strictEqual(this.data, 'test data');
106 resolve();
107 }, 'test data');
108 });
109
110 await new Promise((resolve) => {
111 binding.asyncworker.doWorkWithResult(true, {}, function (succeed, succeedString) {
112 assert(arguments.length === 2);
113 assert(succeed);
114 assert(succeedString === 'ok');
115 assert.strictEqual(typeof this, 'object');
116 assert.strictEqual(this.data, 'test data');
117 resolve();
118 }, 'test data');
119 });
120
121 return;
122 }

Callers

nothing calls this directly

Calls 3

NumberClass · 0.85
checkAsyncHooksFunction · 0.70
installAsyncHooksForTestFunction · 0.70

Tested by

no test coverage detected