diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ade1dcbc..6dabc16c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -65,3 +65,10 @@ jobs: - uses: actions/checkout@v2 - run: yarn - run: cd examples && yarn && npm run test -- auto-relay + test-chat-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: yarn + - run: cd examples && yarn && npm run test -- chat \ No newline at end of file diff --git a/examples/chat/src/dialer.js b/examples/chat/src/dialer.js index 10581ed2..62399984 100644 --- a/examples/chat/src/dialer.js +++ b/examples/chat/src/dialer.js @@ -3,7 +3,7 @@ const PeerId = require('peer-id') const multiaddr = require('multiaddr') -const createLibp2p = require('./libp2p-bundle') +const createLibp2p = require('./libp2p') const { stdinToStream, streamToConsole } = require('./stream') async function run() { diff --git a/examples/chat/src/libp2p-bundle.js b/examples/chat/src/libp2p.js similarity index 100% rename from examples/chat/src/libp2p-bundle.js rename to examples/chat/src/libp2p.js diff --git a/examples/chat/src/listener.js b/examples/chat/src/listener.js index 0a646600..ea7893ee 100644 --- a/examples/chat/src/listener.js +++ b/examples/chat/src/listener.js @@ -2,7 +2,7 @@ /* eslint-disable no-console */ const PeerId = require('peer-id') -const createLibp2p = require('./libp2p-bundle.js') +const createLibp2p = require('./libp2p.js') const { stdinToStream, streamToConsole } = require('./stream') async function run() { diff --git a/examples/chat/test.js b/examples/chat/test.js new file mode 100644 index 00000000..63e67f0c --- /dev/null +++ b/examples/chat/test.js @@ -0,0 +1,77 @@ +'use strict' + +const path = require('path') +const execa = require('execa') +const pDefer = require('p-defer') +const uint8ArrayToString = require('uint8arrays/to-string') + +function startProcess(name) { + return execa('node', [path.join(__dirname, name)], { + cwd: path.resolve(__dirname), + all: true + }) +} + +async function test () { + const message = 'test message' + let listenerOutput = '' + let dialerOutput = '' + + let isListening = false + let messageSent = false + const listenerReady = pDefer() + const dialerReady = pDefer() + const messageReceived = pDefer() + + // Step 1 process + process.stdout.write('node listener.js\n') + const listenerProc = startProcess('src/listener.js') + listenerProc.all.on('data', async (data) => { + process.stdout.write(data) + + listenerOutput += uint8ArrayToString(data) + + if (!isListening && listenerOutput.includes('Listener ready, listening on')) { + listenerReady.resolve() + isListening = true + } else if (isListening && listenerOutput.includes(message)) { + messageReceived.resolve() + } + }) + + await listenerReady.promise + process.stdout.write('==================================================================\n') + + // Step 2 process + process.stdout.write('node dialer.js\n') + const dialerProc = startProcess('src/dialer.js') + dialerProc.all.on('data', async (data) => { + process.stdout.write(data) + dialerOutput += uint8ArrayToString(data) + + if (!messageSent && dialerOutput.includes('Type a message and see what happens')) { + dialerReady.resolve() + dialerProc.stdin.write(message) + dialerProc.stdin.write('\n') + messageSent = true + } + }) + + await dialerReady.promise + process.stdout.write('==================================================================\n') + await messageReceived.promise + process.stdout.write('chat message received\n') + + listenerProc.kill() + dialerProc.kill() + await Promise.all([ + listenerProc, + dialerProc + ]).catch((err) => { + if (err.signal !== 'SIGTERM') { + throw err + } + }) +} + +module.exports = test