@@ -5,6 +5,7 @@ const common = require('../common');
5
5
6
6
module . exports = common . runTest ( test ) ;
7
7
8
+ // Main test body
8
9
async function test ( binding ) {
9
10
const expectedArray = ( function ( arrayLength ) {
10
11
const result = [ ] ;
@@ -14,6 +15,8 @@ async function test (binding) {
14
15
return result ;
15
16
} ) ( binding . threadsafe_function . ARRAY_LENGTH ) ;
16
17
18
+ const expectedDefaultArray = Array . from ( { length : binding . threadsafe_function . ARRAY_LENGTH } , ( _ , i ) => 42 ) ;
19
+
17
20
function testWithJSMarshaller ( {
18
21
threadStarter,
19
22
quitAfter,
@@ -31,7 +34,7 @@ async function test (binding) {
31
34
} ) , ! ! abort ) ;
32
35
}
33
36
} , ! ! abort , ! ! launchSecondary , maxQueueSize ) ;
34
- if ( threadStarter === 'startThreadNonblocking' ) {
37
+ if ( ( threadStarter === 'startThreadNonblocking' || threadStarter === 'startThreadNonblockSingleArg' ) ) {
35
38
// Let's make this thread really busy for a short while to ensure that
36
39
// the queue fills and the thread receives a napi_queue_full.
37
40
const start = Date . now ( ) ;
@@ -40,23 +43,28 @@ async function test (binding) {
40
43
} ) ;
41
44
}
42
45
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
+ }
47
65
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 ) ;
60
68
61
69
// Start the thread in blocking mode, and assert that all values are passed.
62
70
// Quit after it's done.
@@ -124,6 +132,15 @@ async function test (binding) {
124
132
expectedArray
125
133
) ;
126
134
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
+
127
144
// Start the thread in blocking mode, and assert that all values are passed.
128
145
// Quit early, but let the thread finish. Launch a secondary thread to test
129
146
// the reference counter incrementing functionality.
@@ -150,6 +167,16 @@ async function test (binding) {
150
167
expectedArray
151
168
) ;
152
169
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
+
153
180
// Start the thread in blocking mode, and assert that it could not finish.
154
181
// Quit early by aborting.
155
182
assert . strictEqual (
@@ -185,4 +212,14 @@ async function test (binding) {
185
212
} ) ) . indexOf ( 0 ) ,
186
213
- 1
187
214
) ;
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
+ ) ;
188
225
}
0 commit comments