This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
forked from newrelic/yakaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeepalive.js
96 lines (78 loc) · 2.11 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var test = require('tap').test;
var Agent = require('../index.js')
var http = require('http')
var web = http.createServer(function(req,res){req.pipe(process.stdout); res.end()});
test("yakaa should reuse the socket", function (t) {
t.plan(7);
web.listen();
var PORT = web.address().port;
var agent = new Agent({keepAlive : true, keepAliveTimeoutMsecs: 100});
var opts = {
port : PORT,
agent : agent,
};
var socket;
var i = 0;
agent.on('free', function (sock, opts) {
t.ok(i++ < 2, 'free socket');
});
// only destroyed once at the end
var j = 0;
agent.on('yakaa_destroy', function (s) {
t.ok(j++ < 1, 'destroy');
t.equals(j,1, 'destroy called once')
t.equals(i,2, 'free called twice')
t.end()
});
new http.ClientRequest(opts, next).end();
function next(res) {
res.pipe(process.stdout)
t.ok(res, 'first response');
// set short timeout so this test ends quickly
new http.ClientRequest(opts, done).end();
}
function done(res) {
res.pipe(process.stdout);
t.ok(res, 'second response');
web.close();
}
});
test("yakaa should destroy the socket after each request", function (t) {
t.plan(8);
web.listen();
var PORT = web.address().port;
// set a super short socket lifetime
// normally this would kill a TLS session,
// but our super simple test won't mind
var agent = new Agent({keepAlive : true, keepAliveTimeoutMsecs: 1});
var opts = {
port : PORT,
agent : agent,
};
var j=0;
agent.on('yakaa_destroy', function () {
t.ok(j++ < 2, 'destroy');
if (j==2) {
t.equals(j,2, 'destroy called twice')
t.equals(i,2, 'free called twice')
t.end();
}
})
var i=0;
agent.on('free', function (sock, opts) {
t.ok(i++ < 2, 'free socket');
});
new http.ClientRequest(opts, next).end();
function next(res) {
res.pipe(process.stdout)
t.ok(res, 'first response');
setTimeout(function () {
new http.ClientRequest(opts, done).end();
}, 10);
}
function done(res) {
res.pipe(process.stdout);
t.ok(res, 'second response');
web.close();
}
});