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

Emulator wrapper fixes #702

Merged
merged 3 commits into from
May 3, 2018
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
62 changes: 36 additions & 26 deletions detox/src/devices/android/Emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,55 @@ class Emulator {
}

async boot(emulatorName) {
const headless = argparse.getArgValue('headless') ? '-no-window' : '';
const cmd = `-verbose -gpu host -no-audio ${headless} @${emulatorName}`;
log.verbose(this.emulatorBin, cmd);
const emulatorArgs = _.compact([
'-verbose',
'-gpu', 'auto',
'-no-audio',
argparse.getArgValue('headless') ? '-no-window' : '',
`@${emulatorName}`
]);

let childProcessOutput;
const tempLog = `./${emulatorName}.log`;
const stdout = fs.openSync(tempLog, 'a');
const stderr = fs.openSync(tempLog, 'a');
const tail = new Tail(tempLog);
const promise = spawn(this.emulatorBin, _.split(cmd, ' '), {detached: true, stdio: ['ignore', stdout, stderr]});

const childProcess = promise.childProcess;
childProcess.unref();

tail.on("line", function(data) {
if (data.includes('Adb connected, start proxing data')) {
detach();
}
if (data.includes(`There's another emulator instance running with the current AVD`)) {
detach();
const tail = new Tail(tempLog).on("line", (line) => {
if (line.includes('Adb connected, start proxing data')) {
childProcessPromise._cpResolve();
}
});

tail.on("error", function(error) {
detach();
log.verbose('Emulator stderr: ', error);
});
function detach() {
if (childProcessOutput) {
return;
}

promise.catch(function(err) {
log.error('Emulator ERROR: ', err);
});
childProcessOutput = fs.readFileSync(tempLog, 'utf8');

function detach() {
tail.unwatch();
fs.closeSync(stdout);
fs.closeSync(stderr);
fs.unlink(tempLog, () => {});
promise._cpResolve();
fs.unlink(tempLog, _.noop);
}

return promise;
log.verbose(this.emulatorBin, ...emulatorArgs);
const childProcessPromise = spawn(this.emulatorBin, emulatorArgs, { detached: true, stdio: ['ignore', stdout, stderr] });
childProcessPromise.childProcess.unref();

return childProcessPromise.catch((err) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this be separated into two declarations ?

childProcessPromise.catch((err) => {
}
....
return childProcessPromise;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is completely different logic then. What do you mean?
screenshot from 2018-05-02 15-14-50

detach();

if (childProcessOutput.includes(`There's another emulator instance running with the current AVD`)) {
return;
}

log.error('ChildProcessError', '%s', err.message);
log.error('stderr', '%s', childProcessOutput);
throw err;
}).then(() => {
detach();
log.verbose('stdout', '%s', childProcessOutput);
});
}
}

Expand Down