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 9, 2019
1 parent 3eb4d59 commit 9f5e54d
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion .scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,37 @@ const path = require('path');
const fs = require('fs');
const extend = require('util')._extend;
const { exec } = 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',
},
};

Expand Down Expand Up @@ -57,7 +78,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 @@ -67,6 +95,7 @@ function startApp(callback) {
startProcess({
name: 'Meteor App',
command: 'node /tmp/build-test/bundle/main.js',
// command: 'node .meteor/local/build/main.js',
waitForMessage: appOptions.waitForMessage,
options: {
cwd: srcDir,
Expand All @@ -79,6 +108,7 @@ function startChimp() {
startProcess({
name: 'Chimp',
command: 'npm run chimp-test',
// command: 'exit 2',
options: {
env: Object.assign({}, process.env, {
NODE_PATH: `${ process.env.NODE_PATH +
Expand Down

0 comments on commit 9f5e54d

Please sign in to comment.