summaryrefslogtreecommitdiff
path: root/node_modules/forever/test/helpers/macros.js
blob: 47471f42c216ed5d563010d270240c85eb85ce5b (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
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
/*
 * macros.js: Test macros for the forever module
 *
 * (C) 2010 Nodejitsu Inc.
 * MIT LICENCE
 *
 */
 
var assert = require('assert'),
    path = require('path'),
    spawn = require('child_process').spawn,
    nssocket = require('nssocket'),
    forever = require('../../lib/forever'),
    Worker = require('../../lib/forever/worker').Worker;
 
var macros = exports;

macros.assertTimes = function (script, times, options) {
  options.max = times;
  
  return {
    topic: function () {
      var child = new (forever.Monitor)(script, options);
      child.on('exit', this.callback.bind({}, null));
      child.start();
    },
    "should emit 'exit' when completed": function (err, child) {
      assert.equal(child.times, times);
    }
  }
};

macros.spawn = function (args, options) {
  options.topic = function () {
    var self = this;

    args = [path.join(__dirname, '..', 'bin', 'forever')].concat(args);

    var child = spawn(process.argv[0], args),
        stdout = '',
        stderr = '';

    child.stdout.on('data', function (data) {
      stdout += data;
    });
    child.stderr.on('data', function (data) {
      stderr += data;
    });
    child.once('exit', function (exitCode) {
      //
      // Remark: We wait 200 ms because of forever boot up time (master
      // doesn't wait for slave to start up after it's forked, it just quits)
      //
      setTimeout(function () {
        self.callback(null, exitCode, stdout, stderr);
      }, 200);
    });
  };
  return options;
};

macros.list = function (options) {
  options.topic = function () {
    forever.list(false, this.callback)
  };
  return options;
};

macros.assertStartsWith = function (string, substring) {
  assert.equal(string.slice(0, substring.length), substring);
};

macros.assertList = function (list) {
  assert.isNotNull(list);
  assert.lengthOf(list, 1);
};

macros.assertWorkerConnected = function (workerOptions, batch) {
  return {
    topic: function () {
      var self = this,
          reader = new nssocket.NsSocket(),
          worker = new Worker(workerOptions);

      worker.start(function (err, sock) {
        reader.connect(sock, function () {
          self.callback(null, reader, worker, workerOptions);
        });
      });
    },
    'worker should connect': batch
  };
};