def test_stop_standing_subproc_and_descendants(self): # Creates subprocess A with descendants looks like: # subprocess A # ├─ B (child) # │ ├─ X (grandchild) # │ │ ├─ 1 (great grandchild) # │ │ └─ 2 (great grandchild) # │ └─ Y (grandchild) # ├─ C (child) # └─ D (child) process_tree_args = ('subprocess_a', [ ('child_b', [ ('grand_child_x', [ ('great_grand_child_1', []), ('great_grand_child_2', []), ]), ('grand_child_y', []), ]), ('child_c', []), ('child_d', []), ]) subprocess_a = multiprocessing.Process(target=_fork_children_processes, args=process_tree_args) subprocess_a.start() mock_subprocess_a_popen = mock.MagicMock() mock_subprocess_a_popen.pid = subprocess_a.pid # Sleep a while to create all processes. time.sleep(0.01) utils.stop_standing_subprocess(mock_subprocess_a_popen) subprocess_a.join(timeout=1) mock_subprocess_a_popen.wait.assert_called_once()
def _stop_server(self): """Releases all the resources acquired in `start_server`. Raises: android_device_lib_errors.DeviceError: if the server exited with errors on the device side. """ # Although killing the snippet server would abort this subprocess anyway, we # want to call stop_standing_subprocess() to perform a health check, # print the failure stack trace if there was any, and reap it from the # process table. Note that it's much more important to ensure releasing all # the allocated resources on the host side than on the remote device side. # Stop the standing server subprocess running on the host side. if self._proc: utils.stop_standing_subprocess(self._proc) self._proc = None # Send the stop signal to the server running on the device side. out = self._adb.shell( _STOP_CMD.format( snippet_package=self.package, user=self._get_user_command_string())).decode('utf-8') if 'OK (0 tests)' not in out: raise android_device_lib_errors.DeviceError( self._device, f'Failed to stop existing apk. Unexpected output: {out}.')
def stop_adb_logcat(self): """Stops the adb logcat collection subprocess. """ if not self._adb_logcat_process: raise DeviceError(self, 'No ongoing adb logcat collection found.') utils.stop_standing_subprocess(self._adb_logcat_process) self._adb_logcat_process = None
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess([self.sleep_cmd, '4']) p1 = psutil.Process(p.pid) start_time = time.time() utils.stop_standing_subprocess(p) self.assertFalse(p1.is_running()) stop_time = time.time() self.assertTrue(stop_time - start_time < 0.1)
def stop_capture(self): """See base class documentation """ if self._process is None: raise sniffer.InvalidOperationError( "Trying to stop a non-started process") utils.stop_standing_subprocess(self._process) self._post_process()
def stop(self): """Stops the adb logcat service.""" if not self._adb_logcat_process: return try: utils.stop_standing_subprocess(self._adb_logcat_process) except: self._ad.log.exception('Failed to stop adb logcat.') self._adb_logcat_process = None
def stop_adb_logcat(self): """Stops the adb logcat collection subprocess. """ if not self.is_adb_logcat_on: raise Error( "Android device %s does not have an ongoing adb logcat collection." % self.serial) utils.stop_standing_subprocess(self.adb_logcat_process) self.adb_logcat_process = None
def _stop(self): """Stops the background process for logcat.""" if not self._adb_logcat_process: return try: utils.stop_standing_subprocess(self._adb_logcat_process) except Exception: self._ad.log.exception('Failed to stop adb logcat.') self._adb_logcat_process = None
def stop_app(self): """Overrides superclass.""" # Kill the pending 'adb shell am instrument -w' process if there is one. # Although killing the snippet apk would abort this process anyway, we # want to call stop_standing_subprocess() to perform a health check, # print the failure stack trace if there was any, and reap it from the # process table. if self._proc: utils.stop_standing_subprocess(self._proc) self.log.debug('Stopping snippet apk %s', self.package) out = self._adb.shell(_STOP_CMD % self.package).decode('utf-8') if 'OK (0 tests)' not in out: raise Error('Failed to stop existing apk. Unexpected output: %s' % out)
def stop_app(self): # Kill the pending 'adb shell am instrument -w' process if there is one. # Although killing the snippet apk would abort this process anyway, we # want to call stop_standing_subprocess() to perform a health check, # print the failure stack trace if there was any, and reap it from the # process table. self.log.debug('Stopping snippet apk %s', self.package) try: # Close the socket connection. self.disconnect() if self._proc: utils.stop_standing_subprocess(self._proc) out = self._adb.shell(_STOP_CMD % self.package).decode('utf-8') if 'OK (0 tests)' not in out: raise Error('Failed to stop existing apk. Unexpected ' 'output: %s' % out) finally: # Always clean up the adb port if self.host_port: self._adb.forward(['--remove', 'tcp:%d' % self.host_port])
def stop_app(self): # Kill the pending 'adb shell am instrument -w' process if there is one. # Although killing the snippet apk would abort this process anyway, we # want to call stop_standing_subprocess() to perform a health check, # print the failure stack trace if there was any, and reap it from the # process table. self.log.debug('Stopping snippet apk %s', self.package) # Close the socket connection. self.disconnect() if self._proc: utils.stop_standing_subprocess(self._proc) self._proc = None out = self._adb.shell( _STOP_CMD.format( snippet_package=self.package, user=self._get_user_command_string())).decode('utf-8') if 'OK (0 tests)' not in out: raise errors.DeviceError( self._ad, 'Failed to stop existing apk. Unexpected output: %s' % out)
def test_stop_standing_subproc_wihtout_pipe(self): p = subprocess.Popen(self.sleep_cmd(4)) self.assertIsNone(p.stdout) p1 = psutil.Process(p.pid) utils.stop_standing_subprocess(p) self.assertFalse(p1.is_running())
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess(self.sleep_cmd(4)) p1 = psutil.Process(p.pid) utils.stop_standing_subprocess(p) self.assertFalse(p1.is_running())
def test_stop_standing_subproc_already_dead(self): p = utils.start_standing_subprocess(['sleep', '0']) time.sleep(0.5) with self.assertRaisesRegexp(utils.Error, 'Process .* has terminated'): utils.stop_standing_subprocess(p)
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess(['sleep', '5']) utils.stop_standing_subprocess(p) self.assertIsNotNone(p.poll())
def stop(self): if self.started: utils.stop_standing_subprocess(self.iperf_process) self.started = False
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess("sleep 0") time.sleep(0.1) with self.assertRaisesRegexp(utils.Error, "Process .* has terminated"): utils.stop_standing_subprocess(p)
def test_stop_standing_subproc(self): p = utils.start_standing_subprocess(self.sleep_cmd(4)) utils.stop_standing_subprocess(p) self.assertFalse(_is_process_running(p.pid))
def test_stop_standing_subproc_without_pipe(self): p = subprocess.Popen(self.sleep_cmd(4)) self.assertIsNone(p.stdout) utils.stop_standing_subprocess(p) self.assertFalse(_is_process_running(p.pid))