|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const test = require('node:test') |
4 | | -const net = require('node:net') |
5 | | -const path = require('node:path') |
6 | | -const { fork, spawn } = require('node:child_process') |
7 | | -const { Transform } = require('node:stream') |
| 3 | +const { test } = require('node:test') |
| 4 | +const net = require('net') |
| 5 | +const path = require('path') |
| 6 | +const { spawn } = require('child_process') |
| 7 | +const { withResolvers } = require('./utils') |
8 | 8 |
|
9 | | -const { default: why } = require('why-is-node-running') |
10 | | - |
11 | | -// https://github.com/pinojs/pino-socket/issues/5 |
12 | | -test('issue #5', (t, done) => { |
| 9 | +test('issue #5', async function (t) { |
13 | 10 | t.plan(2) |
| 11 | + const { |
| 12 | + promise: scriptPromise, |
| 13 | + resolve: scriptResolve |
| 14 | + } = withResolvers() |
| 15 | + const { |
| 16 | + promise: psockPromise, |
| 17 | + resolve: psockResolve |
| 18 | + } = withResolvers() |
14 | 19 |
|
15 | 20 | const server = net.createServer() |
16 | 21 | server.unref() |
17 | 22 | server.listen(() => { |
18 | 23 | const { address, port } = server.address() |
19 | 24 | const scriptPath = path.join(__dirname, 'fixtures', 'issue5.js') |
| 25 | + const script = spawn('node', [scriptPath], { stdio: ['pipe', 'pipe', 'pipe', 'ipc'] }) |
| 26 | + script.send('start') |
20 | 27 | const psockPath = path.join(__dirname, '..', 'psock.js') |
21 | | - const script = fork(scriptPath, { |
22 | | - stdio: ['pipe', 'pipe', 'pipe', 'ipc'] |
23 | | - }) |
24 | | - const psock = spawn( |
25 | | - process.argv0, |
26 | | - [psockPath, '-a', address, '-p', port, '-m', 'tcp', '-ne'] |
27 | | - ) |
| 28 | + const psock = spawn('node', [psockPath, '-a', address, '-p', port, '-m', 'tcp', '-e']) |
28 | 29 |
|
29 | | - script.on('close', (code, signal) => { |
30 | | - process._rawDebug('!!! script close', code, signal) |
| 30 | + script.on('close', (code) => { |
31 | 31 | t.assert.equal(code, 1) |
| 32 | + scriptResolve() |
32 | 33 | }) |
33 | | - // script.stdout.pipe(process.stdout) |
34 | | - script.stdout.pipe(psock.stdin) |
35 | | - // psock.stdin.pipe(process.stdout) |
36 | | - psock.stdin.pipe( |
37 | | - new Transform({ |
38 | | - transform (chunk, enc, cb) { |
39 | | - process._rawDebug('!!! psock.stdin', chunk.toString()) |
40 | | - cb(null, chunk) |
41 | | - } |
42 | | - }) |
43 | | - ) |
44 | 34 |
|
45 | 35 | let output = '' |
46 | | - psock.stdin.on('data', (data) => { |
47 | | - process._rawDebug('!!! psock data', data.toString()) |
48 | | - output += data.toString() |
49 | | - }) |
50 | | - psock.on('close', (code, signal) => { |
51 | | - process._rawDebug('!!! psock close', code, signal) |
52 | | - t.assert.equal(output.length > 0, true) |
53 | | - done() |
| 36 | + script.stdout.pipe(psock.stdin) |
| 37 | + psock.stdout.on('data', chunk => { |
| 38 | + output += chunk.toString() |
54 | 39 | }) |
55 | | - |
56 | | - script.send('doit', (error) => { |
57 | | - process._rawDebug('!!! send result', error) |
| 40 | + psock.on('close', () => { |
| 41 | + t.assert.strictEqual(output.length > 0, true) |
| 42 | + psockResolve() |
58 | 43 | }) |
59 | 44 | }) |
| 45 | + |
| 46 | + await Promise.all([scriptPromise, psockPromise]) |
60 | 47 | }) |
0 commit comments