def start_up(self, test_command=None, wait_for_middle_child=True, wait_for_children=True): command_file = os.path.join(os.path.dirname(__file__), 'test', 'tmp', 'command.txt') if test_command: with open(command_file, "w") as f: f.write(test_command) elif os.path.exists(command_file): os.remove(command_file) self.start_parent_process() #Wait up to 15 secs for the all ports to be available (the parent might wait 10 for a middle child): start_time = time.time() still_waiting = True while still_waiting and time.time() - start_time < 15: still_waiting = False for i in range(4): if i > 0 and not wait_for_children: continue if i == 2 and not wait_for_middle_child: continue try: s = socket.socket() try: s.connect(("localhost", Config.get_starting_port_nr()+i)) except socket.error, e: still_waiting = True break finally: s.close() if still_waiting: time.sleep(0.3) self.assertFalse(still_waiting, "Waited 10 seconds and some http ports are still not accessible")
def __init__(self): self.parse_args_and_setup_logging() self.port = Config.get_starting_port_nr() + self.process_number MyHTTPRequestHandler.funkyserver = self self.httpd_lock = threading.RLock() self.httpd = None self.child_processes_terminated = None
def assert_middle_child_port_unbound(self): port = Config.get_starting_port_nr()+2 logging.info("Checking for ability to bind to port %d", port) serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: if not sys.platform.startswith('win'): #On linux I need this setting cos we are starting and stopping things #so frequently that they are still in a STOP_WAIT state when I get here serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serversocket.bind(("", port)) except Exception as e: self.fail("Middle child port is not unbound as expected") finally: serversocket.close()
def assert_middle_child_port_unbound(self): port = Config.get_starting_port_nr() + 2 logging.info("Checking for ability to bind to port %d", port) serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: if not sys.platform.startswith('win'): #On linux I need this setting cos we are starting and stopping things #so frequently that they are still in a STOP_WAIT state when I get here serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serversocket.bind(("", port)) except Exception as e: self.fail("Middle child port is not unbound as expected") finally: serversocket.close()
def check_server_ports_unbound(self): bound_ports = [] for pnumber in range(4): port = Config.get_starting_port_nr() + pnumber #I just try and bind to the server port and see if I have a problem: logging.info("Checking for ability to bind to port %d", port) serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: if not sys.platform.startswith('win'): #On linux I need this setting cos we are starting and stopping things #so frequently that they are still in a STOP_WAIT state when I get here serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serversocket.bind(("", port)) except Exception as e: bound_ports.append(port) finally: serversocket.close() self.assertFalse(bound_ports, "The following ports are still bound: %s" % ', '.join([str(p) for p in bound_ports]))
def check_server_ports_unbound(self): bound_ports = [] for pnumber in range(4): port = Config.get_starting_port_nr() + pnumber #I just try and bind to the server port and see if I have a problem: logging.info("Checking for ability to bind to port %d", port) serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: if not sys.platform.startswith('win'): #On linux I need this setting cos we are starting and stopping things #so frequently that they are still in a STOP_WAIT state when I get here serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serversocket.bind(("", port)) except Exception as e: bound_ports.append(port) finally: serversocket.close() self.assertFalse( bound_ports, "The following ports are still bound: %s" % ', '.join([str(p) for p in bound_ports]))
def start_up(self, test_command=None, wait_for_middle_child=True, wait_for_children=True, parent_timeout=None): command_file = os.path.join(os.path.dirname(__file__), 'test', 'tmp', 'command.txt') if test_command: with open(command_file, "w") as f: f.write(test_command) elif os.path.exists(command_file): os.remove(command_file) self.start_parent_process(timeout=parent_timeout) #Wait up to 15 secs for the all ports to be available (the parent might wait 10 for a middle child): start_time = time.time() still_waiting = True ports_to_wait = list(range(4)) if wait_for_children else [0] if not wait_for_middle_child: ports_to_wait.remove(2) while still_waiting and time.time() - start_time < 15: still_waiting = False for i in ports_to_wait: try: s = socket.socket() try: s.connect( ("localhost", Config.get_starting_port_nr() + i)) except socket.error as e: still_waiting = True break finally: s.close() if still_waiting: time.sleep(0.3) self.assertFalse( still_waiting, "Waited 10 seconds and some http ports are still not accessible")
def send_middle_child_http_command(self, command, params=None, **kwargs): return self.send_http_command(Config.get_starting_port_nr() + 2, command, params=params, **kwargs)
def send_parent_http_command(self, command, params=None, **kwargs): return self.send_http_command(Config.get_starting_port_nr(), command, params=params, **kwargs)
def send_middle_child_http_command(self, command, **kwargs): return self.send_http_command(Config.get_starting_port_nr()+2, command, **kwargs)
def send_parent_http_command(self, command, **kwargs): return self.send_http_command(Config.get_starting_port_nr(), command, **kwargs)