feat: add support for timeline proxying (#31)

This commit is contained in:
Jacob Heun 2019-10-17 14:38:37 +02:00 committed by GitHub
parent 721e475fd2
commit 541bf83c1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 8 deletions

View File

@ -40,7 +40,8 @@
"dirty-chai": "^2.0.1",
"err-code": "^2.0.0",
"multiaddr": "^7.1.0",
"peer-id": "~0.13.2"
"peer-id": "~0.13.2",
"sinon": "^7.5.0"
},
"devDependencies": {
"aegir": "^20.2.0",

View File

@ -75,10 +75,6 @@ class Connection {
*/
this._stat = {
...stat,
timeline: {
...stat.timeline,
close: undefined
},
status: 'open'
}

View File

@ -10,7 +10,12 @@ const pair = require('it-pair')
describe('compliance tests', () => {
tests({
async setup () {
/**
* Test setup. `properties` allows the compliance test to override
* certain values for testing.
* @param {*} properties
*/
async setup (properties) {
const localAddr = multiaddr('/ip4/127.0.0.1/tcp/8080')
const remoteAddr = multiaddr('/ip4/127.0.0.1/tcp/8081')
const [localPeer, remotePeer] = await Promise.all([
@ -49,7 +54,8 @@ describe('compliance tests', () => {
}
},
close: () => {},
getStreams: () => openStreams
getStreams: () => openStreams,
...properties
})
},
async teardown () {

View File

@ -5,6 +5,7 @@
const chai = require('chai')
const expect = chai.expect
chai.use(require('dirty-chai'))
const sinon = require('sinon')
module.exports = (test) => {
describe('connection', () => {
@ -69,9 +70,27 @@ module.exports = (test) => {
describe('close connection', () => {
let connection
let timelineProxy
const proxyHandler = {
set () {
return Reflect.set(...arguments)
}
}
beforeEach(async () => {
connection = await test.setup()
timelineProxy = new Proxy({
open: Date.now() - 10,
upgraded: Date.now()
}, proxyHandler)
connection = await test.setup({
stat: {
timeline: timelineProxy,
direction: 'outbound',
encryption: '/crypto/1.0.0',
multiplexer: '/muxer/1.0.0'
}
})
if (!connection) throw new Error('missing connection')
})
@ -100,6 +119,18 @@ module.exports = (test) => {
expect(connection.stat.status).to.equal('closed')
})
it('should support a proxy on the timeline', async () => {
sinon.spy(proxyHandler, 'set')
expect(connection.stat.timeline.close).to.not.exist()
await connection.close()
expect(proxyHandler.set.callCount).to.equal(1)
const [obj, key, value] = proxyHandler.set.getCall(0).args
expect(obj).to.eql(connection.stat.timeline)
expect(key).to.equal('close')
expect(value).to.be.a('number').that.equals(connection.stat.timeline.close)
})
it('should fail to create a new stream if the connection is closing', async () => {
expect(connection.stat.timeline.close).to.not.exist()
connection.close()