|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const buildType = process.config.target_defaults.default_configuration; |
| 4 | +const assert = require('assert'); |
| 5 | +const common = require('../common'); |
| 6 | + |
| 7 | +test(require(`../build/${buildType}/binding.node`)); |
| 8 | +test(require(`../build/${buildType}/binding_noexcept.node`)); |
| 9 | + |
| 10 | +function test(binding) { |
| 11 | + const expectedArray = (function(arrayLength) { |
| 12 | + const result = []; |
| 13 | + for (let index = 0; index < arrayLength; index++) { |
| 14 | + result.push(arrayLength - 1 - index); |
| 15 | + } |
| 16 | + return result; |
| 17 | + })(binding.threadsafe_function_ex_threadsafe.ARRAY_LENGTH); |
| 18 | + |
| 19 | + function testWithJSMarshaller({ |
| 20 | + threadStarter, |
| 21 | + quitAfter, |
| 22 | + abort, |
| 23 | + maxQueueSize, |
| 24 | + launchSecondary }) { |
| 25 | + return new Promise((resolve) => { |
| 26 | + const array = []; |
| 27 | + binding.threadsafe_function_ex_threadsafe[threadStarter](function testCallback(value) { |
| 28 | + array.push(value); |
| 29 | + if (array.length === quitAfter) { |
| 30 | + setImmediate(() => { |
| 31 | + binding.threadsafe_function_ex_threadsafe.stopThread(common.mustCall(() => { |
| 32 | + resolve(array); |
| 33 | + }), !!abort); |
| 34 | + }); |
| 35 | + } |
| 36 | + }, !!abort, !!launchSecondary, maxQueueSize); |
| 37 | + if (threadStarter === 'startThreadNonblocking') { |
| 38 | + // Let's make this thread really busy for a short while to ensure that |
| 39 | + // the queue fills and the thread receives a napi_queue_full. |
| 40 | + const start = Date.now(); |
| 41 | + while (Date.now() - start < 200); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + new Promise(function testWithoutJSMarshaller(resolve) { |
| 47 | + let callCount = 0; |
| 48 | + binding.threadsafe_function_ex_threadsafe.startThreadNoNative(function testCallback() { |
| 49 | + callCount++; |
| 50 | + |
| 51 | + // The default call-into-JS implementation passes no arguments. |
| 52 | + assert.strictEqual(arguments.length, 0); |
| 53 | + if (callCount === binding.threadsafe_function_ex_threadsafe.ARRAY_LENGTH) { |
| 54 | + setImmediate(() => { |
| 55 | + binding.threadsafe_function_ex_threadsafe.stopThread(common.mustCall(() => { |
| 56 | + resolve(); |
| 57 | + }), false); |
| 58 | + }); |
| 59 | + } |
| 60 | + }, false /* abort */, false /* launchSecondary */, |
| 61 | + binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE); |
| 62 | + }) |
| 63 | + |
| 64 | + // Start the thread in blocking mode, and assert that all values are passed. |
| 65 | + // Quit after it's done. |
| 66 | + .then(() => testWithJSMarshaller({ |
| 67 | + threadStarter: 'startThread', |
| 68 | + maxQueueSize: binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE, |
| 69 | + quitAfter: binding.threadsafe_function_ex_threadsafe.ARRAY_LENGTH |
| 70 | + })) |
| 71 | + .then((result) => assert.deepStrictEqual(result, expectedArray)) |
| 72 | + |
| 73 | + // Start the thread in blocking mode with an infinite queue, and assert that |
| 74 | + // all values are passed. Quit after it's done. |
| 75 | + .then(() => testWithJSMarshaller({ |
| 76 | + threadStarter: 'startThread', |
| 77 | + maxQueueSize: 0, |
| 78 | + quitAfter: binding.threadsafe_function_ex_threadsafe.ARRAY_LENGTH |
| 79 | + })) |
| 80 | + .then((result) => assert.deepStrictEqual(result, expectedArray)) |
| 81 | + |
| 82 | + // Start the thread in non-blocking mode, and assert that all values are |
| 83 | + // passed. Quit after it's done. |
| 84 | + .then(() => testWithJSMarshaller({ |
| 85 | + threadStarter: 'startThreadNonblocking', |
| 86 | + maxQueueSize: binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE, |
| 87 | + quitAfter: binding.threadsafe_function_ex_threadsafe.ARRAY_LENGTH |
| 88 | + })) |
| 89 | + .then((result) => assert.deepStrictEqual(result, expectedArray)) |
| 90 | + |
| 91 | + // Start the thread in blocking mode, and assert that all values are passed. |
| 92 | + // Quit early, but let the thread finish. |
| 93 | + .then(() => testWithJSMarshaller({ |
| 94 | + threadStarter: 'startThread', |
| 95 | + maxQueueSize: binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE, |
| 96 | + quitAfter: 1 |
| 97 | + })) |
| 98 | + .then((result) => assert.deepStrictEqual(result, expectedArray)) |
| 99 | + |
| 100 | + // Start the thread in blocking mode with an infinite queue, and assert that |
| 101 | + // 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)) |
| 108 | + |
| 109 | + |
| 110 | + // Start the thread in non-blocking mode, and assert that all values are |
| 111 | + // passed. Quit early, but let the thread finish. |
| 112 | + .then(() => testWithJSMarshaller({ |
| 113 | + threadStarter: 'startThreadNonblocking', |
| 114 | + maxQueueSize: binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE, |
| 115 | + quitAfter: 1 |
| 116 | + })) |
| 117 | + .then((result) => assert.deepStrictEqual(result, expectedArray)) |
| 118 | + |
| 119 | + // Start the thread in blocking mode, and assert that all values are passed. |
| 120 | + // Quit early, but let the thread finish. Launch a secondary thread to test |
| 121 | + // the reference counter incrementing functionality. |
| 122 | + .then(() => testWithJSMarshaller({ |
| 123 | + threadStarter: 'startThread', |
| 124 | + quitAfter: 1, |
| 125 | + maxQueueSize: binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE, |
| 126 | + launchSecondary: true |
| 127 | + })) |
| 128 | + .then((result) => assert.deepStrictEqual(result, expectedArray)) |
| 129 | + |
| 130 | + // Start the thread in non-blocking mode, and assert that all values are |
| 131 | + // passed. Quit early, but let the thread finish. Launch a secondary thread |
| 132 | + // to test the reference counter incrementing functionality. |
| 133 | + .then(() => testWithJSMarshaller({ |
| 134 | + threadStarter: 'startThreadNonblocking', |
| 135 | + quitAfter: 1, |
| 136 | + maxQueueSize: binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE, |
| 137 | + launchSecondary: true |
| 138 | + })) |
| 139 | + .then((result) => assert.deepStrictEqual(result, expectedArray)) |
| 140 | + |
| 141 | + // Start the thread in blocking mode, and assert that it could not finish. |
| 142 | + // Quit early by aborting. |
| 143 | + .then(() => testWithJSMarshaller({ |
| 144 | + threadStarter: 'startThread', |
| 145 | + quitAfter: 1, |
| 146 | + maxQueueSize: binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE, |
| 147 | + abort: true |
| 148 | + })) |
| 149 | + .then((result) => assert.strictEqual(result.indexOf(0), -1)) |
| 150 | + |
| 151 | + // Start the thread in blocking mode with an infinite queue, and assert that |
| 152 | + // 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)) |
| 160 | + |
| 161 | + // Start the thread in non-blocking mode, and assert that it could not finish. |
| 162 | + // Quit early and aborting. |
| 163 | + .then(() => testWithJSMarshaller({ |
| 164 | + threadStarter: 'startThreadNonblocking', |
| 165 | + quitAfter: 1, |
| 166 | + maxQueueSize: binding.threadsafe_function_ex_threadsafe.MAX_QUEUE_SIZE, |
| 167 | + abort: true |
| 168 | + })) |
| 169 | + .then((result) => assert.strictEqual(result.indexOf(0), -1)) |
| 170 | +} |
0 commit comments