Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait port release to finish tests #14066

Merged
merged 2 commits into from
Apr 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions .scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,45 @@
const path = require('path');
const fs = require('fs');
const extend = require('util')._extend;
const { exec } = require('child_process');
const { spawn } = require('child_process');
const net = require('net');
const processes = [];
let exitCode;

const baseDir = path.resolve(__dirname, '..');
const srcDir = path.resolve(baseDir);

const isPortTaken = (port) => new Promise((resolve, reject) => {
const tester = net.createServer()
.once('error', (err) => (err.code === 'EADDRINUSE' ? resolve(true) : reject(err)))
.once('listening', () => tester.once('close', () => resolve(false)).close())
.listen(port);
});

const waitPortRelease = (port) => new Promise((resolve, reject) => {
isPortTaken(port).then((taken) => {
if (!taken) {
return resolve();
}
setTimeout(() => {
waitPortRelease(port).then(resolve).catch(reject);
}, 1000);
});
});

const appOptions = {
env: {
PORT: 3000,
ROOT_URL: 'http://localhost:3000',
// MONGO_URL: 'mongodb://localhost:27017/test',
// MONGO_OPLOG_URL: 'mongodb://localhost:27017/local',
},
};

function startProcess(opts, callback) {
const proc = exec(
const proc = spawn(
opts.command,
opts.params,
opts.options
);

Expand Down Expand Up @@ -57,7 +79,14 @@ function startProcess(opts, callback) {
processes.forEach((p) => p.kill());

if (processes.length === 0) {
process.exit(exitCode);
waitPortRelease(appOptions.env.PORT).then(() => {
console.log(`Port ${ appOptions.env.PORT } was released, exiting with code ${ exitCode }`);
process.exit(exitCode);
}).catch((error) => {
console.error(`Error waiting port ${ appOptions.env.PORT } to be released, exiting with code ${ exitCode }`);
console.error(error);
process.exit(exitCode);
});
}
});
processes.push(proc);
Expand All @@ -66,7 +95,10 @@ function startProcess(opts, callback) {
function startApp(callback) {
startProcess({
name: 'Meteor App',
command: 'node /tmp/build-test/bundle/main.js',
command: 'node',
params: ['/tmp/build-test/bundle/main.js'],
// command: 'node',
// params: ['.meteor/local/build/main.js'],
waitForMessage: appOptions.waitForMessage,
options: {
cwd: srcDir,
Expand All @@ -78,7 +110,10 @@ function startApp(callback) {
function startChimp() {
startProcess({
name: 'Chimp',
command: 'npm run chimp-test',
command: 'npm',
params: ['run', 'chimp-test'],
// command: 'exit',
// params: ['2'],
options: {
env: Object.assign({}, process.env, {
NODE_PATH: `${ process.env.NODE_PATH +
Expand Down