def test_agent_error_address_already_in_use(nsproxy): """ Running an agent should raise an error if address is already in use. """ with pytest.raises(RuntimeError) as error: run_agent('a0', nsaddr=nsproxy.addr(), addr=nsproxy.addr()) assert 'OSError' in str(error.value) assert 'Address already in use' in str(error.value)
def test_nameserver_proxy_shutdown_agents(nsproxy): """ Shutdown agents registered in a name server from a name server proxy. """ run_agent('Agent0', nsaddr=nsproxy.addr()) run_agent('Agent1', nsaddr=nsproxy.addr()) nsproxy.shutdown_agents() assert len(nsproxy.agents()) == 0
def test_nameserver_proxy_list(nsproxy): """ Verify new agents get registered in the nameserver. """ run_agent('a0', nsproxy.addr()) run_agent('a1', nsproxy.addr()) # List registered agents agent_list = nsproxy.list() assert 'a0' in agent_list assert 'a1' in agent_list
def test_nameserver_agent_address(nsproxy): """ A name server proxy can be used to retrieve an agent's socket address as well, given the agent's alias and the socket's alias. """ a0 = run_agent('a0') a1 = run_agent('a1') addr0 = a0.bind('PUB', alias='foo') addr1 = a1.bind('PUSH', alias='bar') assert nsproxy.addr('a0', 'foo') == addr0 assert nsproxy.addr('a1', 'bar') == addr1
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()
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_nameserver_oserror(nsproxy): """ Name server start() should raise an error if address is already in use. """ with pytest.raises(RuntimeError) as error: run_nameserver(nsproxy.addr()) assert 'OSError' in str(error.value) assert 'Address already in use' in str(error.value)
def test_agent_error_permission_denied(nsproxy): """ Running an agent should raise an error if it has not sufficient permissions for binding to the address. """ with pytest.raises(RuntimeError) as error: run_agent('a0', nsaddr=nsproxy.addr(), addr='127.0.0.1:22') assert 'PermissionError' in str(error.value) assert 'Permission denied' in str(error.value)
def test_nameserver_list(nsproxy): """ A simple test that checks the correct creation of a name server. """ agents = nsproxy.list() name = 'Pyro.NameServer' assert len(agents) == 1 assert list(agents.keys())[0] == name assert agents[name] == 'PYRO:%s@%s' % (name, nsproxy.addr())
def test_nameserver_environ(nsproxy): """ When starting a nameserver, a environment variable should be set to ease the process of running new agents. """ assert str(nsproxy.addr()) == os.environ.get('OSBRAIN_NAMESERVER_ADDRESS') run_agent('a0') run_agent('a1') # List registered agents agent_list = nsproxy.list() assert 'a0' in agent_list assert 'a1' in agent_list
def test_nameserver_proxy_shutdown_timeout(nsproxy): """ A NSProxy should raise a TimeoutError if all agents were not shutted down and unregistered after a number of seconds. """ class ShutdownTimeoutNSProxy(NSProxy): def agents(self): return ['agent_foo'] timeoutproxy = ShutdownTimeoutNSProxy(nsproxy.addr()) with pytest.raises(TimeoutError): timeoutproxy.shutdown(timeout=1.)
def test_no_timeout_locate_ns_existing(nsproxy): """ Locating a NS that exists with no timeout should be OK. """ locate_ns(nsproxy.addr(), timeout=0.)
def test_agent_proxy_nameserver_address(nsproxy): """ Agent proxies should be able to return the name server address. """ agent = run_agent('foo') assert agent.nsaddr() == nsproxy.addr()