From 49affef4733648d2a4ab1a3b6b1c577d6a7bd08d Mon Sep 17 00:00:00 2001 From: Velmisov Date: Fri, 7 Aug 2020 18:46:20 +0300 Subject: [PATCH] test: refactor threadsafe_function test with async/await --- .../threadsafe_function.js | 176 ++++++++++-------- 1 file changed, 100 insertions(+), 76 deletions(-) diff --git a/test/threadsafe_function/threadsafe_function.js b/test/threadsafe_function/threadsafe_function.js index a3690fcf3..4c4bbf8a3 100644 --- a/test/threadsafe_function/threadsafe_function.js +++ b/test/threadsafe_function/threadsafe_function.js @@ -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++) { @@ -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++; @@ -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, + ); }