UNPKG

3.08 kBJavaScriptView Raw
1var PS = require('../index');
2var CP = require('child_process');
3var assert = require('assert');
4var Path = require('path');
5
6var serverPath = Path.resolve(__dirname, './node_process_for_test.js');
7var UpperCaseArg = '--UPPER_CASE';
8var child = null;
9var pid = null;
10
11function startProcess() {
12 child = CP.fork(serverPath, [UpperCaseArg]);
13 pid = child.pid;
14}
15
16describe('test', function () {
17 before(function (done) {
18 PS.lookup({arguments: 'node_process_for_test'}, function (err, list) {
19 var processLen = list.length;
20 var killedCount = 0;
21 if (processLen) {
22 list.forEach(function (item) {
23 PS.kill(item.pid, function () {
24 killedCount++;
25 if (killedCount === processLen) {
26 startProcess();
27 done();
28 }
29 });
30 });
31 } else {
32 startProcess();
33 done();
34 }
35 });
36 });
37
38 describe('#lookup()', function () {
39
40 it('by id', function (done) {
41 PS.lookup({pid: String(pid)}, function (err, list) {
42 assert.equal(list.length, 1);
43 assert.equal(list[0].arguments[0], serverPath);
44
45 done();
46 });
47 });
48
49 it('by command & arguments', function (done) {
50 PS.lookup({command: '.*(node|iojs).*', arguments: 'node_process_for_test'}, function (err, list) {
51 assert.equal(list.length, 1);
52 assert.equal(list[0].pid, pid);
53 assert.equal(list[0].arguments[0], serverPath);
54 done();
55 });
56 });
57
58 it('by arguments, the matching should be case insensitive ', function (done) {
59 PS.lookup({arguments: 'UPPER_CASE'}, function (err, list) {
60 assert.equal(list.length, 1);
61 assert.equal(list[0].pid, pid);
62 assert.equal(list[0].arguments[0], serverPath);
63
64 PS.lookup({arguments: 'upper_case'}, function (err, list) {
65 assert.equal(list.length, 1);
66 assert.equal(list[0].pid, pid);
67 assert.equal(list[0].arguments[0], serverPath);
68 done();
69 });
70 });
71 });
72
73 it('empty result list should be safe ', function (done) {
74 PS.lookup({command: 'NOT_EXIST', psargs: 'l'}, function (err, list) {
75 assert.equal(list.length, 0);
76 done();
77 });
78 });
79
80 it('should work correctly with options `aux`', function (done) {
81 PS.lookup({command: 'node', psargs: 'aux'}, function (err, list) {
82 assert.equal(list.length > 0, true);
83 list.forEach(function (row) {
84 assert.equal(/^\d+$/.test(row.pid), true);
85 });
86 done();
87 });
88 });
89 });
90
91 describe('#kill()', function () {
92
93 it('kill', function (done) {
94
95 PS.kill(pid, function (err) {
96 assert.equal(err, null);
97 PS.lookup({pid: String(pid)}, function (err, list) {
98 assert.equal(list.length, 0);
99 done();
100 });
101 });
102 });
103
104 it('should not throw an exception if the callback is undefined', function (done) {
105 assert.doesNotThrow(function () {
106 PS.kill(pid);
107 setTimeout(done, 400);
108 });
109 });
110 });
111});