/*
 * net-dolisten-test.js: Basic tests for the lib/net.js module
 *
 * (C) 2011 stolsma
 * MIT LICENCE
 *
 */

var assert = require('assert'),
    path = require('path'),
    spawn = require('child_process').spawn,
    vows = require('vows'),
    helper = require('../helper/macros.js'),
    carapace = require('../../lib/carapace');

var script = path.join(__dirname, '..', 'fixtures' ,'multi-server.js'),
    IOPORT = 5060,
    testPort = 8000,
    argv = ['--hook-port', IOPORT, '--hook-name', 'carapace', script];

vows.describe('carapace/net/dolisten').addBatch({
  "When using haibu-carapace": helper.assertListen(IOPORT, {
    "spawning the server-dolisten.js script the child carapace": {
      topic: function () {
        var that = this,
            child,
            result = {
              events: [],
              exitCode: -1
            };
        child = spawn(carapace.bin, argv);

        child.stdout.on('data', function (data) {
          process.stdout.write(data);
        });

        child.stderr.on('data', function (data) {
          process.stdout.write(data);
        });

        child.on('exit', function (code) {
          result.exitCode = code;
          // process all events before asserting
          process.nextTick(function () {
            that.callback(null, result, child);
          });
        });

        carapace.on('*::carapace::port', function onPort (info) {
          result.events.push({
            event: this.event,
            info: info
          });
        });
      },
      "should exit": {
        topic: function (info, child) {
          this.callback(null, info, child);
        },
        "with the correct exit code": function (_, info, child) {
          assert.equal(info.exitCode, 0);
        },
        "and 3x emit the `*::carapace::port` event with the correct port": function (_, info, child) {
          var desired = testPort,
              basePort = desired;
          assert.equal(info.events.length, 3);
          info.events.forEach(function (event) {
            assert.equal(event.info.port, basePort++);
            assert.equal(event.info.desired, desired);
          });
        }
      }
    }
  })
}).export(module);

@@ -169,21 +169,20 @@
 '),%0A    
-spawn
+fork
  = requi
@@ -201,21 +201,20 @@
 ocess').
-spawn
+fork
 ,%0A    vo
@@ -403,27 +403,8 @@
 '),%0A
-    IOPORT = 5060,%0A
     
@@ -436,58 +436,8 @@
  = %5B
-'--hook-port', IOPORT, '--hook-name', 'carapace', 
 scri
@@ -526,36 +526,8 @@
 ce%22:
- helper.assertListen(IOPORT,
  %7B%0A 
@@ -770,21 +770,20 @@
 child = 
-spawn
+fork
 (carapac
@@ -801,210 +801,8 @@
 );%0A%0A
-        child.stdout.on('data', function (data) %7B%0A          process.stdout.write(data);%0A        %7D);%0A%0A        child.stderr.on('data', function (data) %7B%0A          process.stdout.write(data);%0A        %7D);%0A%0A
     
@@ -1052,37 +1052,24 @@
    c
-arapace.on('*::carapace::port
+hild.on('message
 ', f
@@ -1093,32 +1093,56 @@
 nfo) %7B%0A         
+ info.event == 'port' &&
  result.events.p
@@ -1166,20 +1166,20 @@
  event: 
-this
+info
 .event,%0A
@@ -1200,16 +1200,21 @@
 fo: info
+.data
 %0A       
@@ -1508,21 +1508,8 @@
 he %60
-*::carapace::
 port
@@ -1883,17 +1883,16 @@
    %7D%0A  %7D
-)
 %0A%7D).expo

Use the `fork` API in `net-multiple-servers-test`