11/* tslint:disable: no-unused-expression */
2+ import * as zmq from "../../src"
3+
24import { assert } from "chai"
3- import { spawn } from "child_process "
5+ import { createProcess } from "./helpers "
46
5- /* This file is in JavaScript instead of TypeScript because most code is
6- being evaluated with toString() and executed in a sub-process. */
77describe ( "context process exit" , function ( ) {
88 describe ( "with default context" , function ( ) {
99 it ( "should occur when sockets are closed" , async function ( ) {
1010 this . slow ( 200 )
11- await ensureExit ( function ( ) {
12- const zmq = require ( "." )
11+ const code = await createProcess ( ( ) => {
1312 const socket1 = new zmq . Dealer
1413 socket1 . close ( )
1514 const socket2 = new zmq . Router
1615 socket2 . close ( )
1716 } )
17+
18+ assert . equal ( code , 0 )
1819 } )
1920
2021 it ( "should occur when sockets are not closed" , async function ( ) {
2122 this . slow ( 200 )
22- await ensureExit ( function ( ) {
23- const zmq = require ( "." )
23+ const code = await createProcess ( ( ) => {
2424 const socket1 = new zmq . Dealer
2525 const socket2 = new zmq . Router
2626 } )
27+
28+ assert . equal ( code , 0 )
2729 } )
2830
2931 it ( "should not occur when sockets are open and polling" , async function ( ) {
3032 this . slow ( 750 )
31- await ensureNoExit ( function ( ) {
32- const zmq = require ( "." )
33+ const code = await createProcess ( ( ) => {
3334 const socket1 = new zmq . Dealer
3435 socket1 . connect ( "inproc://foo" )
3536 socket1 . receive ( )
3637 } )
38+
39+ assert . equal ( code , - 1 )
3740 } )
3841 } )
3942
4043 describe ( "with custom context" , function ( ) {
4144 it ( "should occur when sockets are closed" , async function ( ) {
4245 this . slow ( 200 )
43- await ensureExit ( function ( ) {
44- const zmq = require ( "." )
46+ const code = await createProcess ( ( ) => {
4547 const context = new zmq . Context
4648 const socket1 = new zmq . Dealer ( { context} )
4749 socket1 . close ( )
4850 const socket2 = new zmq . Router ( { context} )
4951 socket2 . close ( )
5052 } )
53+
54+ assert . equal ( code , 0 )
5155 } )
5256
5357 it ( "should occur when sockets are closed and context is gced" , async function ( ) {
5458 this . slow ( 200 )
55- await ensureExit ( function ( ) {
56- const zmq = require ( "." )
59+ const code = await createProcess ( ( ) => {
5760 function run ( ) {
5861 const context = new zmq . Context
5962 const socket1 = new zmq . Dealer ( { context} )
@@ -65,68 +68,31 @@ describe("context process exit", function() {
6568 run ( )
6669 global . gc ( )
6770 } )
71+
72+ assert . equal ( code , 0 )
6873 } )
6974
7075 it ( "should occur when sockets are not closed" , async function ( ) {
7176 this . slow ( 200 )
72- await ensureExit ( function ( ) {
73- const zmq = require ( "." )
77+ const code = await createProcess ( ( ) => {
7478 const context = new zmq . Context
7579 const socket1 = new zmq . Dealer ( { context} )
7680 const socket2 = new zmq . Router ( { context} )
7781 } )
82+
83+ assert . equal ( code , 0 )
7884 } )
7985
8086 it ( "should not occur when sockets are open and polling" , async function ( ) {
8187 this . slow ( 750 )
82- await ensureNoExit ( function ( ) {
83- const zmq = require ( "." )
88+ const code = await createProcess ( ( ) => {
8489 const context = new zmq . Context
8590 const socket1 = new zmq . Dealer ( { context} )
8691 socket1 . connect ( "inproc://foo" )
8792 socket1 . receive ( )
8893 } )
89- } )
90- } )
91- } )
92-
93- async function ensureExit ( fn : ( ) => void ) : Promise < void > {
94- return new Promise ( ( resolve ) => {
95- const child = spawn ( process . argv [ 0 ] , [ "--expose_gc" ] )
96- child . stdin . write ( `(${ fn } )()` )
97- child . stdin . end ( )
98-
99- child . stdout . on ( "data" , ( data : Buffer ) => console . log ( data . toString ( ) ) )
100- child . stderr . on ( "data" , ( data : Buffer ) => console . error ( data . toString ( ) ) )
10194
102- child . on ( "close" , ( code : number ) => {
103- assert . equal ( code , 0 )
104- resolve ( )
95+ assert . equal ( code , - 1 )
10596 } )
106-
107- setTimeout ( ( ) => {
108- resolve ( )
109- child . kill ( )
110- } , 2000 )
11197 } )
112- }
113-
114- async function ensureNoExit ( fn : ( ) => void ) : Promise < void > {
115- return new Promise ( ( resolve , reject ) => {
116- const child = spawn ( process . argv [ 0 ] , [ "--expose_gc" ] )
117- child . stdin . write ( `(${ fn } )()` )
118- child . stdin . end ( )
119-
120- child . stdout . on ( "data" , ( data : Buffer ) => console . log ( data . toString ( ) ) )
121- child . stderr . on ( "data" , ( data : Buffer ) => console . error ( data . toString ( ) ) )
122-
123- child . on ( "close" , ( code : number ) => {
124- reject ( new Error ( `Exit with code ${ code } ` ) )
125- } )
126-
127- setTimeout ( ( ) => {
128- resolve ( )
129- child . kill ( )
130- } , 500 )
131- } )
132- }
98+ } )
0 commit comments