def wait_for_end(): try: while True: print 'Waiting' if parent_process.poll() is not None: print 'Ended' return time.sleep(1) except Exception as e: print _traceback_str()
def get_response_object(self): if self.path.startswith('/injob'): return json.dumps(win32job.IsProcessInJob( win32api.GetCurrentProcess(), None), indent=3) if self.path.startswith('/job'): extended_info = win32job.QueryInformationJobObject( None, win32job.JobObjectExtendedLimitInformation) return json.dumps(extended_info, indent=3) if self.path.startswith('/close_file_and_delete_it'): try: if self.funkyserver._open_file_handle is not None: f = os.path.join(os.path.dirname(__file__), 'tmp', 'testfile.txt') logging.info("Closing test file handle") self.funkyserver._open_file_handle.close() self.funkyserver._open_file_handle = None assert os.path.exists(f) os.remove(f) assert not os.path.exists(f) return "OK" except Exception as e: logging.error( "Failed to close file handle and delete file: %s\n%s", e, _traceback_str()) return "FAIL" if self.path.startswith('/stop'): logging.info("Returning child_processes_terminated: %r", self.funkyserver.child_processes_terminated) return repr(self.funkyserver.child_processes_terminated) return "OK"
def wait_for_end(): try: while True: print('Waiting') if parent_process.poll() is not None: print('Ended') return time.sleep(1) except Exception as e: print(_traceback_str())
def tearDown(self): command_file = os.path.join(os.path.dirname(__file__), 'test', 'tmp', 'command.txt') if os.path.exists(command_file): os.remove(command_file) self.wait_for_parent_to_stop(5) #Now check that no processes are left over: start_time = time.time() processes_left_running = [] for pid_file in self.get_pid_files(): with open(pid_file, "r") as f: pid = f.read().strip() if pid: while self.process_exists_or_access_denied( int(pid)) and time.time() - start_time < 5: time.sleep(0.3) if self.process_exists_or_access_denied(int(pid)): processes_left_running.append(int(pid)) os.remove(pid_file) if processes_left_running: for pid in processes_left_running: try: self.kill_process_ignore_access_denied(pid) except Exception as e: logging.warning("Error killing process with pid %d: %s", pid, _traceback_str()) self.try_and_stop_everything_for_tear_down() start_time = time.time() for pid in processes_left_running: while self.process_exists_or_access_denied( int(pid)) and time.time() - start_time < 40: time.sleep(0.3) self.check_server_ports_unbound() self.assertFalse( processes_left_running, msg="There should have been no PIDs left running but there were: %s" % (', '.join([str(p) for p in processes_left_running])))
def SvcDoRun(self): servicemanager.LogInfoMsg("ProcessFamilyTest starting up ..." ) try: logging.getLogger().setLevel(logging.INFO) self.server = FunkyWebServer() logging.info("Starting process family") family = ProcessFamilyForWin32ServiceTests(number_of_child_processes=self.server.num_children) self.server.family = family family.start(timeout=10) servicemanager.LogInfoMsg("ProcessFamilyTest started") try: logging.info("Starting HTTP server") self.server.run() except KeyboardInterrupt: logging.info("Stopping...") except Exception as e: logging.error("Error in windows service: %s\n%s", e, _traceback_str()) finally: logging.info("Stopping") stop_threads(exclude_thread_fn=lambda t: t.getName() != 'MainThread') servicemanager.LogInfoMsg("ProcessFamilyTest stopped" )
def tearDown(self): command_file = os.path.join(os.path.dirname(__file__), 'test', 'tmp', 'command.txt') if os.path.exists(command_file): os.remove(command_file) self.wait_for_parent_to_stop(5) #Now check that no processes are left over: start_time = time.time() processes_left_running = [] for pid_file in self.get_pid_files(): with open(pid_file, "r") as f: pid = f.read().strip() if pid: while self.process_exists_or_access_denied(int(pid)) and time.time() - start_time < 5: time.sleep(0.3) if self.process_exists_or_access_denied(int(pid)): processes_left_running.append(int(pid)) os.remove(pid_file) if processes_left_running: for pid in processes_left_running: try: self.kill_process_ignore_access_denied(pid) except Exception as e: logging.warning("Error killing process with pid %d: %s", pid, _traceback_str()) self.try_and_stop_everything_for_tear_down() start_time = time.time() for pid in processes_left_running: while self.process_exists_or_access_denied(int(pid)) and time.time() - start_time < 40: time.sleep(0.3) self.check_server_ports_unbound() self.assertFalse(processes_left_running, msg="There should have been no PIDs left running but there were: %s" % (', '.join([str(p) for p in processes_left_running])))
return super(ProcessFamilyForTests, self).get_child_process_cmd(child_number) + [ '--process_number', str(child_number+1)] if __name__ == '__main__': logging.basicConfig(level=logging.INFO) STARTUP_TIMEOUT = int(get_env("STARTUP_TIMEOUT", "") or "10") logging.info("Starting") try: try: server = FunkyWebServer() server_thread = None family = ProcessFamilyForTests(number_of_child_processes=server.num_children) server.family = family try: try: family.start(timeout=STARTUP_TIMEOUT) server_thread = threading.Thread(target=server.run) server_thread.start() while server_thread.is_alive(): server_thread.join(1) except KeyboardInterrupt: logging.info("Stopping...") server.stop() finally: if server_thread and server_thread.is_alive(): server_thread.join(5) finally: stop_threads() except Exception as e: logging.error("Error in process family test parent process: %s\n%s", e, processfamily._traceback_str()) logging.info("Done")
return super(ProcessFamilyForTests, self).get_child_process_cmd(child_number) + [ '--process_number', str(child_number+1)] if __name__ == '__main__': logging.basicConfig(level=logging.INFO) STARTUP_TIMEOUT = int(os.environ.get("STARTUP_TIMEOUT", "") or "10") logging.info("Starting") try: try: server = FunkyWebServer() server_thread = None family = ProcessFamilyForTests(number_of_child_processes=server.num_children) server.family = family try: try: family.start(timeout=STARTUP_TIMEOUT) server_thread = threading.Thread(target=server.run) server_thread.start() while server_thread.isAlive(): server_thread.join(1) except KeyboardInterrupt: logging.info("Stopping...") server.stop() finally: if server_thread and server_thread.isAlive(): server_thread.join(5) finally: stop_threads() except Exception as e: logging.error("Error in process family test parent process: %s\n%s", e, processfamily._traceback_str()) logging.info("Done")
if __name__ == '__main__': logging.basicConfig(level=logging.INFO) STARTUP_TIMEOUT = int(get_env("STARTUP_TIMEOUT", "") or "10") logging.info("Starting") try: try: server = FunkyWebServer() server_thread = None family = ProcessFamilyForTests( number_of_child_processes=server.num_children) server.family = family try: try: family.start(timeout=STARTUP_TIMEOUT) server_thread = threading.Thread(target=server.run) server_thread.start() while server_thread.isAlive(): server_thread.join(1) except KeyboardInterrupt: logging.info("Stopping...") server.stop() finally: if server_thread and server_thread.isAlive(): server_thread.join(5) finally: stop_threads() except Exception as e: logging.error("Error in process family test parent process: %s\n%s", e, processfamily._traceback_str()) logging.info("Done")
def get_response_object(self): if self.path.startswith('/injob'): return json.dumps(win32job.IsProcessInJob(win32api.GetCurrentProcess(), None), indent=3) if self.path.startswith('/job'): extended_info = win32job.QueryInformationJobObject(None, win32job.JobObjectExtendedLimitInformation) return json.dumps(extended_info, indent=3) if self.path.startswith('/close_file_and_delete_it'): try: if self.funkyserver._open_file_handle is not None: f = os.path.join(os.path.dirname(__file__), 'tmp', 'testfile.txt') logging.info("Closing test file handle") self.funkyserver._open_file_handle.close() self.funkyserver._open_file_handle = None assert os.path.exists(f) os.remove(f) assert not os.path.exists(f) return "OK" except Exception as e: logging.error("Failed to close file handle and delete file: %s\n%s", e, _traceback_str()) return "FAIL" if self.path.startswith('/stop'): logging.info("Returning child_processes_terminated: %r", self.funkyserver.child_processes_terminated) return repr(self.funkyserver.child_processes_terminated) return "OK"
def handle_error(self, request, client_address): logging.error('Exception happened during processing of request from %s:\n%s', client_address, _traceback_str())
def handle_error(self, request, client_address): logging.error( 'Exception happened during processing of request from %s:\n%s', client_address, _traceback_str())