def test_connection_error_public_key_already_in_use(self): """Test that a OEFConnectionError is raised when we try to connect two agents with the same public key.""" with pytest.raises(OEFConnectionError, match="Public key already in use."): with NetworkOEFNode(): agent_1 = OEFAgent("the_same_public_key", "127.0.0.1", 3333) agent_2 = OEFAgent(agent_1.public_key, "127.0.0.1", 3333) agent_1.connect() agent_2.connect()
def __init__( self, oef_addr: str, oef_port: int, loop: Optional[asyncio.AbstractEventLoop] = None, ): """ Initialize. :param oef_addr: IP address of the OEF node. :param oef_port: Port of the OEF node. """ self.oef_addr = oef_addr self.oef_port = oef_port self._result = False self._stop = False self._core = AsyncioCore() self.agent = OEFAgent("check", core=self._core, oef_addr=self.oef_addr, oef_port=self.oef_port) self.agent.on_connect_success = self.on_connect_ok self.agent.on_connection_terminated = self.on_connect_terminated self.agent.on_connect_failed = self.exception_handler
def test_competition_stops_too_few_registered_agents(self, network_node): """Test that if the controller agent does not receive enough registrations, it stops.""" controller_agent = ControllerAgent(version=1) controller_agent.connect() parameters = TACParameters(min_nb_agents=2, start_time=datetime.datetime.now(), registration_timeout=5) job = Thread(target=controller_agent.handle_competition, args=(parameters, )) job.start() crypto = Crypto() agent1 = OEFAgent(crypto.public_key, "127.0.0.1", 10000, loop=asyncio.new_event_loop()) agent1.connect() agent1.send_message( 0, 0, controller_agent.public_key, Register(agent1.public_key, crypto, 'agent_name').serialize()) job.join() assert len(controller_agent.game_handler.registered_agents) == 1
def test_disconnect(self): """Test that the disconnect method works correctly.""" with NetworkOEFNode(): agent_1 = OEFAgent("disconnect", "127.0.0.1", 3333) assert not agent_1._oef_proxy.is_connected() agent_1.connect() assert agent_1._oef_proxy.is_connected() agent_1.disconnect() assert not agent_1._oef_proxy.is_connected()
def test_that_two_connect_attempts_work_correctly(self): """Test that two call to the :func:'~agents.Agent.connect()' method work correctly. Use the local implementation of the OEF.""" with NetworkOEFNode(): agent_1 = OEFAgent("two_connect_attempt", "127.0.0.1", 3333) first_status = agent_1.connect() second_status = agent_1.connect() assert first_status assert second_status
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # ------------------------------------------------------------------------------ """ A short script that show how an agent can register/unregister to the OEF. """ from oef.schema import Description from oef.agents import OEFAgent if __name__ == '__main__': agent = OEFAgent("agent_1", oef_addr="127.0.0.1", oef_port=3333) agent.connect() print("Agent successfully connected.") # specify message id to receive any error messages from the OEF Node. # look at the 'onOEFError' agent method. message_id = 0 agent.register_agent(message_id, Description({})) print("Agent registered to the OEF Node.") agent.unregister_agent(message_id + 1) print("Agent {} unregistered".format(agent.public_key)) agent.disconnect()