import * as path from "path" ; import * as semver from "semver" ; import { spawn } from "child_process" ; import * as zmq from "../../src" ; console . log ( `ZeroMQ version ${ zmq . version }` ) ; if ( semver . satisfies ( zmq . version , ">= 4.2" ) ) { zmq . context . blocky = false ; } let seq = 5000 ; type Proto = "ipc" | "tcp" | "udp" | "inproc" ; export function uniqAddress ( proto ) { const id = seq ++ ; switch ( proto ) { case "ipc" : { const sock = path . resolve ( __dirname , `../../tmp/${ proto }-${ id }` ) ; return `${ proto }://${ sock }` ; } case "tcp" : case "udp" : return `${ proto }://127.0.0.1:${ id }` ; default : return `${ proto }://${ proto }-${ id }` ; } } export function testProtos ( ... requested ) { const set = new Set ( requested ) ; if ( ! zmq . capability . ipc ) set . delete ( "ipc" ) ; if ( semver . satisfies ( zmq . version , "< 4.2" ) ) set . delete ( "inproc" ) ; if ( ! set . size ) console . error ( "Warning: test protocol set is empty" ) ; return [ ... set ] ; } export async function createWorker < T , D extends { } > ( data , fn ) { const src = `
    const {parentPort, workerData} = require("worker_threads")
    const zmq = require(${ JSON . stringify ( path . resolve ( __dirname , "../.." ) ) })

    async function run() {
      const fn = ${ fn . toString ( ) }
      const msg = await fn(workerData)
      parentPort.postMessage(msg)
    }

    run()
  ` ; const { Worker } = await import ( "worker_threads" ) ; return new Promise < T > ( ( resolve , reject ) => { const worker = new Worker ( src , { eval : true , workerData : data , } ) ; let message ; worker . on ( "message" , msg => { message = msg ; } ) ; worker . on ( "exit" , code => { if ( code === 0 ) { resolve ( message ) ; } else { reject ( new Error ( `Worker stopped with exit code ${ code }` ) ) ; } } ) ; } ) ; } interface Result { code : number ; stdout : Buffer ; stderr : Buffer ; } export function createProcess ( fn ) { const src = `
    const zmq = require(${ JSON . stringify ( path . resolve ( __dirname , "../.." ) ) })
    const fn = ${ fn . toString ( ) }
    fn()
  ` ; const child = spawn ( process . argv [ 0 ] , [ "--expose_gc" ] ) ; child . stdin . write ( src ) ; child . stdin . end ( ) ; let stdout = Buffer . alloc ( 0 ) ; let stderr = Buffer . alloc ( 0 ) ; child . stdout . on ( "data" , ( data ) => { stdout = Buffer . concat ( [ stdout , data ] ) ; } ) ; child . stderr . on ( "data" , ( data ) => { stderr = Buffer . concat ( [ stderr , data ] ) ; } ) ; return new Promise ( ( resolve , reject ) => { child . on ( "close" , ( code , signal ) => { if ( signal != null ) { reject ( new Error ( `Child exited with ${ signal }` ) ) ; } else { resolve ( { code , stdout , stderr } ) ; } } ) ; setTimeout ( ( ) => { resolve ( { code : - 1 , stdout , stderr } ) ; child . kill ( ) ; } , 750 ) ; } ) ; } export function captureEvent < E extends zmq . EventType > ( socket , type ) { return new Promise ( resolve => socket . events . on < E > ( type , resolve ) ) ; } export async function captureEventsUntil ( socket , type ) { const events = [ ] ; for await ( const event of socket . events ) { events . push ( event ) ; if ( event . type === type ) break ; } return events ; }