js-libp2p/test/utils/mockMultiaddrConn.js
Jacob Heun 5e1dbc21a2 refactor(async): add dialer and upgrader (#462)
* chore(deps): update connection and multistream

* feat: add basic dial support for addresses and peers

* test: automatically require all node test files

* fix: dont catch and log in the wrong place

* test: add direct spec test

fix: improve dial error consistency

* feat: add dial timeouts and concurrency

Queue timeouts will result in aborts of the dials

* chore: fix linting

* test: verify dialer defaults

* feat: add initial upgrader

* fix: add more test coverage and fix bugs

* feat: libp2p creates the upgrader

* feat: hook up handle to the upgrader

* feat: hook up the dialer to libp2p

test: add node dialer libp2p tests

* feat: add connection listeners to upgrader

* feat: emit connect and disconnect events

* chore: use libp2p-interfaces

* fix: address review feedback

* fix: correct import

* refactor: dedupe connection creation code
2020-01-24 14:40:40 +01:00

44 lines
1.3 KiB
JavaScript

'use strict'
const duplexPair = require('it-pair/duplex')
const abortable = require('abortable-iterator')
const AbortController = require('abort-controller')
/**
* Returns both sides of a mocked MultiaddrConnection
* @param {object} options
* @param {Multiaddr[]} options.addrs Should contain two addresses for the local and remote peer
* @param {PeerId} options.remotePeer The peer that is being "dialed"
* @returns {{inbound:MultiaddrConnection, outbound:MultiaddrConnection}}
*/
module.exports = function mockMultiaddrConnPair ({ addrs, remotePeer }) {
const controller = new AbortController()
const [inbound, outbound] = duplexPair()
outbound.localAddr = addrs[0]
outbound.remoteAddr = addrs[1].encapsulate(`/p2p/${remotePeer.toB58String()}`)
outbound.timeline = {
open: Date.now()
}
outbound.close = () => {
outbound.timeline.close = Date.now()
controller.abort()
}
inbound.localAddr = addrs[1]
inbound.remoteAddr = addrs[0]
inbound.timeline = {
open: Date.now()
}
inbound.close = () => {
inbound.timeline.close = Date.now()
controller.abort()
}
// Make the sources abortable so we can close them easily
inbound.source = abortable(inbound.source, controller.signal)
outbound.source = abortable(outbound.source, controller.signal)
return { inbound, outbound }
}