@@ -23,7 +23,7 @@ if (process.argv[2] === 'child') {
2323 if ( callCount === 2 ) {
2424 binding . Unref ( ) ;
2525 }
26- } , false /* abort */ , true /* launchSecondary */ ) ;
26+ } , false /* abort */ , true /* launchSecondary */ , + process . argv [ 3 ] ) ;
2727
2828 // Release the thread-safe function from the main thread so that it may be
2929 // torn down via the environment cleanup handler.
@@ -35,6 +35,7 @@ function testWithJSMarshaller({
3535 threadStarter,
3636 quitAfter,
3737 abort,
38+ maxQueueSize,
3839 launchSecondary } ) {
3940 return new Promise ( ( resolve ) => {
4041 const array = [ ] ;
@@ -47,7 +48,7 @@ function testWithJSMarshaller({
4748 } ) , ! ! abort ) ;
4849 } ) ;
4950 }
50- } , ! ! abort , ! ! launchSecondary ) ;
51+ } , ! ! abort , ! ! launchSecondary , maxQueueSize ) ;
5152 if ( threadStarter === 'StartThreadNonblocking' ) {
5253 // Let's make this thread really busy for a short while to ensure that
5354 // the queue fills and the thread receives a napi_queue_full.
@@ -57,6 +58,24 @@ function testWithJSMarshaller({
5758 } ) ;
5859}
5960
61+ function testUnref ( queueSize ) {
62+ return new Promise ( ( resolve , reject ) => {
63+ let output = '' ;
64+ const child = fork ( __filename , [ 'child' , queueSize ] , {
65+ stdio : [ process . stdin , 'pipe' , process . stderr , 'ipc' ]
66+ } ) ;
67+ child . on ( 'close' , ( code ) => {
68+ if ( code === 0 ) {
69+ resolve ( output . match ( / \S + / g) ) ;
70+ } else {
71+ reject ( new Error ( 'Child process died with code ' + code ) ) ;
72+ }
73+ } ) ;
74+ child . stdout . on ( 'data' , ( data ) => ( output += data . toString ( ) ) ) ;
75+ } )
76+ . then ( ( result ) => assert . strictEqual ( result . indexOf ( 0 ) , - 1 ) ) ;
77+ }
78+
6079new Promise ( function testWithoutJSMarshaller ( resolve ) {
6180 let callCount = 0 ;
6281 binding . StartThreadNoNative ( function testCallback ( ) {
@@ -71,13 +90,23 @@ new Promise(function testWithoutJSMarshaller(resolve) {
7190 } ) , false ) ;
7291 } ) ;
7392 }
74- } , false /* abort */ , false /* launchSecondary */ ) ;
93+ } , false /* abort */ , false /* launchSecondary */ , binding . MAX_QUEUE_SIZE ) ;
7594} )
7695
7796// Start the thread in blocking mode, and assert that all values are passed.
7897// Quit after it's done.
7998. then ( ( ) => testWithJSMarshaller ( {
8099 threadStarter : 'StartThread' ,
100+ maxQueueSize : binding . MAX_QUEUE_SIZE ,
101+ quitAfter : binding . ARRAY_LENGTH
102+ } ) )
103+ . then ( ( result ) => assert . deepStrictEqual ( result , expectedArray ) )
104+
105+ // Start the thread in blocking mode with an infinite queue, and assert that all
106+ // values are passed. Quit after it's done.
107+ . then ( ( ) => testWithJSMarshaller ( {
108+ threadStarter : 'StartThread' ,
109+ maxQueueSize : 0 ,
81110 quitAfter : binding . ARRAY_LENGTH
82111} ) )
83112. then ( ( result ) => assert . deepStrictEqual ( result , expectedArray ) )
@@ -86,6 +115,7 @@ new Promise(function testWithoutJSMarshaller(resolve) {
86115// Quit after it's done.
87116. then ( ( ) => testWithJSMarshaller ( {
88117 threadStarter : 'StartThreadNonblocking' ,
118+ maxQueueSize : binding . MAX_QUEUE_SIZE ,
89119 quitAfter : binding . ARRAY_LENGTH
90120} ) )
91121. then ( ( result ) => assert . deepStrictEqual ( result , expectedArray ) )
@@ -94,6 +124,16 @@ new Promise(function testWithoutJSMarshaller(resolve) {
94124// Quit early, but let the thread finish.
95125. then ( ( ) => testWithJSMarshaller ( {
96126 threadStarter : 'StartThread' ,
127+ maxQueueSize : binding . MAX_QUEUE_SIZE ,
128+ quitAfter : 1
129+ } ) )
130+ . then ( ( result ) => assert . deepStrictEqual ( result , expectedArray ) )
131+
132+ // Start the thread in blocking mode with an infinite queue, and assert that all
133+ // values are passed. Quit early, but let the thread finish.
134+ . then ( ( ) => testWithJSMarshaller ( {
135+ threadStarter : 'StartThread' ,
136+ maxQueueSize : 0 ,
97137 quitAfter : 1
98138} ) )
99139. then ( ( result ) => assert . deepStrictEqual ( result , expectedArray ) )
@@ -102,6 +142,7 @@ new Promise(function testWithoutJSMarshaller(resolve) {
102142// Quit early, but let the thread finish.
103143. then ( ( ) => testWithJSMarshaller ( {
104144 threadStarter : 'StartThreadNonblocking' ,
145+ maxQueueSize : binding . MAX_QUEUE_SIZE ,
105146 quitAfter : 1
106147} ) )
107148. then ( ( result ) => assert . deepStrictEqual ( result , expectedArray ) )
@@ -112,6 +153,7 @@ new Promise(function testWithoutJSMarshaller(resolve) {
112153. then ( ( ) => testWithJSMarshaller ( {
113154 threadStarter : 'StartThread' ,
114155 quitAfter : 1 ,
156+ maxQueueSize : binding . MAX_QUEUE_SIZE ,
115157 launchSecondary : true
116158} ) )
117159. then ( ( result ) => assert . deepStrictEqual ( result , expectedArray ) )
@@ -122,15 +164,27 @@ new Promise(function testWithoutJSMarshaller(resolve) {
122164. then ( ( ) => testWithJSMarshaller ( {
123165 threadStarter : 'StartThreadNonblocking' ,
124166 quitAfter : 1 ,
167+ maxQueueSize : binding . MAX_QUEUE_SIZE ,
125168 launchSecondary : true
126169} ) )
127170. then ( ( result ) => assert . deepStrictEqual ( result , expectedArray ) )
128171
129172// Start the thread in blocking mode, and assert that it could not finish.
130- // Quit early and aborting.
173+ // Quit early by aborting.
174+ . then ( ( ) => testWithJSMarshaller ( {
175+ threadStarter : 'StartThread' ,
176+ quitAfter : 1 ,
177+ maxQueueSize : binding . MAX_QUEUE_SIZE ,
178+ abort : true
179+ } ) )
180+ . then ( ( result ) => assert . strictEqual ( result . indexOf ( 0 ) , - 1 ) )
181+
182+ // Start the thread in blocking mode with an infinite queue, and assert that it
183+ // could not finish. Quit early by aborting.
131184. then ( ( ) => testWithJSMarshaller ( {
132185 threadStarter : 'StartThread' ,
133186 quitAfter : 1 ,
187+ maxQueueSize : 0 ,
134188 abort : true
135189} ) )
136190. then ( ( result ) => assert . strictEqual ( result . indexOf ( 0 ) , - 1 ) )
@@ -140,25 +194,13 @@ new Promise(function testWithoutJSMarshaller(resolve) {
140194. then ( ( ) => testWithJSMarshaller ( {
141195 threadStarter : 'StartThreadNonblocking' ,
142196 quitAfter : 1 ,
197+ maxQueueSize : binding . MAX_QUEUE_SIZE ,
143198 abort : true
144199} ) )
145200. then ( ( result ) => assert . strictEqual ( result . indexOf ( 0 ) , - 1 ) )
146201
147202// Start a child process to test rapid teardown
148- . then ( ( ) => {
149- return new Promise ( ( resolve , reject ) => {
150- let output = '' ;
151- const child = fork ( __filename , [ 'child' ] , {
152- stdio : [ process . stdin , 'pipe' , process . stderr , 'ipc' ]
153- } ) ;
154- child . on ( 'close' , ( code ) => {
155- if ( code === 0 ) {
156- resolve ( output . match ( / \S + / g) ) ;
157- } else {
158- reject ( new Error ( 'Child process died with code ' + code ) ) ;
159- }
160- } ) ;
161- child . stdout . on ( 'data' , ( data ) => ( output += data . toString ( ) ) ) ;
162- } ) ;
163- } )
164- . then ( ( result ) => assert . strictEqual ( result . indexOf ( 0 ) , - 1 ) ) ;
203+ . then ( ( ) => testUnref ( binding . MAX_QUEUE_SIZE ) )
204+
205+ // Start a child process with an infinite queue to test rapid teardown
206+ . then ( ( ) => testUnref ( 0 ) ) ;
0 commit comments