Skip to content

Commit 5c88d77

Browse files
authored
fix(connection): tracks streams properly (libp2p#25)
1 parent f6871af commit 5c88d77

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/connection/connection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class Connection {
138138

139139
const { stream, protocol } = await this._newStream(protocols)
140140

141-
this.addStream(stream, protocol)
141+
this.addStream(stream, { protocol })
142142

143143
return {
144144
stream,

src/connection/tests/connection.js

+13
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ module.exports = (test) => {
120120
expect(connection.stat.status).to.equal(Status.CLOSED)
121121
})
122122

123+
it('should properly track streams', async () => {
124+
// Open stream
125+
const protocol = '/echo/0.0.1'
126+
const { stream } = await connection.newStream(protocol)
127+
const trackedStream = connection.registry.get(stream.id)
128+
expect(trackedStream).to.have.property('protocol', protocol)
129+
130+
// Close stream
131+
await stream.close()
132+
133+
expect(connection.registry.get(stream.id)).to.not.exist()
134+
})
135+
123136
it('should support a proxy on the timeline', async () => {
124137
sinon.spy(proxyHandler, 'set')
125138
expect(connection.stat.timeline.close).to.not.exist()

test/connection/compliance.spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('compliance tests', () => {
2525
const openStreams = []
2626
let streamId = 0
2727

28-
return new Connection({
28+
const connection = new Connection({
2929
localPeer,
3030
remotePeer,
3131
localAddr,
@@ -43,7 +43,10 @@ describe('compliance tests', () => {
4343
const id = streamId++
4444
const stream = pair()
4545

46-
stream.close = () => stream.sink([])
46+
stream.close = async () => {
47+
await stream.sink([])
48+
connection.removeStream(stream.id)
49+
}
4750
stream.id = id
4851

4952
openStreams.push(stream)
@@ -57,6 +60,7 @@ describe('compliance tests', () => {
5760
getStreams: () => openStreams,
5861
...properties
5962
})
63+
return connection
6064
},
6165
async teardown () {
6266
// cleanup resources created by setup()

0 commit comments

Comments
 (0)