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 with pytest.raises(Pyro4.errors.NamingError): a0 = Proxy('a0', timeout=1.) a0 = Proxy('a0', timeout=10.) # Just check agent is ready assert a0.unsafe.ping() == 'pong'
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_proxy_without_nsaddr(nsproxy): """ Creating a proxy without specifying the name server address should result in the OSBRAIN_NAMESERVER_ADDRESS being used. """ agent0 = run_agent('foo') agent0.set_attr(x=1.0) agent1 = Proxy('foo') assert agent1.get_attr('x') == 1.0
def test_agent_proxy_safe_and_unsafe_parameter(monkeypatch, nsproxy): """ Using the safe/unsafe parameter when initializing a proxy should allow us to override the environment global configuration. """ run_agent('foo') # Safe environment monkeypatch.setitem(osbrain.config, 'SAFE', True) proxy = Proxy('foo') assert proxy._safe proxy = Proxy('foo', safe=False) assert not proxy._safe # Unsafe environment monkeypatch.setitem(osbrain.config, 'SAFE', False) proxy = Proxy('foo') assert not proxy._safe proxy = Proxy('foo', safe=True) assert proxy._safe
def test_agent_proxy_safe_and_unsafe_property(monkeypatch, nsproxy): """ Using the safe/unsafe property from a proxy should allow us to override the environment global configuration. """ run_agent('foo') # Safe environment monkeypatch.setitem(osbrain.config, 'SAFE', True) proxy = Proxy('foo') assert proxy._safe assert proxy.safe._safe assert not proxy.unsafe._safe # Unsafe environment monkeypatch.setitem(osbrain.config, 'SAFE', False) proxy = Proxy('foo') assert not proxy._safe assert proxy.safe._safe assert not proxy.unsafe._safe
def test_agent_proxy_wait_running_0_seconds(nsproxy): """ Using `wait_for_running` on a proxy after initialization should block until the agent is running or time out. """ run_agent('agent') proxy = Proxy('agent').wait_for_running(timeout=0) assert proxy.ping() == 'pong'
def test_agent_proxy_safe_and_unsafe_parameter(nsproxy): """ Using the safe/unsafe parameter when initializating a proxy should allow us to override the environment global configuration. """ run_agent('foo') # Safe environment osbrain.config['SAFE'] = True proxy = Proxy('foo') assert proxy._safe proxy = Proxy('foo', safe=False) assert not proxy._safe # Unsafe environment osbrain.config['SAFE'] = False proxy = Proxy('foo') assert not proxy._safe proxy = Proxy('foo', safe=True) assert proxy._safe
def test_agent_proxy_safe_and_unsafe_property(nsproxy): """ Using the safe/unsafe property from a proxy should allow us to override the environment global configuration. """ run_agent('foo') # Safe environment osbrain.config['SAFE'] = True proxy = Proxy('foo') assert proxy._safe assert proxy.safe._safe assert not proxy.unsafe._safe # Unsafe environment osbrain.config['SAFE'] = False proxy = Proxy('foo') assert not proxy._safe assert proxy.safe._safe assert not proxy.unsafe._safe
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_proxy_pickle_serialization(nsproxy): """ Make sure proxies can be (de)serialized using pickle. """ agent0 = run_agent('foo') agent0.set_attr(x=1.0) proxy = Proxy('foo') serialized = pickle.dumps(proxy) assert serialized deserialized = pickle.loads(serialized) assert deserialized assert deserialized.get_attr('x') == 1.0
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_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