diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 306d1fd6..5faac18f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -72,6 +72,13 @@ jobs: - uses: actions/checkout@v2 - run: yarn - run: cd examples && yarn && npm run test -- chat + test-echo-example: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: yarn + - run: cd examples && yarn && npm run test -- echo test-libp2p-in-the-browser-example: needs: check runs-on: macos-latest diff --git a/examples/echo/src/dialer.js b/examples/echo/src/dialer.js index aa20208e..59387607 100644 --- a/examples/echo/src/dialer.js +++ b/examples/echo/src/dialer.js @@ -6,7 +6,7 @@ */ const PeerId = require('peer-id') -const createLibp2p = require('./libp2p-bundle') +const createLibp2p = require('./libp2p') const pipe = require('it-pipe') async function run() { diff --git a/examples/echo/src/libp2p-bundle.js b/examples/echo/src/libp2p.js similarity index 100% rename from examples/echo/src/libp2p-bundle.js rename to examples/echo/src/libp2p.js diff --git a/examples/echo/src/listener.js b/examples/echo/src/listener.js index e4a9cd17..1f814fc3 100644 --- a/examples/echo/src/listener.js +++ b/examples/echo/src/listener.js @@ -6,7 +6,7 @@ */ const PeerId = require('peer-id') -const createLibp2p = require('./libp2p-bundle') +const createLibp2p = require('./libp2p') const pipe = require('it-pipe') async function run() { diff --git a/examples/echo/test.js b/examples/echo/test.js new file mode 100644 index 00000000..168f0044 --- /dev/null +++ b/examples/echo/test.js @@ -0,0 +1,61 @@ +'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 listenerReady = 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) + const s = uint8ArrayToString(data) + + if (s.includes('Listener ready, listening on:')) { + listenerReady.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) + const s = uint8ArrayToString(data) + + if (s.includes('received echo:')) { + messageReceived.resolve() + } + }) + + await messageReceived.promise + process.stdout.write('echo message received\n') + + listenerProc.kill() + dialerProc.kill() + await Promise.all([ + listenerProc, + dialerProc + ]).catch((err) => { + if (err.signal !== 'SIGTERM') { + throw err + } + }) +} + +module.exports = test