3 const childProcess
= require('child_process')
4 const exec
= childProcess
.exec
5 const fork
= childProcess
.fork
6 const pathUtils
= require('path')
9 flushAndRunMultipleServers
,
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}`,
50 host: `localhost:${9000 + number}`,
61 // These actions are async so we need to be sure that they have both been done
62 const serverRunString
= {
63 'Server listening on port': false
65 const key
= 'Database peertube_test' + number
+ ' is ready'
66 serverRunString
[key
] = false
69 client_id: 'Client id: (.+)',
70 client_secret: 'Client secret: (.+)',
71 user_username: 'Username: (.+)',
72 user_password: 'User password: (.+)'
75 // Share the environment
76 const env
= Object
.create(process
.env
)
78 env
.NODE_APP_INSTANCE
= number
85 server
.app
= fork(pathUtils
.join(__dirname
, '..', '..', '..', 'server.js'), [], options
)
86 server
.app
.stdout
.on('data', function onStdout (data
) {
87 let dontContinue
= false
89 // Capture things if we want to
90 for (const key
of Object
.keys(regexps
)) {
91 const regexp
= regexps
[key
]
92 const matches
= data
.toString().match(regexp
)
93 if (matches
!== null) {
94 if (key
=== 'client_id') server
.client
.id
= matches
[1]
95 else if (key
=== 'client_secret') server
.client
.secret
= matches
[1]
96 else if (key
=== 'user_username') server
.user
.username
= matches
[1]
97 else if (key
=== 'user_password') server
.user
.password
= matches
[1]
101 // Check if all required sentences are here
102 for (const key
of Object
.keys(serverRunString
)) {
103 if (data
.toString().indexOf(key
) !== -1) serverRunString
[key
] = true
104 if (serverRunString
[key
] === false) dontContinue
= true
107 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
108 if (dontContinue
=== true) return
110 server
.app
.stdout
.removeListener('data', onStdout
)
115 // ---------------------------------------------------------------------------
117 module
.exports
= serversUtils