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

Commit

Permalink
fixes socket hang by adding timeout to close
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed May 18, 2016
1 parent 651a5b3 commit 3b8e0e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
6 changes: 4 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const rawPeer = require('./test/peer.json')
const id = Id.createFromPrivKey(rawPeer.privKey)

gulp.task('libnode:start', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
const mh = multiaddr('/ip4/127.0.0.1/tcp/9200/ws')
const peer = new Peer(id)
peer.multiaddr.add(mh)

Expand All @@ -25,7 +25,9 @@ gulp.task('libnode:start', (done) => {
})

gulp.task('libnode:stop', (done) => {
node.swarm.close(done)
setTimeout(() => {
node.swarm.close(done)
}, 2000)
})

gulp.task('test:browser:before', ['libnode:start'])
Expand Down
30 changes: 16 additions & 14 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ const id = Id.createFromPrivKey(rawPeer.privKey)
describe('libp2p-ipfs-browser', function () {
this.timeout(10000)
let node
let peer

before((done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9200/ws')
peer = new Peer(id)
peer.multiaddr.add(mh)
done()
})

it('start', (done) => {
node = new Node()
node.start(done)
})

it('echo', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
const peer = new Peer(id)
peer.multiaddr.add(mh)

const message = 'Hello World!'
node.swarm.dial(peer, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist
Expand All @@ -40,18 +44,16 @@ describe('libp2p-ipfs-browser', function () {

describe('stress', () => {
it('one big write', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
const peer = new Peer(id)
peer.multiaddr.add(mh)

const message = new Buffer(1000000).fill('a').toString('hex')

node.swarm.dial(peer, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist

conn.write(message)
conn.write('STOP')

let result = ''

conn.on('data', (data) => {
if (data.toString() === 'STOP') {
conn.end()
Expand Down Expand Up @@ -83,14 +85,14 @@ describe('libp2p-ipfs-browser', function () {
expected += `${counter} `
}

setTimeout(() => {
while (++counter < 20000) {
conn.write(`${counter} `)
expected += `${counter} `
}
while (++counter < 20000) {
conn.write(`${counter} `)
expected += `${counter} `
}

setTimeout(() => {
conn.write('STOP')
}, 1000)
}, 2000)

let result = ''
conn.on('data', (data) => {
Expand Down

0 comments on commit 3b8e0e3

Please sign in to comment.