forked from rase-/node-XMLHttpRequest
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtest-keepalive.js
37 lines (34 loc) · 980 Bytes
/
test-keepalive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var assert = require("assert");
var http = require('http');
var { XMLHttpRequest } = require("../lib/XMLHttpRequest");
var server = http.createServer({ keepAliveTimeout: 200 }, function handleConnection (req, res) {
res.write('hello\n');
res.end();
}).listen(8889);
var agent = new http.Agent({
keepAlive: true,
keepAliveMsecs: 2000,
});
var xhr = new XMLHttpRequest({ agent });
var url = "http://localhost:8889";
var repeats = 0;
var maxMessages = 20;
var interval = setInterval(function sendRequest() {
xhr.open("GET", url);
xhr.onloadend = function(event) {
if (xhr.status !== 200) {
console.error('Error: non-200 xhr response, message is\n', xhr.responseText);
clearInterval(interval);
agent.destroy();
server.close();
assert.equal(xhr.status, 200);
}
if (repeats++ > maxMessages) {
console.log('Done.');
clearInterval(interval);
agent.destroy();
server.close();
}
}
xhr.send();
}, 200);