Last active
November 23, 2017 23:15
-
-
Save shuhei/32190cd48be9b6a6ccd64509e085b06b to your computer and use it in GitHub Desktop.
Tests for request timeout of perron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const assert = require('assert'); | |
const http = require('http'); | |
const Agent = require('agentkeepalive'); | |
const request = require('../lib/request'); | |
describe('request timeout', () => { | |
it('should not leak timeout event handler', (done) => { | |
const server = http.createServer((req, res) => { | |
res.writeHead(200, { | |
'Content-Type': 'application/json' | |
}); | |
setTimeout(() => { | |
res.end(JSON.stringify({ ok: true })); | |
}, 10); | |
}); | |
server.on('listening', () => { | |
const address = server.address(); | |
const agent = new Agent(); | |
const options = { | |
agent, | |
timeout: 100, | |
protocol: 'http:', | |
port: address.port | |
}; | |
const makeRequests = () => { | |
let promise = request(options); | |
for (let index = 0; index < 50; index += 1) { | |
promise = promise.then(response => { | |
assert.equal(response.req.socket.listeners('timeout').length, 1); | |
assert.equal(response.statusCode, 200); | |
return request(options); | |
}); | |
} | |
return promise; | |
}; | |
const promises = []; | |
for (let index = 0; index < 20; index += 1) { | |
promises.push(makeRequests()); | |
} | |
Promise.all(promises).then(() => { | |
// console.log(agent); | |
done(); | |
}).catch(done); | |
}); | |
server.listen(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment