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

Commit 541bf83

Browse files
authored
feat: add support for timeline proxying (#31)
1 parent 721e475 commit 541bf83

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"dirty-chai": "^2.0.1",
4141
"err-code": "^2.0.0",
4242
"multiaddr": "^7.1.0",
43-
"peer-id": "~0.13.2"
43+
"peer-id": "~0.13.2",
44+
"sinon": "^7.5.0"
4445
},
4546
"devDependencies": {
4647
"aegir": "^20.2.0",

src/connection.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ class Connection {
7575
*/
7676
this._stat = {
7777
...stat,
78-
timeline: {
79-
...stat.timeline,
80-
close: undefined
81-
},
8278
status: 'open'
8379
}
8480

test/compliance.spec.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ const pair = require('it-pair')
1010

1111
describe('compliance tests', () => {
1212
tests({
13-
async setup () {
13+
/**
14+
* Test setup. `properties` allows the compliance test to override
15+
* certain values for testing.
16+
* @param {*} properties
17+
*/
18+
async setup (properties) {
1419
const localAddr = multiaddr('/ip4/127.0.0.1/tcp/8080')
1520
const remoteAddr = multiaddr('/ip4/127.0.0.1/tcp/8081')
1621
const [localPeer, remotePeer] = await Promise.all([
@@ -49,7 +54,8 @@ describe('compliance tests', () => {
4954
}
5055
},
5156
close: () => {},
52-
getStreams: () => openStreams
57+
getStreams: () => openStreams,
58+
...properties
5359
})
5460
},
5561
async teardown () {

test/connection.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const chai = require('chai')
66
const expect = chai.expect
77
chai.use(require('dirty-chai'))
8+
const sinon = require('sinon')
89

910
module.exports = (test) => {
1011
describe('connection', () => {
@@ -69,9 +70,27 @@ module.exports = (test) => {
6970

7071
describe('close connection', () => {
7172
let connection
73+
let timelineProxy
74+
const proxyHandler = {
75+
set () {
76+
return Reflect.set(...arguments)
77+
}
78+
}
7279

7380
beforeEach(async () => {
74-
connection = await test.setup()
81+
timelineProxy = new Proxy({
82+
open: Date.now() - 10,
83+
upgraded: Date.now()
84+
}, proxyHandler)
85+
86+
connection = await test.setup({
87+
stat: {
88+
timeline: timelineProxy,
89+
direction: 'outbound',
90+
encryption: '/crypto/1.0.0',
91+
multiplexer: '/muxer/1.0.0'
92+
}
93+
})
7594
if (!connection) throw new Error('missing connection')
7695
})
7796

@@ -100,6 +119,18 @@ module.exports = (test) => {
100119
expect(connection.stat.status).to.equal('closed')
101120
})
102121

122+
it('should support a proxy on the timeline', async () => {
123+
sinon.spy(proxyHandler, 'set')
124+
expect(connection.stat.timeline.close).to.not.exist()
125+
126+
await connection.close()
127+
expect(proxyHandler.set.callCount).to.equal(1)
128+
const [obj, key, value] = proxyHandler.set.getCall(0).args
129+
expect(obj).to.eql(connection.stat.timeline)
130+
expect(key).to.equal('close')
131+
expect(value).to.be.a('number').that.equals(connection.stat.timeline.close)
132+
})
133+
103134
it('should fail to create a new stream if the connection is closing', async () => {
104135
expect(connection.stat.timeline.close).to.not.exist()
105136
connection.close()

0 commit comments

Comments
 (0)