Skip to content

Commit

Permalink
Wait port release to finish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Apr 10, 2019
1 parent 3eb4d59 commit bd1ef98
Showing 1 changed file with 40 additions and 5 deletions.
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

0 comments on commit bd1ef98

Please sign in to comment.