Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
- runServerRemote script now validated that 'jx' is running
Browse files Browse the repository at this point in the history
- tasker/android.js improve error handling for runAndroidApp method
- tasker/______.js improvements based on the review
  • Loading branch information
larryonoff committed Dec 23, 2016
1 parent 3ed74db commit b9f52a3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 49 deletions.
93 changes: 55 additions & 38 deletions tasker/______.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,61 @@ var eopts = {
killSignal: 'SIGTERM'
};

var errors = [
'Unidentified target argument',
'Unknown information received by IS manager',
'No android test node available',
'jx install failed',
'index.js failed'
];

function logErrorAndExit(code, args) {
var message = errors[code-1];
if (message) {
console.error(errors[code-1], args);
var errors = {
unidentifiedTargetArgument: {
message: 'Unidentified target argument',
code: 1
},
unknownInformationReceivedByISManager: {
message: 'Unknown information received by IS manager',
code: 2
},
androidNodeNotAvailable: {
message: 'No android test node available',
code: 3
},
jxInstallFailed: {
message: 'jx install failed',
code: 4
},
indexJsFailed: {
message: 'index.js failed',
code: 5
}
};

function logAndExit(error, args) {
if (error) {
console.error(error.message, args);

process.exit(error.code);
} else {
console.error(args);
}

process.exit(code);
process.exit(-1);
}
}

var target = process.argv[2];

if (!(target == "ios" || target == "android" || target == "all")) {
logErrorAndExit(1, [target]);
if (!(target === 'ios' || target === 'android' || target === 'all')) {
logAndExit(errors.unidentifiedTargetArgument, [target]);
}

var targets = {};
if (target == "ios") {
targets["ios"] = 0;
} else if (target == "android") {
targets["android"] = 0;
if (target === 'ios') {
targets.ios = 0;
} else if (target === 'android') {
targets.android = 0;
targets.nodes = 0;
targets.droid = 0;
} else {
targets["ios"] = 0;
targets["android"] = 0;
targets.ios = 0;
targets.android = 0;
targets.nodes = 0;
targets.droid = 0;
}
targets.cancel=1;
targets.cancel = 1;

var http = require('http');
var lock_me = false;
Expand All @@ -61,7 +77,7 @@ var checkIt = function(){
var no_exit = false;
for (var o in targets) {
if (targets.hasOwnProperty(o)) {
if (targets[o] == 0) {
if (targets[o] === 0) {
no_exit = true;
break;
}
Expand All @@ -71,39 +87,38 @@ var checkIt = function(){
if (!no_exit) {
lock_me = true;
var obj = {devices: targets};
console.log("IS Running:");
console.log('IS Running:');

console.log('Running \'jx install\'');
var out = execSync('cd ' + __dirname + '; jx install');

if (out.exitCode != 0) {
console.log(out.out, "\n");
if (out.exitCode !== 0) {
// If jx install fails, we can't run the server
// and without the server, the tests wouldn't run
logErrorAndExit(3, [out.exitCode, out.out]);
logAndExit(errors.jxInstallFailed, [out.exitCode, out.out]);
} else {
console.log("Skipping the log for NPM since the exitCode was 0");
console.log('Skipping the log for NPM since the exitCode was 0');
}

obj.devices.cancel = 0;
delete obj.devices.cancel;

console.log(">", process.argv[0] + " index.js " + JSON.stringify(obj));
console.log('>', process.argv[0] + ' index.js ' + JSON.stringify(obj));

// give a small break for a potential kill SIGNAL from server
setTimeout(function(){
var child = spawn(process.argv[0], ["index.js", JSON.stringify(obj)], eopts);
var child = spawn(process.argv[0], ['index.js', JSON.stringify(obj)], eopts);

child.stdout.on('data', function (data) {
console.log(data+"");
console.log(data+'');
});

child.stderr.on('data', function (data) {
console.error(data+"");
console.error(data+'');
});

child.on('exit', function (code) {
logErrorAndExit(4, [code]);
logAndExit(errors.indexJsFailed, [code]);
});
}, 500);
}
Expand All @@ -115,11 +130,13 @@ http.createServer(function (req, res) {
if (!lock_me) {
var url = req.url.substr(1).split('=');
if (url.length < 2) {
logErrorAndExit(2, [req.url]);
logAndExit(errors.unknownInformationReceivedByISManager, [req.url]);
}

if (!targets.hasOwnProperty(url[0]) || isNaN(parseInt(url[1])) || parseInt(url[1]) == 0) {
logErrorAndExit(2, [req.url]);
if (!targets.hasOwnProperty(url[0]) ||
isNaN(parseInt(url[1])) ||
parseInt(url[1]) === 0) {
logAndExit(errors.unknownInformationReceivedByISManager, [req.url]);
}

var name = url[0];
Expand All @@ -134,7 +151,7 @@ http.createServer(function (req, res) {
targets.droid = 0;

if (targets.nodes === 0) {
logErrorAndExit(3);
logAndExit(errors.androidNodeNotAvailable);
}
} else {
targets.nodes--;
Expand Down
26 changes: 18 additions & 8 deletions tasker/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,31 @@ var runAndroidApp = function (class_name, deviceId, deviceName, cb) {
var lg = new grabLogcat(class_name, deviceId, deviceName, function (err, _this) {
logcatIndex++;
if (!err) {
var cmd = 'adb -s "' + deviceId + '" shell am start -n ' + class_name + "/" + class_name + ".MainActivity";
var cmd = 'adb -s "' + deviceId + '" shell am start -n ' +
class_name + '/' + class_name + '.MainActivity';
var res = sync(cmd);
if (res.exitCode != 0) {
var str = "\n" + res.out;
if (str.length > 512) str = str.substr(0, 512);
logme("Error: problem running Android apk(" + class_name + ") on device " + deviceName, str, "");
if (res.exitCode !== 0 ||
res.out.indexOf('Error') !== -1) {
var str = '\n' + res.out;
if (str.length > 512) {
str = str.substr(0, 512);
}

logme('Error: problem running Android apk(' +
class_name + ') on device ' + deviceName, str, '');

if (_this) {
_this.child.kill();
}

if (_this) _this.child.kill();
cb(true, null);
return false;
}
logme("App was succesfully started on " + deviceId + "\n");

logme('App was succesfully started on ' + deviceId + '\n');
cb(null);
} else {
cb(err)
cb(err);
}
});
lg.run();
Expand Down
6 changes: 3 additions & 3 deletions tasker/runServerRemote__.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ trap 'log_error $LINENO' ERR
### END - JXcore Test Server --------

cd ~/Test/{{SERVER_LOCATION}}
# I don't know the reason why it fails
# so we're skipping errors for 'sudo pkill jx'
# MUST be fixed
# killing JXcore if it already running
# ignoring errors here since
# pkill returns an error when process not found
! sudo pkill jx
sudo jx ______.js {{TARGET}}

0 comments on commit b9f52a3

Please sign in to comment.