Skip to content

Commit

Permalink
test: refactor test to use async/await
Browse files Browse the repository at this point in the history
Refactor threadsafe_function test with async/await

PR-URL: #787
Reviewed-By: Kevin Eady <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
Velmisov authored and mhdawson committed Aug 24, 2020
1 parent 518cfdc commit 2bc45bb
Showing 1 changed file with 100 additions and 76 deletions.
176 changes: 100 additions & 76 deletions test/threadsafe_function/threadsafe_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const common = require('../common');

module.exports = test(require(`../build/${buildType}/binding.node`))
.then(() => test(require(`../build/${buildType}/binding_noexcept.node`)));
module.exports = async function() {
await test(require(`../build/${buildType}/binding.node`));
await test(require(`../build/${buildType}/binding_noexcept.node`));
};

function test(binding) {
async function test(binding) {
const expectedArray = (function(arrayLength) {
const result = [];
for (let index = 0; index < arrayLength; index++) {
Expand Down Expand Up @@ -43,7 +45,7 @@ function test(binding) {
});
}

return new Promise(function testWithoutJSMarshaller(resolve) {
await new Promise(function testWithoutJSMarshaller(resolve) {
let callCount = 0;
binding.threadsafe_function.startThreadNoNative(function testCallback() {
callCount++;
Expand All @@ -59,112 +61,134 @@ function test(binding) {
}
}, false /* abort */, false /* launchSecondary */,
binding.threadsafe_function.MAX_QUEUE_SIZE);
})
});

// Start the thread in blocking mode, and assert that all values are passed.
// Quit after it's done.
.then(() => testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}))
.then((result) => assert.deepStrictEqual(result, expectedArray))
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}),
expectedArray,
);

// Start the thread in blocking mode with an infinite queue, and assert that
// all values are passed. Quit after it's done.
.then(() => testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: 0,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}))
.then((result) => assert.deepStrictEqual(result, expectedArray))
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: 0,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}),
expectedArray,
);

// Start the thread in non-blocking mode, and assert that all values are
// passed. Quit after it's done.
.then(() => testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}))
.then((result) => assert.deepStrictEqual(result, expectedArray))
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: binding.threadsafe_function.ARRAY_LENGTH
}),
expectedArray,
);

// Start the thread in blocking mode, and assert that all values are passed.
// Quit early, but let the thread finish.
.then(() => testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: 1
}))
.then((result) => assert.deepStrictEqual(result, expectedArray))
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: 1
}),
expectedArray,
);

// Start the thread in blocking mode with an infinite queue, and assert that
// all values are passed. Quit early, but let the thread finish.
.then(() => testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: 0,
quitAfter: 1
}))
.then((result) => assert.deepStrictEqual(result, expectedArray))
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
maxQueueSize: 0,
quitAfter: 1
}),
expectedArray,
);


// Start the thread in non-blocking mode, and assert that all values are
// passed. Quit early, but let the thread finish.
.then(() => testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: 1
}))
.then((result) => assert.deepStrictEqual(result, expectedArray))
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
quitAfter: 1
}),
expectedArray,
);

// Start the thread in blocking mode, and assert that all values are passed.
// Quit early, but let the thread finish. Launch a secondary thread to test
// the reference counter incrementing functionality.
.then(() => testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
launchSecondary: true
}))
.then((result) => assert.deepStrictEqual(result, expectedArray))
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
launchSecondary: true
}),
expectedArray,
);

// Start the thread in non-blocking mode, and assert that all values are
// passed. Quit early, but let the thread finish. Launch a secondary thread
// to test the reference counter incrementing functionality.
.then(() => testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
launchSecondary: true
}))
.then((result) => assert.deepStrictEqual(result, expectedArray))
assert.deepStrictEqual(
await testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
launchSecondary: true
}),
expectedArray,
);

// Start the thread in blocking mode, and assert that it could not finish.
// Quit early by aborting.
.then(() => testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
abort: true
}))
.then((result) => assert.strictEqual(result.indexOf(0), -1))
assert.strictEqual(
(await testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
abort: true
})).indexOf(0),
-1,
);

// Start the thread in blocking mode with an infinite queue, and assert that
// it could not finish. Quit early by aborting.
.then(() => testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: 0,
abort: true
}))
.then((result) => assert.strictEqual(result.indexOf(0), -1))
assert.strictEqual(
(await testWithJSMarshaller({
threadStarter: 'startThread',
quitAfter: 1,
maxQueueSize: 0,
abort: true
})).indexOf(0),
-1,
);

// Start the thread in non-blocking mode, and assert that it could not finish.
// Quit early and aborting.
.then(() => testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
abort: true
}))
.then((result) => assert.strictEqual(result.indexOf(0), -1))
assert.strictEqual(
(await testWithJSMarshaller({
threadStarter: 'startThreadNonblocking',
quitAfter: 1,
maxQueueSize: binding.threadsafe_function.MAX_QUEUE_SIZE,
abort: true
})).indexOf(0),
-1,
);
}

0 comments on commit 2bc45bb

Please sign in to comment.