• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Flags: --expose-gc
3// just like test-gc-http-client.js,
4// but with a timeout set
5
6const common = require('../common');
7const onGC = require('../common/ongc');
8
9function serverHandler(req, res) {
10  setTimeout(function() {
11    req.resume();
12    res.writeHead(200);
13    res.end('hello\n');
14  }, 100);
15}
16
17const http = require('http');
18const todo = 300;
19let done = 0;
20let count = 0;
21let countGC = 0;
22
23console.log(`We should do ${todo} requests`);
24
25const server = http.createServer(serverHandler);
26server.listen(0, common.mustCall(getall));
27
28function getall() {
29  if (count >= todo)
30    return;
31
32  const req = http.get({
33    hostname: 'localhost',
34    pathname: '/',
35    port: server.address().port
36  }, cb);
37
38  req.setTimeout(10, function() {
39    console.log('timeout (expected)');
40  });
41
42  count++;
43  onGC(req, { ongc });
44
45  setImmediate(getall);
46}
47
48for (let i = 0; i < 10; i++)
49  getall();
50
51function cb(res) {
52  res.resume();
53  done += 1;
54}
55
56function ongc() {
57  countGC++;
58}
59
60setInterval(status, 100).unref();
61
62function status() {
63  global.gc();
64  console.log('Done: %d/%d', done, todo);
65  console.log('Collected: %d/%d', countGC, count);
66  if (countGC === todo) server.close();
67}
68