diff options
Diffstat (limited to 'test/utils.js')
-rw-r--r-- | test/utils.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 000000000..69f43d731 --- /dev/null +++ b/test/utils.js | |||
@@ -0,0 +1,81 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var child_process = require('child_process') | ||
5 | var fork = child_process.fork | ||
6 | var exec = child_process.exec | ||
7 | |||
8 | function runMultipleServers (total_servers, serversRun) { | ||
9 | var apps = [] | ||
10 | var urls = [] | ||
11 | var i = 0 | ||
12 | |||
13 | function anotherServerDone (number, app, url) { | ||
14 | apps[number - 1] = app | ||
15 | urls[number - 1] = url | ||
16 | i++ | ||
17 | if (i === total_servers) { | ||
18 | serversRun(apps, urls) | ||
19 | } | ||
20 | } | ||
21 | |||
22 | flushTests(function () { | ||
23 | for (var j = 1; j <= total_servers; j++) { | ||
24 | (function (k) { // TODO: ES6 with let | ||
25 | // For the virtual buffer | ||
26 | setTimeout(function () { | ||
27 | runServer(k, function (app, url) { | ||
28 | anotherServerDone(k, app, url) | ||
29 | }) | ||
30 | }, 1000 * k) | ||
31 | })(j) | ||
32 | } | ||
33 | }) | ||
34 | } | ||
35 | |||
36 | function runServer (number, callback) { | ||
37 | var port = 9000 + number | ||
38 | var server_run_string = { | ||
39 | 'Connected to mongodb': false, | ||
40 | 'Server listening on port': false | ||
41 | } | ||
42 | |||
43 | // Share the environment | ||
44 | var env = Object.create(process.env) | ||
45 | env.NODE_ENV = 'test' | ||
46 | env.NODE_APP_INSTANCE = number | ||
47 | var options = { | ||
48 | silent: true, | ||
49 | env: env, | ||
50 | detached: true | ||
51 | } | ||
52 | |||
53 | var app = fork(__dirname + '/../server.js', [], options) | ||
54 | app.stdout.on('data', function onStdout (data) { | ||
55 | var dont_continue = false | ||
56 | // Check if all required sentences are here | ||
57 | for (var key of Object.keys(server_run_string)) { | ||
58 | if (data.toString().indexOf(key) !== -1) server_run_string[key] = true | ||
59 | if (server_run_string[key] === false) dont_continue = true | ||
60 | } | ||
61 | |||
62 | // If no, there is maybe one thing not already initialized (mongodb...) | ||
63 | if (dont_continue === true) return | ||
64 | |||
65 | app.stdout.removeListener('data', onStdout) | ||
66 | callback(app, 'http://localhost:' + port) | ||
67 | }) | ||
68 | } | ||
69 | |||
70 | function flushTests (callback) { | ||
71 | exec(__dirname + '/../scripts/clean_test.sh', function () { | ||
72 | callback() | ||
73 | }) | ||
74 | } | ||
75 | |||
76 | module.exports = { | ||
77 | runMultipleServers: runMultipleServers, | ||
78 | runServer: runServer, | ||
79 | flushTests: flushTests | ||
80 | } | ||
81 | })() | ||