def test_random_nameserver_process(): """ Basic random_nameserver_process function tests: port range and exceptions. """ # Port range port_start = 11000 port_stop = port_start + 100 nsprocess = random_nameserver_process(port_start=port_start, port_stop=port_stop) address = nsprocess.addr assert port_start <= address.port <= port_stop ns = NSProxy(address) ns.shutdown() # Raising exceptions with pytest.raises(ValueError): random_nameserver_process(port_start=-1, port_stop=-2) with pytest.raises(RuntimeError): random_nameserver_process(port_start=22, port_stop=22, timeout=0.5)
def test_nameserverprocess_shutdown(): """ Name server shutdown can be called directly from the name server process. """ nameserver = random_nameserver_process() run_agent('a0') run_agent('a1') while not len(nameserver.agents()) == 2: continue assert 'a0' in nameserver.agents() assert 'a1' in nameserver.agents() nameserver.shutdown() assert not nameserver.is_alive()
def test_nameserverprocess_shutdown_lazy_agents(): """ Shutdown a name server process with agents that wait some time before shutting down. """ class Lazy(Agent): def shutdown(self): time.sleep(1) super().shutdown() nsprocess = random_nameserver_process() run_agent('a0', base=Lazy) run_agent('a1', base=Lazy) t0 = time.time() nsprocess.shutdown() assert time.time() - t0 > 1