Name: js-handler/node_modules/daemon/test/daemon-test.js 
1:
var assert = require('assert');
2:
var http = require('http');
3:
var spawn = require('child_process').spawn;
4:
var after = require('after');
5:
 
6:
function launch(args) {
7:
    var child = spawn(process.execPath, args);
8:
 
9:
    child.stdout.pipe(process.stdout, {end: false});
10:
    child.stderr.pipe(process.stderr, {end: false});
11:
 
12:
    return child;
13:
};
14:
 
15:
// sanity check that a no daemon process exits
16:
test('no daemon', function(done) {
17:
    var script = __dirname + '/fixtures/nodaemon.js';
18:
    var child = launch([script]);
19:
    child.on('exit', function(code) {
20:
      assert.equal(code, 0);
21:
      done();
22:
    });
23:
});
24:
 
25:
test('simple', function(done) {
26:
    var script = __dirname + '/fixtures/simple.js';
27:
 
28:
    done = after(2, done);
29:
    var port = 12345;
30:
 
31:
    var child = launch([script, port]);
32:
 
33:
    child.stdout.pipe(process.stdout, {end: false});
34:
    child.stderr.pipe(process.stderr, {end: false});
35:
 
36:
    // spawning child should exit
37:
    child.on('exit', function(code) {
38:
      assert.equal(code, 0);
39:
      done();
40:
    });
41:
 
42:
    // wait for http server to start up
43:
    setTimeout(function() {
44:
      var opt = {
45:
        host: 'localhost',
46:
        port: port
47:
      };
48:
 
49:
      http.get(opt, function(res) {
50:
        res.setEncoding('utf8');
51:
        res.on('data', function(chunk) {
52:
          process.kill(chunk, 'SIGTERM');
53:
          done();
54:
        });
55:
      });
56:
    }, 500);
57:
});