Skip to content

Commit 7201db6

Browse files
committed
test: refactor test to use async/await
Refactor threadsafe_function test with async/await PR-URL: nodejs/node-addon-api#787 Reviewed-By: Kevin Eady <kevin.c.eady@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent b6b4664 commit 7201db6

File tree

1 file changed

+100
-76
lines changed

1 file changed

+100
-76
lines changed

test/threadsafe_function/threadsafe_function.js

Lines changed: 100 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const buildType = process.config.target_defaults.default_configuration;
44
const assert = require('assert');
55
const common = require('../common');
66

7-
module.exports = test(require(`../build/${buildType}/binding.node`))
8-
.then(() => test(require(`../build/${buildType}/binding_noexcept.node`)));
7+
module.exports = async function() {
8+
await test(require(`../build/${buildType}/binding.node`));
9+
await test(require(`../build/${buildType}/binding_noexcept.node`));
10+
};
911

10-
function test(binding) {
12+
async function test(binding) {
1113
const expectedArray = (function(arrayLength) {
1214
const result = [];
1315
for (let index = 0; index < arrayLength; index++) {
@@ -43,7 +45,7 @@ function test(binding) {
4345
});
4446
}
4547

46-
return new Promise(function testWithoutJSMarshaller(resolve) {
48+
await new Promise(function testWithoutJSMarshaller(resolve) {
4749
let callCount = 0;
4850
binding.threadsafe_function.startThreadNoNative(function testCallback() {
4951
callCount++;
@@ -59,112 +61,134 @@ function test(binding) {
5961
}
6062
}, false /* abort */, false /* launchSecondary */,
6163
binding.threadsafe_function.MAX_QUEUE_SIZE);
62-
})
64+
});
6365

6466
// Start the thread in blocking mode, and assert that all values are passed.
6567
// Quit after it's done.
66-
.then(() => testWithJSMarshaller({
67-
threadStarter: 'startThread',
68-
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
69-
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
70-
}))
71-
.then((result) => assert.deepStrictEqual(result, expectedArray))
68+
assert.deepStrictEqual(
69+
await testWithJSMarshaller({
70+
threadStarter: 'startThread',
71+
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
72+
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
73+
}),
74+
expectedArray,
75+
);
7276

7377
// Start the thread in blocking mode with an infinite queue, and assert that
7478
// all values are passed. Quit after it's done.
75-
.then(() => testWithJSMarshaller({
76-
threadStarter: 'startThread',
77-
maxQueueSize: 0,
78-
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
79-
}))
80-
.then((result) => assert.deepStrictEqual(result, expectedArray))
79+
assert.deepStrictEqual(
80+
await testWithJSMarshaller({
81+
threadStarter: 'startThread',
82+
maxQueueSize: 0,
83+
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
84+
}),
85+
expectedArray,
86+
);
8187

8288
// Start the thread in non-blocking mode, and assert that all values are
8389
// passed. Quit after it's done.
84-
.then(() => testWithJSMarshaller({
85-
threadStarter: 'startThreadNonblocking',
86-
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
87-
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
88-
}))
89-
.then((result) => assert.deepStrictEqual(result, expectedArray))
90+
assert.deepStrictEqual(
91+
await testWithJSMarshaller({
92+
threadStarter: 'startThreadNonblocking',
93+
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
94+
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
95+
}),
96+
expectedArray,
97+
);
9098

9199
// Start the thread in blocking mode, and assert that all values are passed.
92100
// Quit early, but let the thread finish.
93-
.then(() => testWithJSMarshaller({
94-
threadStarter: 'startThread',
95-
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
96-
quitAfter: 1
97-
}))
98-
.then((result) => assert.deepStrictEqual(result, expectedArray))
101+
assert.deepStrictEqual(
102+
await testWithJSMarshaller({
103+
threadStarter: 'startThread',
104+
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
105+
quitAfter: 1
106+
}),
107+
expectedArray,
108+
);
99109

100110
// Start the thread in blocking mode with an infinite queue, and assert that
101111
// all values are passed. Quit early, but let the thread finish.
102-
.then(() => testWithJSMarshaller({
103-
threadStarter: 'startThread',
104-
maxQueueSize: 0,
105-
quitAfter: 1
106-
}))
107-
.then((result) => assert.deepStrictEqual(result, expectedArray))
112+
assert.deepStrictEqual(
113+
await testWithJSMarshaller({
114+
threadStarter: 'startThread',
115+
maxQueueSize: 0,
116+
quitAfter: 1
117+
}),
118+
expectedArray,
119+
);
108120

109121

110122
// Start the thread in non-blocking mode, and assert that all values are
111123
// passed. Quit early, but let the thread finish.
112-
.then(() => testWithJSMarshaller({
113-
threadStarter: 'startThreadNonblocking',
114-
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
115-
quitAfter: 1
116-
}))
117-
.then((result) => assert.deepStrictEqual(result, expectedArray))
124+
assert.deepStrictEqual(
125+
await testWithJSMarshaller({
126+
threadStarter: 'startThreadNonblocking',
127+
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
128+
quitAfter: 1
129+
}),
130+
expectedArray,
131+
);
118132

119133
// Start the thread in blocking mode, and assert that all values are passed.
120134
// Quit early, but let the thread finish. Launch a secondary thread to test
121135
// the reference counter incrementing functionality.
122-
.then(() => testWithJSMarshaller({
123-
threadStarter: 'startThread',
124-
quitAfter: 1,
125-
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
126-
launchSecondary: true
127-
}))
128-
.then((result) => assert.deepStrictEqual(result, expectedArray))
136+
assert.deepStrictEqual(
137+
await testWithJSMarshaller({
138+
threadStarter: 'startThread',
139+
quitAfter: 1,
140+
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
141+
launchSecondary: true
142+
}),
143+
expectedArray,
144+
);
129145

130146
// Start the thread in non-blocking mode, and assert that all values are
131147
// passed. Quit early, but let the thread finish. Launch a secondary thread
132148
// to test the reference counter incrementing functionality.
133-
.then(() => testWithJSMarshaller({
134-
threadStarter: 'startThreadNonblocking',
135-
quitAfter: 1,
136-
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
137-
launchSecondary: true
138-
}))
139-
.then((result) => assert.deepStrictEqual(result, expectedArray))
149+
assert.deepStrictEqual(
150+
await testWithJSMarshaller({
151+
threadStarter: 'startThreadNonblocking',
152+
quitAfter: 1,
153+
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
154+
launchSecondary: true
155+
}),
156+
expectedArray,
157+
);
140158

141159
// Start the thread in blocking mode, and assert that it could not finish.
142160
// Quit early by aborting.
143-
.then(() => testWithJSMarshaller({
144-
threadStarter: 'startThread',
145-
quitAfter: 1,
146-
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
147-
abort: true
148-
}))
149-
.then((result) => assert.strictEqual(result.indexOf(0), -1))
161+
assert.strictEqual(
162+
(await testWithJSMarshaller({
163+
threadStarter: 'startThread',
164+
quitAfter: 1,
165+
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
166+
abort: true
167+
})).indexOf(0),
168+
-1,
169+
);
150170

151171
// Start the thread in blocking mode with an infinite queue, and assert that
152172
// it could not finish. Quit early by aborting.
153-
.then(() => testWithJSMarshaller({
154-
threadStarter: 'startThread',
155-
quitAfter: 1,
156-
maxQueueSize: 0,
157-
abort: true
158-
}))
159-
.then((result) => assert.strictEqual(result.indexOf(0), -1))
173+
assert.strictEqual(
174+
(await testWithJSMarshaller({
175+
threadStarter: 'startThread',
176+
quitAfter: 1,
177+
maxQueueSize: 0,
178+
abort: true
179+
})).indexOf(0),
180+
-1,
181+
);
160182

161183
// Start the thread in non-blocking mode, and assert that it could not finish.
162184
// Quit early and aborting.
163-
.then(() => testWithJSMarshaller({
164-
threadStarter: 'startThreadNonblocking',
165-
quitAfter: 1,
166-
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
167-
abort: true
168-
}))
169-
.then((result) => assert.strictEqual(result.indexOf(0), -1))
185+
assert.strictEqual(
186+
(await testWithJSMarshaller({
187+
threadStarter: 'startThreadNonblocking',
188+
quitAfter: 1,
189+
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
190+
abort: true
191+
})).indexOf(0),
192+
-1,
193+
);
170194
}

0 commit comments

Comments
 (0)