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

add mechanism to specify maximal duration of tests #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion cavy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function test(cmd) {
const outputAsXml = cmd.xml;
const dev = cmd.dev;
const bootTimeout = cmd.bootTimeout;
runTests(commandName, entryFile, skipbuild, dev, outputAsXml, bootTimeout, args);
const allTestRunTimeout = cmd.allTestRunTimeout;
runTests(commandName, entryFile, skipbuild, dev, outputAsXml, bootTimeout, allTestRunTimeout, args);
}

// Stop quitting unless we want to
Expand Down Expand Up @@ -63,6 +64,11 @@ program
'Set how long the CLI should wait for the RN app to boot '
+ '(is ignored if used with --skipbuild, defaults to 2 minutes, requires Cavy 4.0.0)'
)
.option(
'-t, --allTestRunTimeout <minutes>',
'Set how long the CLI should wait for the RN app for finishing tests '
+ '(defaults to 2 minutes, requires Cavy 4.0.0)'
)
.option('--xml', 'Write out test results to cavy_results.xml (requires Cavy 3.3.0)')
.allowUnknownOption()
.action(cmd => test(cmd));
Expand All @@ -81,6 +87,12 @@ program
'Set how long the CLI should wait for the RN app to boot '
+ '(is ignored if used with --skipbuild, defaults to 2 minutes, requires Cavy 4.0.0)'
)
.option(
'-t, --allTestRunTimeout <minutes>',
'Set how long the CLI should wait for the RN app for finishing tests '
+ '(defaults to 2 minutes, requires Cavy 4.0.0)'
)

.option('--xml', 'Write out test results to cavy_results.xml (requires Cavy 3.3.0)')
.allowUnknownOption()
.action(cmd => test(cmd));
Expand Down
10 changes: 8 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const server = http.createServer();
// Setup local variables for server
server.locals = {
appBooted: false,
testCount: 0
testCount: 0,
testFinished: false
};

// Initialize a WebSocket Server instance
Expand All @@ -27,9 +28,14 @@ wss.on('connection', socket => {
case 'singleResult':
logTestResult(json.data);
break;
case 'testingComplete':
case 'testingComplete':{
server.locals.testFinished = true;
finishTesting(json.data);
break;
}
default: {
console.log(json.event);
}
}
});

Expand Down
20 changes: 16 additions & 4 deletions src/runTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { spawn, execFileSync } = require('child_process');

// Default boot timeout in minutes
const BOOT_TIMEOUT = 2;
const ALL_TESTS_RUN_TIMEOUT = 2;

let switched = false;

Expand Down Expand Up @@ -57,7 +58,7 @@ function getAdbPath() {
}

// Start test server, listening for test results to be posted.
function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }) {
function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout, allTestRunTimeout }) {
server.locals.dev = dev;
server.locals.outputAsXml = outputAsXml;
server.listen(8082, () => {
Expand All @@ -73,6 +74,7 @@ function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }) {
} else {
// bootTimeout defaults to two minutes
const timeout = bootTimeout || BOOT_TIMEOUT;
const testsTimeout = allTestRunTimeout || ALL_TESTS_RUN_TIMEOUT
setTimeout(() => {
if (!server.locals.appBooted) {
console.log(`No response from Cavy within ${timeout} minutes.`);
Expand All @@ -81,6 +83,16 @@ function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }) {
}
// Convert bootTimeout to milliseconds
}, minsToMillisecs(timeout));
setTimeout(() => {
if (!server.locals.testFinished) {
console.log(`No response from Cavy within ${allTestRunTimeout} minutes.`);
console.log('Terminating processes.');
process.exit(1);
} else {
console.log("Finished")
}
// Convert testsTimeout to milliseconds
}, minsToMillisecs(testsTimeout));
}
});
}
Expand All @@ -93,7 +105,7 @@ function runServer({ command, dev, outputAsXml, skipbuild, bootTimeout }) {
// outputAsXml: whether to write and save the results to XML file
// bootTimeout: how long the CLI should wait for the RN app to boot.
// args: any extra arguments the user would usually to pass to `react native run...`
function runTests(command, file, skipbuild, dev, outputAsXml, bootTimeout, args) {
function runTests(command, file, skipbuild, dev, outputAsXml, bootTimeout, allTestRunTimeout, args) {

// Assume entry file is 'index.js' if user doesn't supply one.
const entryFile = file || 'index.js';
Expand Down Expand Up @@ -134,7 +146,7 @@ function runTests(command, file, skipbuild, dev, outputAsXml, bootTimeout, args)
});

if (skipbuild) {
runServer({ command, dev, outputAsXml, skipbuild, bootTimeout });
runServer({ command, dev, outputAsXml, skipbuild, bootTimeout, allTestRunTimeout});
} else {
// Build the app, start the test server and wait for results.
console.log(`cavy: Running \`npx react-native ${command}\`...`);
Expand All @@ -151,7 +163,7 @@ function runTests(command, file, skipbuild, dev, outputAsXml, bootTimeout, args)
if (code) {
return process.exit(code);
}
runServer({ command, dev, outputAsXml, skipbuild, bootTimeout });
runServer({ command, dev, outputAsXml, skipbuild, bootTimeout, allTestRunTimeout });
});
}
}
Expand Down