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

Commit 2ed01e8

Browse files
fix: address cr
1 parent 4a12169 commit 2ed01e8

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const contains = require('lodash.contains')
1212
exports = module.exports = TCP
1313

1414
const IPFS_CODE = 421
15+
const CLOSE_TIMEOUT = 300
1516

1617
function TCP () {
1718
if (!(this instanceof TCP)) {
@@ -97,8 +98,7 @@ function TCP () {
9798
}
9899

99100
this.close = (callback) => {
100-
const closeTimeout = 300
101-
101+
log('closing')
102102
if (listeners.length === 0) {
103103
log('Called close with no active listeners')
104104
return callback()
@@ -110,7 +110,7 @@ function TCP () {
110110
log('destroying %s', key)
111111
listener.__connections[key].destroy()
112112
})
113-
}, closeTimeout)
113+
}, CLOSE_TIMEOUT)
114114

115115
listener.close(cb)
116116
}), callback)

test/index.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,35 @@ describe('libp2p-tcp', function () {
148148
expect(valid[0]).to.deep.equal(mh1)
149149
done()
150150
})
151+
152+
it('destroys after timeout', (done) => {
153+
const server = new TCPlibp2p()
154+
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
155+
server.createListener(mh, (conn) => {
156+
const i = setInterval(() => {
157+
conn.read()
158+
conn.write('hi\n')
159+
}, 100)
160+
i.unref()
161+
}, () => {
162+
let connected = 0
163+
const connectHandler = () => {
164+
connected++
165+
if (connected === 10) {
166+
setTimeout(() => {
167+
server.close(done)
168+
}, 1)
169+
}
170+
}
171+
const errorHandler = () => {}
172+
173+
for (let i = 0; i < 10; i++) {
174+
const client = net.connect(9090)
175+
client.on('connect', connectHandler)
176+
177+
// just ignore the resets
178+
client.on('error', errorHandler)
179+
}
180+
})
181+
})
151182
})

0 commit comments

Comments
 (0)