From 541bf83c1ea56834ceed6f658bd4d2776cfed66e Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Thu, 17 Oct 2019 14:38:37 +0200 Subject: [PATCH] feat: add support for timeline proxying (#31) --- package.json | 3 ++- src/connection.js | 4 ---- test/compliance.spec.js | 10 ++++++++-- test/connection.js | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index bee3f18..cf84648 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/connection.js b/src/connection.js index 77e9ecb..71ba299 100644 --- a/src/connection.js +++ b/src/connection.js @@ -75,10 +75,6 @@ class Connection { */ this._stat = { ...stat, - timeline: { - ...stat.timeline, - close: undefined - }, status: 'open' } diff --git a/test/compliance.spec.js b/test/compliance.spec.js index e2b9b4d..decbdd2 100644 --- a/test/compliance.spec.js +++ b/test/compliance.spec.js @@ -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 () { diff --git a/test/connection.js b/test/connection.js index feb388e..267760f 100644 --- a/test/connection.js +++ b/test/connection.js @@ -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()