Skip to content
This repository was archived by the owner on Mar 11, 2020. It is now read-only.

feat: add support for timeline proxying #31

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 0 additions & 4 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ class Connection {
*/
this._stat = {
...stat,
timeline: {
...stat.timeline,
close: undefined
},
status: 'open'
}

Expand Down
10 changes: 8 additions & 2 deletions test/compliance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -49,7 +54,8 @@ describe('compliance tests', () => {
}
},
close: () => {},
getStreams: () => openStreams
getStreams: () => openStreams,
...properties
})
},
async teardown () {
Expand Down
33 changes: 32 additions & 1 deletion test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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')
})

Expand Down Expand Up @@ -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()
Expand Down