summaryrefslogtreecommitdiff
path: root/benchmark/http/http_server_for_chunky_client.js
blob: d85e15bcbed4e932cb080ef0c5ddaa15159436bd (plain)
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
'use strict';

var path = require('path');
var http = require('http');
var fs = require('fs');
var spawn = require('child_process').spawn;
require('../common.js');
var test = require('../../test/common.js');
var pep = path.dirname(process.argv[1]) + '/_chunky_http_client.js';
var PIPE = test.PIPE;

try {
  fs.accessSync(test.tmpDir, fs.F_OK);
} catch (e) {
  fs.mkdirSync(test.tmpDir);
}

var server;
try {
  fs.unlinkSync(PIPE);
} catch (e) { /* ignore */ }

server = http.createServer(function(req, res) {
  res.writeHead(200, { 'content-type': 'text/plain',
                       'content-length': '2' });
  res.end('ok');
});

server.on('error', function(err) {
  throw new Error('server error: ' + err);
});

try {
  var child;

  server.listen(PIPE);

  child = spawn(process.execPath, [pep], { });

  child.on('error', function(err) {
    throw new Error('spawn error: ' + err);
  });

  child.stdout.pipe(process.stdout);
  child.stderr.pipe(process.stderr);

  child.on('close', function(exitCode) {
    server.close();
  });

} catch (e) {
  throw new Error('error: ' + e);
}