1 import { ChildProcess, exec, fork } from 'child_process'
2 import { join } from 'path'
36 function flushAndRunMultipleServers (totalServers) {
40 return new Promise<ServerInfo[]>(res => {
41 function anotherServerDone (serverNumber, app) {
42 apps[serverNumber - 1] = app
44 if (i === totalServers) {
51 for (let j = 1; j <= totalServers; j++) {
52 // For the virtual buffer
54 runServer(j).then(app => anotherServerDone(j, app))
61 function flushTests () {
62 return new Promise<void>((res, rej) => {
63 return exec('npm run clean:server:test', err => {
64 if (err) return rej(err)
71 function runServer (serverNumber: number, configOverride?: Object) {
72 const server: ServerInfo = {
74 serverNumber: serverNumber,
75 url: `http://localhost:${9000 + serverNumber}`,
76 host: `localhost:${9000 + serverNumber}`,
87 // These actions are async so we need to be sure that they have both been done
88 const serverRunString = {
89 'Server listening on port': false
91 const key = 'Database peertube_test' + serverNumber + ' is ready'
92 serverRunString[key] = false
95 client_id: 'Client id: (.+)',
96 client_secret: 'Client secret: (.+)',
97 user_username: 'Username: (.+)',
98 user_password: 'User password: (.+)'
101 // Share the environment
102 const env = Object.create(process.env)
103 env['NODE_ENV'] = 'test'
104 env['NODE_APP_INSTANCE'] = serverNumber.toString()
106 if (configOverride !== undefined) {
107 env['NODE_CONFIG'] = JSON.stringify(configOverride)
116 return new Promise<ServerInfo>(res => {
117 server.app = fork(join(__dirname, '..', '..', '..', '..', 'dist', 'server.js'), [], options)
118 server.app.stdout.on('data', function onStdout (data) {
119 let dontContinue = false
121 // Capture things if we want to
122 for (const key of Object.keys(regexps)) {
123 const regexp = regexps[key]
124 const matches = data.toString().match(regexp)
125 if (matches !== null) {
126 if (key === 'client_id') server.client.id = matches[1]
127 else if (key === 'client_secret') server.client.secret = matches[1]
128 else if (key === 'user_username') server.user.username = matches[1]
129 else if (key === 'user_password') server.user.password = matches[1]
133 // Check if all required sentences are here
134 for (const key of Object.keys(serverRunString)) {
135 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
136 if (serverRunString[key] === false) dontContinue = true
139 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
140 if (dontContinue === true) return
142 server.app.stdout.removeListener('data', onStdout)
148 function killallServers (servers: ServerInfo[]) {
149 for (const server of servers) {
150 process.kill(-server.app.pid)
154 // ---------------------------------------------------------------------------
158 flushAndRunMultipleServers,