3 const childProcess
= require('child_process')
4 const exec
= childProcess
.exec
5 const fork
= childProcess
.fork
6 const pathUtils
= require('path')
9 flushAndRunMultipleServers: flushAndRunMultipleServers
,
10 flushTests: flushTests
,
14 // ---------------------- Export functions --------------------
16 function flushAndRunMultipleServers (totalServers
, serversRun
) {
21 function anotherServerDone (number
, app
, url
) {
22 apps
[number
- 1] = app
23 urls
[number
- 1] = url
25 if (i
=== totalServers
) {
26 serversRun(apps
, urls
)
30 flushTests(function () {
31 for (let j
= 1; j
<= totalServers
; j
++) {
32 // For the virtual buffer
33 setTimeout(function () {
34 runServer(j
, function (app
, url
) {
35 anotherServerDone(j
, app
, url
)
42 function flushTests (callback
) {
43 exec('npm run clean:server:test', callback
)
46 function runServer (number
, callback
) {
49 url: `http://localhost:${9000 + number}`,
60 // These actions are async so we need to be sure that they have both been done
61 const serverRunString
= {
62 'Connected to mongodb': false,
63 'Server listening on port': false
67 client_id: 'Client id: ([a-f0-9]+)',
68 client_secret: 'Client secret: (.+)',
69 user_username: 'Username: (.+)',
70 user_password: 'User password: (.+)'
73 // Share the environment
74 const env
= Object
.create(process
.env
)
76 env
.NODE_APP_INSTANCE
= number
83 server
.app
= fork(pathUtils
.join(__dirname
, '../../../server.js'), [], options
)
84 server
.app
.stdout
.on('data', function onStdout (data
) {
85 let dontContinue
= false
87 // Capture things if we want to
88 for (const key
of Object
.keys(regexps
)) {
89 const regexp
= regexps
[key
]
90 const matches
= data
.toString().match(regexp
)
91 if (matches
!== null) {
92 if (key
=== 'client_id') server
.client
.id
= matches
[1]
93 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
94 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
95 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
99 // Check if all required sentences are here
100 for (const key
of Object
.keys(serverRunString
)) {
101 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
102 if (serverRunString
[key
] === false) dontContinue
= true
105 // If no, there is maybe one thing not already initialized (mongodb...)
106 if (dontContinue
=== true) return
108 server
.app
.stdout
.removeListener('data', onStdout
)
113 // ---------------------------------------------------------------------------
115 module
.exports
= serversUtils