@@ -5,6 +5,7 @@ const common = require('../common');
55
66module . exports = common . runTest ( test ) ;
77
8+ // Main test body
89async function test ( binding ) {
910 const expectedArray = ( function ( arrayLength ) {
1011 const result = [ ] ;
@@ -14,6 +15,8 @@ async function test (binding) {
1415 return result ;
1516 } ) ( binding . threadsafe_function . ARRAY_LENGTH ) ;
1617
18+ const expectedDefaultArray = Array . from ( { length : binding . threadsafe_function . ARRAY_LENGTH } , ( _ , i ) => 42 ) ;
19+
1720 function testWithJSMarshaller ( {
1821 threadStarter,
1922 quitAfter,
@@ -31,7 +34,7 @@ async function test (binding) {
3134 } ) , ! ! abort ) ;
3235 }
3336 } , ! ! abort , ! ! launchSecondary , maxQueueSize ) ;
34- if ( threadStarter === 'startThreadNonblocking' ) {
37+ if ( ( threadStarter === 'startThreadNonblocking' || threadStarter === 'startThreadNonblockSingleArg' ) ) {
3538 // Let's make this thread really busy for a short while to ensure that
3639 // the queue fills and the thread receives a napi_queue_full.
3740 const start = Date . now ( ) ;
@@ -40,23 +43,28 @@ async function test (binding) {
4043 } ) ;
4144 }
4245
43- await new Promise ( function testWithoutJSMarshaller ( resolve ) {
44- let callCount = 0 ;
45- binding . threadsafe_function . startThreadNoNative ( function testCallback ( ) {
46- callCount ++ ;
46+ function testWithoutJSMarshallers ( nativeFunction ) {
47+ return new Promise ( ( resolve ) => {
48+ let callCount = 0 ;
49+ nativeFunction ( function testCallback ( ) {
50+ callCount ++ ;
51+
52+ // The default call-into-JS implementation passes no arguments.
53+ assert . strictEqual ( arguments . length , 0 ) ;
54+ if ( callCount === binding . threadsafe_function . ARRAY_LENGTH ) {
55+ setImmediate ( ( ) => {
56+ binding . threadsafe_function . stopThread ( common . mustCall ( ( ) => {
57+ resolve ( ) ;
58+ } ) , false ) ;
59+ } ) ;
60+ }
61+ } , false /* abort */ , false /* launchSecondary */ ,
62+ binding . threadsafe_function . MAX_QUEUE_SIZE ) ;
63+ } ) ;
64+ }
4765
48- // The default call-into-JS implementation passes no arguments.
49- assert . strictEqual ( arguments . length , 0 ) ;
50- if ( callCount === binding . threadsafe_function . ARRAY_LENGTH ) {
51- setImmediate ( ( ) => {
52- binding . threadsafe_function . stopThread ( common . mustCall ( ( ) => {
53- resolve ( ) ;
54- } ) , false ) ;
55- } ) ;
56- }
57- } , false /* abort */ , false /* launchSecondary */ ,
58- binding . threadsafe_function . MAX_QUEUE_SIZE ) ;
59- } ) ;
66+ await testWithoutJSMarshallers ( binding . threadsafe_function . startThreadNoNative ) ;
67+ await testWithoutJSMarshallers ( binding . threadsafe_function . startThreadNonblockingNoNative ) ;
6068
6169 // Start the thread in blocking mode, and assert that all values are passed.
6270 // Quit after it's done.
@@ -124,6 +132,15 @@ async function test (binding) {
124132 expectedArray
125133 ) ;
126134
135+ assert . deepStrictEqual (
136+ await testWithJSMarshaller ( {
137+ threadStarter : 'startThreadNonblockSingleArg' ,
138+ maxQueueSize : binding . threadsafe_function . MAX_QUEUE_SIZE ,
139+ quitAfter : 1
140+ } ) ,
141+ expectedDefaultArray
142+ ) ;
143+
127144 // Start the thread in blocking mode, and assert that all values are passed.
128145 // Quit early, but let the thread finish. Launch a secondary thread to test
129146 // the reference counter incrementing functionality.
@@ -150,6 +167,16 @@ async function test (binding) {
150167 expectedArray
151168 ) ;
152169
170+ assert . deepStrictEqual (
171+ await testWithJSMarshaller ( {
172+ threadStarter : 'startThreadNonblockSingleArg' ,
173+ maxQueueSize : binding . threadsafe_function . MAX_QUEUE_SIZE ,
174+ quitAfter : 1 ,
175+ launchSecondary : true
176+ } ) ,
177+ expectedDefaultArray
178+ ) ;
179+
153180 // Start the thread in blocking mode, and assert that it could not finish.
154181 // Quit early by aborting.
155182 assert . strictEqual (
@@ -185,4 +212,14 @@ async function test (binding) {
185212 } ) ) . indexOf ( 0 ) ,
186213 - 1
187214 ) ;
215+
216+ assert . strictEqual (
217+ ( await testWithJSMarshaller ( {
218+ threadStarter : 'startThreadNonblockSingleArg' ,
219+ quitAfter : 1 ,
220+ maxQueueSize : binding . threadsafe_function . MAX_QUEUE_SIZE ,
221+ abort : true
222+ } ) ) . indexOf ( 0 ) ,
223+ - 1
224+ ) ;
188225}
0 commit comments