From 7c3b79768bd174b22154e8d2df0b1211e01ee56a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 24 Apr 2019 15:10:37 +0200 Subject: Use test wrapper exit function --- shared/core-utils/miscs/miscs.ts | 7 ++++ shared/extra-utils/miscs/email-child-process.js | 2 +- shared/extra-utils/miscs/email.ts | 12 ++++-- shared/extra-utils/server/servers.ts | 51 ++++++++++++++++++------- 4 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 shared/core-utils/miscs/miscs.ts (limited to 'shared') diff --git a/shared/core-utils/miscs/miscs.ts b/shared/core-utils/miscs/miscs.ts new file mode 100644 index 000000000..c668e44c1 --- /dev/null +++ b/shared/core-utils/miscs/miscs.ts @@ -0,0 +1,7 @@ +function randomInt (low: number, high: number) { + return Math.floor(Math.random() * (high - low) + low) +} + +export { + randomInt +} diff --git a/shared/extra-utils/miscs/email-child-process.js b/shared/extra-utils/miscs/email-child-process.js index 40ae37d70..088a5a08c 100644 --- a/shared/extra-utils/miscs/email-child-process.js +++ b/shared/extra-utils/miscs/email-child-process.js @@ -6,7 +6,7 @@ process.on('message', (msg) => { if (msg.start) { const maildev = new MailDev({ ip: '127.0.0.1', - smtp: 1025, + smtp: msg.port, disableWeb: true, silent: true }) diff --git a/shared/extra-utils/miscs/email.ts b/shared/extra-utils/miscs/email.ts index f9f1bd95b..b2a1093da 100644 --- a/shared/extra-utils/miscs/email.ts +++ b/shared/extra-utils/miscs/email.ts @@ -1,4 +1,6 @@ -import { fork, ChildProcess } from 'child_process' +import { ChildProcess, fork } from 'child_process' +import { randomInt } from '../../core-utils/miscs/miscs' +import { parallelTests } from '../server/servers' class MockSmtpServer { @@ -20,7 +22,9 @@ class MockSmtpServer { } collectEmails (emailsCollection: object[]) { - return new Promise((res, rej) => { + return new Promise((res, rej) => { + const port = parallelTests() ? randomInt(1000, 2000) : 1025 + if (this.started) { this.emails = emailsCollection return res() @@ -28,7 +32,7 @@ class MockSmtpServer { // ensure maildev isn't started until // unexpected exit can be reported to test runner - this.emailChildProcess.send({ start: true }) + this.emailChildProcess.send({ start: true, port }) this.emailChildProcess.on('exit', () => { return rej(new Error('maildev exited unexpectedly, confirm port not in use')) }) @@ -38,7 +42,7 @@ class MockSmtpServer { } this.started = true this.emails = emailsCollection - return res() + return res(port) }) }) } diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 3ef4b9746..ed41bfa48 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -3,10 +3,11 @@ import { ChildProcess, exec, fork } from 'child_process' import { join } from 'path' import { root, wait } from '../miscs/miscs' -import { readdir, readFile } from 'fs-extra' +import { copy, readdir, readFile, remove } from 'fs-extra' import { existsSync } from 'fs' import { expect } from 'chai' import { VideoChannel } from '../../models/videos' +import { randomInt } from '../../core-utils/miscs/miscs' interface ServerInfo { app: ChildProcess, @@ -29,6 +30,8 @@ interface ServerInfo { email?: string } + customConfigFile?: string + accessToken?: string videoChannel?: VideoChannel @@ -49,6 +52,10 @@ interface ServerInfo { videos?: { id: number, uuid: string }[] } +function parallelTests () { + return process.env.MOCHA_PARALLEL === 'true' +} + function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) { let apps = [] let i = 0 @@ -84,23 +91,23 @@ function randomServer () { const low = 10 const high = 10000 - return Math.floor(Math.random() * (high - low) + low) + return randomInt(low, high) } async function flushAndRunServer (serverNumber: number, configOverride?: Object, args = []) { - const parallel = process.env.MOCHA_PARALLEL === 'true' + const parallel = parallelTests() const internalServerNumber = parallel ? randomServer() : serverNumber const port = 9000 + internalServerNumber - await flushTests(serverNumber) + await flushTests(internalServerNumber) const server: ServerInfo = { app: null, port, internalServerNumber, parallel, - serverNumber: internalServerNumber, + serverNumber, url: `http://localhost:${port}`, host: `localhost:${port}`, client: { @@ -116,7 +123,7 @@ async function flushAndRunServer (serverNumber: number, configOverride?: Object, return runServer(server, configOverride, args) } -function runServer (server: ServerInfo, configOverrideArg?: any, args = []) { +async function runServer (server: ServerInfo, configOverrideArg?: any, args = []) { // These actions are async so we need to be sure that they have both been done const serverRunString = { 'Server listening': false @@ -131,15 +138,19 @@ function runServer (server: ServerInfo, configOverrideArg?: any, args = []) { user_password: 'User password: (.+)' } - // Share the environment - const env = Object.create(process.env) - env['NODE_ENV'] = 'test' - env['NODE_APP_INSTANCE'] = server.serverNumber.toString() + if (server.internalServerNumber !== server.serverNumber) { + const basePath = join(root(), 'config') + + const tmpConfigFile = join(basePath, `test-${server.internalServerNumber}.yaml`) + await copy(join(basePath, `test-${server.serverNumber}.yaml`), tmpConfigFile) + + server.customConfigFile = tmpConfigFile + } - let configOverride: any = {} + const configOverride: any = {} if (server.parallel) { - configOverride = { + Object.assign(configOverride, { listen: { port: server.port }, @@ -165,18 +176,22 @@ function runServer (server: ServerInfo, configOverrideArg?: any, args = []) { admin: { email: `admin${server.internalServerNumber}@example.com` } - } + }) } if (configOverrideArg !== undefined) { Object.assign(configOverride, configOverrideArg) } + // Share the environment + const env = Object.create(process.env) + env['NODE_ENV'] = 'test' + env['NODE_APP_INSTANCE'] = server.internalServerNumber.toString() env['NODE_CONFIG'] = JSON.stringify(configOverride) const options = { silent: true, - env: env, + env, detached: true } @@ -244,7 +259,10 @@ async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) { function killallServers (servers: ServerInfo[]) { for (const server of servers) { + if (!server.app) continue + process.kill(-server.app.pid) + server.app = null } } @@ -256,6 +274,10 @@ function cleanupTests (servers: ServerInfo[]) { if (server.parallel) { p.push(flushTests(server.internalServerNumber)) } + + if (server.customConfigFile) { + p.push(remove(server.customConfigFile)) + } } return Promise.all(p) @@ -280,6 +302,7 @@ export { checkDirectoryIsEmpty, checkTmpIsEmpty, ServerInfo, + parallelTests, cleanupTests, flushAndRunMultipleServers, flushTests, -- cgit v1.2.3