diff --git a/examples/connection-encryption/test.js b/examples/connection-encryption/test.js index 4574dd9acf..b6ea10ad5d 100644 --- a/examples/connection-encryption/test.js +++ b/examples/connection-encryption/test.js @@ -1,30 +1,14 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pDefer = require('p-defer') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') async function test () { - const messageReceived = pDefer() process.stdout.write('1.js\n') - const proc = execa('node', [path.join(__dirname, '1.js')], { - cwd: path.resolve(__dirname), - all: true + await waitForOutput('This information is sent out encrypted to the other peer', 'node', [path.join(__dirname, '1.js')], { + cwd: __dirname }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - - const s = uint8ArrayToString(data) - if (s.includes('This information is sent out encrypted to the other peer')) { - messageReceived.resolve() - } - }) - - await messageReceived.promise - proc.kill() } module.exports = test diff --git a/examples/discovery-mechanisms/test-1.js b/examples/discovery-mechanisms/test-1.js index 36f297b3b3..73e90ad54c 100644 --- a/examples/discovery-mechanisms/test-1.js +++ b/examples/discovery-mechanisms/test-1.js @@ -1,27 +1,13 @@ 'use strict' const path = require('path') -const execa = require('execa') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') async function test () { process.stdout.write('1.js\n') - const proc = execa('node', [path.join(__dirname, '1.js')], { - cwd: path.resolve(__dirname), - all: true - }) - - let output = '' - - proc.all.on('data', async (data) => { - process.stdout.write(data) - output += uint8ArrayToString(data) - - // Discovered and connected - if (output.includes('Connection established to:')) { - proc.kill() - } + await waitForOutput('Connection established to:', 'node', [path.join(__dirname, '1.js')], { + cwd: __dirname }) } diff --git a/examples/peer-and-content-routing/test-1.js b/examples/peer-and-content-routing/test-1.js index 9e10f7aeee..43d6c1eb7e 100644 --- a/examples/peer-and-content-routing/test-1.js +++ b/examples/peer-and-content-routing/test-1.js @@ -1,28 +1,13 @@ 'use strict' const path = require('path') -const execa = require('execa') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') -async function test() { +async function test () { process.stdout.write('1.js\n') - const proc = execa('node', [path.join(__dirname, '1.js')], { - cwd: path.resolve(__dirname), - all: true - }) - - let output = '' - - proc.all.on('data', async (data) => { - process.stdout.write(data) - - output += uint8ArrayToString(data) - - // Discovered peers - if (output.includes('Found it, multiaddrs are:')) { - proc.kill() - } + await waitForOutput('Found it, multiaddrs are:', 'node', [path.join(__dirname, '1.js')], { + cwd: __dirname }) } diff --git a/examples/peer-and-content-routing/test-2.js b/examples/peer-and-content-routing/test-2.js index 644820b687..76c492de66 100644 --- a/examples/peer-and-content-routing/test-2.js +++ b/examples/peer-and-content-routing/test-2.js @@ -1,27 +1,13 @@ 'use strict' const path = require('path') -const execa = require('execa') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') -async function test() { +async function test () { process.stdout.write('2.js\n') - const proc = execa('node', [path.join(__dirname, '2.js')], { - cwd: path.resolve(__dirname), - all: true - }) - - let output = '' - - proc.all.on('data', async (data) => { - process.stdout.write(data) - - output += uint8ArrayToString(data) - - if (output.includes('Found provider:')) { - proc.kill() - } + await waitForOutput('Found provider:', 'node', [path.join(__dirname, '2.js')], { + cwd: __dirname }) } diff --git a/examples/pnet/test.js b/examples/pnet/test.js index 8a295dbb8e..56519927aa 100644 --- a/examples/pnet/test.js +++ b/examples/pnet/test.js @@ -1,30 +1,13 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pDefer = require('p-defer') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') async function test () { - const messageReceived = pDefer() - process.stdout.write('index.js\n') - - const proc = execa('node', [path.join(__dirname, 'index.js')], { - cwd: path.resolve(__dirname), - all: true - }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - - const s = uint8ArrayToString(data) - if (s.includes('This message is sent on a private network')) { - messageReceived.resolve() - } + await waitForOutput('This message is sent on a private network', 'node', [path.join(__dirname, 'index.js')], { + cwd: __dirname }) - - await messageReceived.promise - proc.kill() } module.exports = test + diff --git a/examples/protocol-and-stream-muxing/test-1.js b/examples/protocol-and-stream-muxing/test-1.js index c851b879e8..91f409d5a9 100644 --- a/examples/protocol-and-stream-muxing/test-1.js +++ b/examples/protocol-and-stream-muxing/test-1.js @@ -1,31 +1,14 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pDefer = require('p-defer') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') -async function test() { - const messageDefer = pDefer() +async function test () { process.stdout.write('1.js\n') - const proc = execa('node', [path.join(__dirname, '1.js')], { - cwd: path.resolve(__dirname), - all: true + await waitForOutput('my own protocol, wow!', 'node', [path.join(__dirname, '1.js')], { + cwd: __dirname }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - - const line = uint8ArrayToString(data) - - if (line.includes('my own protocol, wow!')) { - messageDefer.resolve() - } - }) - - await messageDefer.promise - proc.kill() } module.exports = test diff --git a/examples/protocol-and-stream-muxing/test-2.js b/examples/protocol-and-stream-muxing/test-2.js index 3f04c574f2..26b4b12ea1 100644 --- a/examples/protocol-and-stream-muxing/test-2.js +++ b/examples/protocol-and-stream-muxing/test-2.js @@ -1,38 +1,14 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pWaitFor = require('p-wait-for') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') -const messages = [ - 'protocol (a)', - 'protocol (b)', - 'another stream on protocol (b)' -] - -async function test() { +async function test () { process.stdout.write('2.js\n') - let count = 0 - const proc = execa('node', [path.join(__dirname, '2.js')], { - cwd: path.resolve(__dirname), - all: true + await waitForOutput('another stream on protocol (b)', 'node', [path.join(__dirname, '2.js')], { + cwd: __dirname }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - - const line = uint8ArrayToString(data) - - if (messages.find((m) => line.includes(m))) { - count += 1 - } - }) - - await pWaitFor(() => count === messages.length) - - proc.kill() } module.exports = test diff --git a/examples/protocol-and-stream-muxing/test-3.js b/examples/protocol-and-stream-muxing/test-3.js index 223e1d124b..8724237cf2 100644 --- a/examples/protocol-and-stream-muxing/test-3.js +++ b/examples/protocol-and-stream-muxing/test-3.js @@ -1,37 +1,14 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pWaitFor = require('p-wait-for') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') -const messages = [ - 'from 1 to 2', - 'from 2 to 1' -] - -async function test() { +async function test () { process.stdout.write('3.js\n') - let count = 0 - const proc = execa('node', [path.join(__dirname, '3.js')], { - cwd: path.resolve(__dirname), - all: true + await waitForOutput('from 2 to 1', 'node', [path.join(__dirname, '3.js')], { + cwd: __dirname }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - - const line = uint8ArrayToString(data) - - if (messages.find((m) => line.includes(m))) { - count += 1 - } - }) - - await pWaitFor(() => count === messages.length) - - proc.kill() } module.exports = test diff --git a/examples/transports/test-1.js b/examples/transports/test-1.js index 63e320329d..bcdaf57b23 100644 --- a/examples/transports/test-1.js +++ b/examples/transports/test-1.js @@ -1,38 +1,14 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pDefer = require('p-defer') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') async function test () { - const deferStarted = pDefer() - const deferListen = pDefer() - process.stdout.write('1.js\n') - const proc = execa('node', [path.join(__dirname, '1.js')], { - cwd: path.resolve(__dirname), - all: true - }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - const line = uint8ArrayToString(data) - - - if (line.includes('node has started (true/false): true')) { - deferStarted.resolve() - } else if (line.includes('p2p')) { - deferListen.resolve() - } + await waitForOutput('/p2p/', 'node', [path.join(__dirname, '1.js')], { + cwd: __dirname }) - - await Promise.all([ - deferStarted.promise, - deferListen.promise - ]) - proc.kill() } module.exports = test diff --git a/examples/transports/test-2.js b/examples/transports/test-2.js index 48c37fff70..b383ac9e98 100644 --- a/examples/transports/test-2.js +++ b/examples/transports/test-2.js @@ -1,30 +1,14 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pDefer = require('p-defer') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') async function test () { - const defer = pDefer() process.stdout.write('2.js\n') - const proc = execa('node', [path.join(__dirname, '2.js')], { - cwd: path.resolve(__dirname), - all: true + await waitForOutput('Hello p2p world!', 'node', [path.join(__dirname, '2.js')], { + cwd: __dirname }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - const line = uint8ArrayToString(data) - - if (line.includes('Hello p2p world!')) { - defer.resolve() - } - }) - - await defer.promise - proc.kill() } module.exports = test diff --git a/examples/transports/test-3.js b/examples/transports/test-3.js index cf75b44e45..642ab045b4 100644 --- a/examples/transports/test-3.js +++ b/examples/transports/test-3.js @@ -1,41 +1,14 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pDefer = require('p-defer') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') async function test () { - const deferNode1 = pDefer() - const deferNode2 = pDefer() - const deferNode3 = pDefer() - process.stdout.write('3.js\n') - const proc = execa('node', [path.join(__dirname, '3.js')], { - cwd: path.resolve(__dirname), - all: true + await waitForOutput('node 3 failed to dial to node 1 with:', 'node', [path.join(__dirname, '3.js')], { + cwd: __dirname }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - const line = uint8ArrayToString(data) - - if (line.includes('node 1 dialed to node 2 successfully')) { - deferNode1.resolve() - } else if (line.includes('node 2 dialed to node 3 successfully')) { - deferNode2.resolve() - } else if (line.includes('node 3 failed to dial to node 1 with:')) { - deferNode3.resolve() - } - }) - - await Promise.all([ - deferNode1.promise, - deferNode2.promise, - deferNode3.promise - ]) - proc.kill() } module.exports = test diff --git a/examples/transports/test-4.js b/examples/transports/test-4.js index 7b2b6e5d79..5186ff533e 100644 --- a/examples/transports/test-4.js +++ b/examples/transports/test-4.js @@ -1,33 +1,14 @@ 'use strict' const path = require('path') -const execa = require('execa') -const pDefer = require('p-defer') -const { toString: uint8ArrayToString } = require('uint8arrays/to-string') +const { waitForOutput } = require('../utils') async function test () { - const deferNode1 = pDefer() - process.stdout.write('4.js\n') - const proc = execa('node', [path.join(__dirname, '4.js')], { - cwd: path.resolve(__dirname), - all: true + await waitForOutput('node 2 dialed to node 1 successfully', 'node', [path.join(__dirname, '4.js')], { + cwd: __dirname }) - - proc.all.on('data', async (data) => { - process.stdout.write(data) - const line = uint8ArrayToString(data) - - if (line.includes('node 2 dialed to node 1 successfully')) { - deferNode1.resolve() - } - }) - - await Promise.all([ - deferNode1.promise, - ]) - proc.kill() } module.exports = test