Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix: address cr
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 31, 2016
1 parent 4a12169 commit 2ed01e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const contains = require('lodash.contains')
exports = module.exports = TCP

const IPFS_CODE = 421
const CLOSE_TIMEOUT = 300

function TCP () {
if (!(this instanceof TCP)) {
Expand Down Expand Up @@ -97,8 +98,7 @@ function TCP () {
}

this.close = (callback) => {
const closeTimeout = 300

log('closing')
if (listeners.length === 0) {
log('Called close with no active listeners')
return callback()
Expand All @@ -110,7 +110,7 @@ function TCP () {
log('destroying %s', key)
listener.__connections[key].destroy()
})
}, closeTimeout)
}, CLOSE_TIMEOUT)

listener.close(cb)
}), callback)
Expand Down
31 changes: 31 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,35 @@ describe('libp2p-tcp', function () {
expect(valid[0]).to.deep.equal(mh1)
done()
})

it('destroys after timeout', (done) => {
const server = new TCPlibp2p()
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
server.createListener(mh, (conn) => {
const i = setInterval(() => {
conn.read()
conn.write('hi\n')
}, 100)
i.unref()
}, () => {
let connected = 0
const connectHandler = () => {
connected++
if (connected === 10) {
setTimeout(() => {
server.close(done)
}, 1)
}
}
const errorHandler = () => {}

for (let i = 0; i < 10; i++) {
const client = net.connect(9090)
client.on('connect', connectHandler)

// just ignore the resets
client.on('error', errorHandler)
}
})
})
})

0 comments on commit 2ed01e8

Please sign in to comment.