diff --git a/lib/units/device/plugins/screen/stream.js b/lib/units/device/plugins/screen/stream.js index 8810533d..36b231da 100644 --- a/lib/units/device/plugins/screen/stream.js +++ b/lib/units/device/plugins/screen/stream.js @@ -325,39 +325,32 @@ module.exports = syrup.serial() var pid = this.banner ? this.banner.pid : -1 - function waitForEnd() { - var endListener - return new Promise(function(resolve/*, reject*/) { - output.expectEnd().on('end', endListener = function() { - resolve(true) - }) - }) - .finally(function() { - output.removeListener('end', endListener) - }) - } - function kill(signal) { if (pid <= 0) { return Promise.reject(new Error('Minicap service pid is unknown')) } - log.info('Sending SIGTERM to minicap') + var signum = { + 'SIGTERM': -15 + , 'SIGKILL': -9 + }[signal] + + log.info('Sending %s to minicap', signal) return Promise.all([ - waitForEnd() - , adb.shell(options.serial, ['kill', signal, pid]) - .then(adbkit.util.readAll) - .timeout(2000) - .return(true) - ]) + output.waitForEnd() + , adb.shell(options.serial, ['kill', signum, pid]) + .then(adbkit.util.readAll) + .return(true) + ]) + .timeout(2000) } function kindKill() { - return kill('-15') + return kill('SIGTERM') } function forceKill() { - return kill('-9') + return kill('SIGKILL') } function forceEnd() { diff --git a/lib/units/device/plugins/touch/index.js b/lib/units/device/plugins/touch/index.js index 412c7440..522bc346 100644 --- a/lib/units/device/plugins/touch/index.js +++ b/lib/units/device/plugins/touch/index.js @@ -337,39 +337,32 @@ module.exports = syrup.serial() var pid = this.banner ? this.banner.pid : -1 - function waitForEnd() { - var endListener - return new Promise(function(resolve/*, reject*/) { - output.expectEnd().on('end', endListener = function() { - resolve(true) - }) - }) - .finally(function() { - output.removeListener('end', endListener) - }) - } - function kill(signal) { if (pid <= 0) { return Promise.reject(new Error('Minitouch service pid is unknown')) } - log.info('Sending SIGTERM to minitouch') + var signum = { + 'SIGTERM': -15 + , 'SIGKILL': -9 + }[signal] + + log.info('Sending %s to minitouch', signal) return Promise.all([ - waitForEnd() - , adb.shell(options.serial, ['kill', signal, pid]) - .then(adbkit.util.readAll) - .timeout(2000) - .return(true) - ]) + output.waitForEnd() + , adb.shell(options.serial, ['kill', signum, pid]) + .then(adbkit.util.readAll) + .return(true) + ]) + .timeout(2000) } function kindKill() { - return kill('-15') + return kill('SIGTERM') } function forceKill() { - return kill('-9') + return kill('SIGKILL') } function forceEnd() { diff --git a/lib/util/riskystream.js b/lib/util/riskystream.js index 643fa172..0f8dae2b 100644 --- a/lib/util/riskystream.js +++ b/lib/util/riskystream.js @@ -1,5 +1,6 @@ var util = require('util') +var Promise = require('bluebird') var EventEmitter = require('eventemitter3').EventEmitter function RiskyStream(stream) { @@ -34,4 +35,27 @@ RiskyStream.prototype.expectEnd = function() { return this } +RiskyStream.prototype.waitForEnd = function() { + var stream = this.stream + , endListener + + this.expectEnd() + + return new Promise(function(resolve/*, reject*/) { + if (stream.ended) { + return resolve(true) + } + + stream.on('end', endListener = function() { + resolve(true) + }) + + // Make sure we actually have a chance to get the 'end' event. + stream.resume() + }) + .finally(function() { + stream.removeListener('end', endListener) + }) +} + module.exports = RiskyStream