mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-31 23:01:04 +00:00
* test: remove all tests for a clean slate The refactor will require a large number of updates to the tests. In order to ensure we have done a decent deduplication, and have a cleaner suite of tests we've removed all tests. This will also allow us to more easily see tests for the refactored systems. We have a record of the latest test suites in master, so we are not losing any history. * chore: update tcp and websockets * chore: remove other transports until they are converted * chore: use mafmt and multiaddr async versions * chore: add and fix dependencies * chore: clean up travis file * feat: add new transport manager * docs: add constructor jsdocs * refactor(config): check that transports exist This also removes the other logic, it can be added when those subsystems are refactored * chore(deps): use async peer-id and peer-info * feat: wire up the transport manager with libp2p * chore: remove superstruct dep
140 lines
3.8 KiB
JavaScript
140 lines
3.8 KiB
JavaScript
'use strict'
|
|
/* eslint-env mocha */
|
|
|
|
const chai = require('chai')
|
|
chai.use(require('dirty-chai'))
|
|
const { expect } = chai
|
|
const sinon = require('sinon')
|
|
|
|
const multiaddr = require('multiaddr')
|
|
const Transport = require('libp2p-websockets')
|
|
const TransportManager = require('../../src/transport-manager')
|
|
const mockUpgrader = require('../utils/mockUpgrader')
|
|
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
|
|
const { codes: ErrorCodes } = require('../../src/errors')
|
|
const Libp2p = require('../../src')
|
|
const Peers = require('../fixtures/peers')
|
|
const PeerId = require('peer-id')
|
|
const PeerInfo = require('peer-info')
|
|
|
|
describe('Transport Manager (WebSockets)', () => {
|
|
let tm
|
|
|
|
before(() => {
|
|
tm = new TransportManager({
|
|
libp2p: {},
|
|
upgrader: mockUpgrader,
|
|
onConnection: () => {}
|
|
})
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await tm.removeAll()
|
|
expect(tm._transports.size).to.equal(0)
|
|
})
|
|
|
|
it('should be able to add and remove a transport', async () => {
|
|
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
|
|
expect(tm._transports.size).to.equal(1)
|
|
await tm.remove(Transport.prototype[Symbol.toStringTag])
|
|
})
|
|
|
|
it('should not be able to add a transport without a key', () => {
|
|
expect(() => {
|
|
tm.add(undefined, Transport)
|
|
}).to.throw().that.satisfies((err) => {
|
|
return err.code === ErrorCodes.ERR_INVALID_KEY
|
|
})
|
|
})
|
|
|
|
it('should not be able to add a transport twice', () => {
|
|
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
|
|
expect(() => {
|
|
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
|
|
}).to.throw().that.satisfies((err) => {
|
|
return err.code === ErrorCodes.ERR_DUPLICATE_TRANSPORT
|
|
})
|
|
})
|
|
|
|
it('should be able to dial', async () => {
|
|
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
|
|
const addr = MULTIADDRS_WEBSOCKETS[0]
|
|
const connection = await tm.dial(addr)
|
|
expect(connection).to.exist()
|
|
await connection.close()
|
|
})
|
|
|
|
it('should fail to dial an unsupported address', async () => {
|
|
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
|
|
const addr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
|
try {
|
|
await tm.dial(addr)
|
|
} catch (err) {
|
|
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
|
|
return
|
|
}
|
|
|
|
expect.fail('Dial attempt should have failed')
|
|
})
|
|
|
|
it('should fail to listen with no valid address', async () => {
|
|
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
|
|
const addrs = [multiaddr('/ip4/127.0.0.1/tcp/0')]
|
|
try {
|
|
await tm.listen(addrs)
|
|
} catch (err) {
|
|
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_NO_VALID_ADDRESSES)
|
|
return
|
|
}
|
|
|
|
expect.fail('should have failed')
|
|
})
|
|
})
|
|
|
|
describe('libp2p.transportManager', () => {
|
|
let peerInfo
|
|
let libp2p
|
|
|
|
before(async () => {
|
|
const peerId = await PeerId.createFromJSON(Peers[0])
|
|
peerInfo = new PeerInfo(peerId)
|
|
})
|
|
|
|
afterEach(async () => {
|
|
sinon.restore()
|
|
libp2p && await libp2p.stop()
|
|
libp2p = null
|
|
})
|
|
|
|
it('should create a TransportManager', () => {
|
|
libp2p = new Libp2p({
|
|
peerInfo,
|
|
modules: {
|
|
transport: [Transport]
|
|
}
|
|
})
|
|
|
|
expect(libp2p.transportManager).to.exist()
|
|
expect(libp2p.transportManager._transports.size).to.equal(1)
|
|
})
|
|
|
|
it('starting and stopping libp2p should start and stop TransportManager', async () => {
|
|
libp2p = new Libp2p({
|
|
peerInfo,
|
|
modules: {
|
|
transport: [Transport]
|
|
}
|
|
})
|
|
|
|
// We don't need to listen, stub it
|
|
sinon.stub(libp2p.transportManager, 'listen').returns(true)
|
|
sinon.spy(libp2p.transportManager, 'close')
|
|
|
|
await libp2p.start()
|
|
await libp2p.stop()
|
|
|
|
expect(libp2p.transportManager.listen.callCount).to.equal(1)
|
|
expect(libp2p.transportManager.close.callCount).to.equal(1)
|
|
})
|
|
})
|