def test_nameserver_proxy_shutdown_with_pyroerror(): """ Check that `PyroError`s raised during `async_nameserver_shutdown` are handled correctly. """ nameserver = run_nameserver() ap = AgentProcess() name = ap.start() proxy = Proxy(name) proxy.run() ap.kill() nameserver.async_shutdown_agents(nameserver.addr()) nameserver.shutdown()
def test_early_agent_proxy(nsproxy): """ It must be possible to create a Proxy when the registration of the new agent is imminent, even if it has not occured yet. A timeout will occur in case the agent could not be located. """ agent = AgentProcess('a0') # Start agent later Timer(2, agent.start).start() # Locate agent now a0 = Proxy('a0', timeout=3.) # Just check agent is ready assert a0.unsafe.ping() == 'pong'
def test_agent_proxy_wait_running_timeout(nsproxy, timeout): """ Check that the `wait_for_running` method times out if the agent is not running after the specified number of seconds. """ AgentProcess('agent').start() time0 = time.time() with pytest.raises(TimeoutError) as error: Proxy('agent').wait_for_running(timeout=timeout) elapsed = time.time() - time0 assert 'Timed out' in str(error.value) assert elapsed >= timeout assert elapsed < timeout + 0.5
def test_agent_inheritance(nsproxy): """ Test agent inheritance; agents can be based on a custom class. """ class NewAgent(Agent): def the_answer_to_life(self): return 42 # Test an Agent based on the new class AgentProcess('new', nsaddr=nsproxy.addr(), base=NewAgent).start() new = Proxy('new', nsproxy.addr()) new.run() assert new.the_answer_to_life() == 42 # Test the quick `run_agent` function a0 = run_agent('a0', nsproxy.addr(), base=NewAgent) assert a0.the_answer_to_life() == 42
def test_agent_proxy_wait_running(nsproxy, timeout): """ Using `wait_for_running` on a proxy after initialization should block until the agent is running or time out. """ AgentProcess('agent').start() # Get "offline" proxy agent = Proxy('agent') time0 = time.time() Timer(abs(timeout) / 2, agent.run).start() proxy = Proxy('agent').wait_for_running(timeout=timeout) elapsed = time.time() - time0 assert proxy.ping() == 'pong' assert elapsed >= abs(timeout) / 2 assert elapsed <= abs(timeout)
def test_agent_shutdown(nsproxy): """ An agent must unregister itself before shutting down. """ agent = AgentProcess('a0', nsproxy.addr()) agent.start() a0 = Proxy('a0', nsproxy.addr()) a0.run() assert 'a0' in nsproxy.list() a0.shutdown() agent.join() assert 'a0' not in nsproxy.list()