chore: add transports example (#851)

This commit is contained in:
Vasco Santos 2021-02-11 11:12:23 +01:00 committed by GitHub
parent f6a4cad827
commit 1af8472dc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 135 additions and 8 deletions

View File

@ -48,7 +48,7 @@ const after = async () => {
}
module.exports = {
bundlesize: { maxSize: '215kB' },
bundlesize: { maxSize: '220kB' },
hooks: {
pre: before,
post: after

View File

@ -128,3 +128,10 @@ jobs:
- uses: actions/checkout@v2
- run: yarn
- run: cd examples && yarn && npm run test -- pubsub
test-transports-example:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: yarn
- run: cd examples && yarn && npm run test -- transports

View File

@ -0,0 +1,38 @@
'use strict'
const path = require('path')
const execa = require('execa')
const pDefer = require('p-defer')
const uint8ArrayToString = require('uint8arrays/to-string')
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 Promise.all([
deferStarted.promise,
deferListen.promise
])
proc.kill()
}
module.exports = test

View File

@ -0,0 +1,30 @@
'use strict'
const path = require('path')
const execa = require('execa')
const pDefer = require('p-defer')
const uint8ArrayToString = require('uint8arrays/to-string')
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
})
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

View File

@ -0,0 +1,41 @@
'use strict'
const path = require('path')
const execa = require('execa')
const pDefer = require('p-defer')
const uint8ArrayToString = require('uint8arrays/to-string')
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
})
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

View File

@ -0,0 +1,13 @@
'use strict'
const test1 = require('./test-1')
const test2 = require('./test-2')
const test3 = require('./test-3')
async function test() {
await test1()
await test2()
await test3()
}
module.exports = test

View File

@ -119,7 +119,6 @@ describe('Dialing (direct, WebSockets)', () => {
await expect(dialer.connectToPeer(unsupportedAddr))
.to.eventually.be.rejectedWith(AggregateError)
.and.to.have.nested.property('._errors[0].code', ErrorCodes.ERR_TRANSPORT_DIAL_FAILED)
})
it('should be able to connect to a given peer', async () => {
@ -151,7 +150,6 @@ describe('Dialing (direct, WebSockets)', () => {
await expect(dialer.connectToPeer(peerId))
.to.eventually.be.rejectedWith(AggregateError)
.and.to.have.nested.property('._errors[0].code', ErrorCodes.ERR_TRANSPORT_DIAL_FAILED)
})
it('should abort dials on queue task timeout', async () => {