1 import { ChildProcess, exec, fork } from 'child_process'
2 import { join } from 'path'
33 async function flushAndRunMultipleServers (totalServers) {
37 return new Promise<ServerInfo[]>(res => {
38 function anotherServerDone (serverNumber, app) {
39 apps[serverNumber - 1] = app
41 if (i === totalServers) {
48 for (let j = 1; j <= totalServers; j++) {
49 // For the virtual buffer
51 runServer(j).then(app => anotherServerDone(j, app))
58 function flushTests () {
59 return new Promise<void>((res, rej) => {
60 return exec('npm run clean:server:test', err => {
61 if (err) return rej(err)
68 function runServer (serverNumber: number) {
69 const server: ServerInfo = {
71 url: `http://localhost:${9000 + serverNumber}`,
72 host: `localhost:${9000 + serverNumber}`,
83 // These actions are async so we need to be sure that they have both been done
84 const serverRunString = {
85 'Server listening on port': false
87 const key = 'Database peertube_test' + serverNumber + ' is ready'
88 serverRunString[key] = false
91 client_id: 'Client id: (.+)',
92 client_secret: 'Client secret: (.+)',
93 user_username: 'Username: (.+)',
94 user_password: 'User password: (.+)'
97 // Share the environment
98 const env = Object.create(process.env)
99 env['NODE_ENV'] = 'test'
100 env['NODE_APP_INSTANCE'] = serverNumber.toString()
107 return new Promise<ServerInfo>(res => {
108 server.app = fork(join(__dirname, '..', '..', '..', 'dist', 'server.js'), [], options)
109 server.app.stdout.on('data', function onStdout (data) {
110 let dontContinue = false
112 // Capture things if we want to
113 for (const key of Object.keys(regexps)) {
114 const regexp = regexps[key]
115 const matches = data.toString().match(regexp)
116 if (matches !== null) {
117 if (key === 'client_id') server.client.id = matches[1]
118 else if (key === 'client_secret') server.client.secret = matches[1]
119 else if (key === 'user_username') server.user.username = matches[1]
120 else if (key === 'user_password') server.user.password = matches[1]
124 // Check if all required sentences are here
125 for (const key of Object.keys(serverRunString)) {
126 if (data.toString().indexOf(key) !== -1) serverRunString[key] = true
127 if (serverRunString[key] === false) dontContinue = true
130 // If no, there is maybe one thing not already initialized (client/user credentials generation...)
131 if (dontContinue === true) return
133 server.app.stdout.removeListener('data', onStdout)
139 function killallServers (servers: ServerInfo[]) {
140 for (const server of servers) {
141 process.kill(-server.app.pid)
145 // ---------------------------------------------------------------------------
149 flushAndRunMultipleServers,