async def test_module_routing_many(): """ Test that routing to a module works. """ dispatcher = Dispatcher() dispatcher.called_module = None routed_event = asyncio.Event() class TestModule1(Module): DOC_URI = '' PROTOCOL = 'test_protocol' VERSION = '1.0' async def testing_type(self, msg, *args, **kwargs): kwargs['dispatcher'].called_module = 1 kwargs['event'].set() class TestModule2(Module): DOC_URI = '' PROTOCOL = 'test_protocol' VERSION = '2.0' async def testing_type(self, msg, *args, **kwargs): kwargs['dispatcher'].called_module = 2 kwargs['event'].set() dispatcher.route_module(TestModule1()) dispatcher.route_module(TestModule2()) test_msg = Message({ '@type': 'test_protocol/1.0/testing_type', 'test': 'test' }) await dispatcher.dispatch(test_msg, event=routed_event, dispatcher=dispatcher) await routed_event.wait() assert routed_event.is_set() assert dispatcher.called_module == 1 routed_event.clear() test_msg = Message({ '@type': 'test_protocol/2.0/testing_type', 'test': 'test' }) await dispatcher.dispatch(test_msg, event=routed_event, dispatcher=dispatcher) await routed_event.wait() assert routed_event.is_set() assert dispatcher.called_module == 2
async def test_dispatching_selection(): """Test that routing works in agent.""" dispatcher = Dispatcher() called_event = asyncio.Event() async def route_gets_called(_msg, **kwargs): kwargs["event"].set() async def route_not_called(_msg, **_kwargs): print("this should not be called") dispatcher.add( MsgType("test_protocol/2.0/testing_type"), route_gets_called, ) dispatcher.add( MsgType("test_protocol/1.0/testing_type"), route_not_called, ) test_msg = Message.parse_obj( {"@type": "test_protocol/2.0/testing_type", "test": "test"} ) await dispatcher.dispatch(test_msg, event=called_event) assert called_event.is_set()
async def test_dispatching_selection(): """ Test that routing works in agent. """ dispatcher = Dispatcher() called_event = asyncio.Event() async def route_gets_called(_msg, **kwargs): kwargs['event'].set() async def route_not_called(_msg, **_kwargs): print('this should not be called') dispatcher.add_handler(Handler( Type.from_str('test_protocol/2.0/testing_type'), route_gets_called, )) dispatcher.add_handler(Handler( Type.from_str('test_protocol/1.0/testing_type'), route_not_called, )) test_msg = Message({ '@type': 'test_protocol/2.0/testing_type', 'test': 'test' }) await dispatcher.dispatch(test_msg, event=called_event) assert called_event.is_set()
async def test_module_routing_explicit_def(): """ Test that routing to a module works. """ dispatcher = Dispatcher() called_event = asyncio.Event() class TestModule(Module): DOC_URI = '' PROTOCOL = 'test_protocol' VERSION = '1.0' routes = {} @route_def(routes, 'test_protocol/1.0/testing_type') async def route_gets_called(self, msg, **kwargs): kwargs['event'].set() mod = TestModule() dispatcher.route_module(mod) test_msg = Message({ '@type': 'test_protocol/1.0/testing_type', 'test': 'test' }) await dispatcher.dispatch(test_msg, event=called_event) assert called_event.is_set()
def test_message_serialization(): """Test deserializing and serializing a message""" msg = Message.deserialize(json.dumps({"@type": TEST_TYPE}), mtc=MessageTrustContext()) assert msg.type == TEST_TYPE assert msg.id is not None assert msg.mtc assert msg.serialize() == json.dumps({"@type": TEST_TYPE, "@id": msg.id})
async def test_dispatching_selection_message_too_old(): """Test that routing works in agent.""" dispatcher = Dispatcher() dispatcher.add(MsgType("test_protocol/3.0/testing_type"), lambda msg: msg) dispatcher.add(MsgType("test_protocol/2.0/testing_type"), lambda msg: msg) test_msg = Message.parse_obj( {"@type": "test_protocol/1.0/testing_type", "test": "test"} ) with pytest.raises(NoRegisteredHandlerException): await dispatcher.dispatch(test_msg)
def test_valid_message_no_doc_uri(): """ Test basic message creation and member access. """ id_ = '12345' msg = Message({'@type': TEST_TYPE_NO_DOC, '@id': id_}) assert msg.type == TEST_TYPE_NO_DOC assert msg.id == id_ assert msg.doc_uri == '' assert msg.protocol == 'protocol' assert msg.version == '1.0' assert msg.normalized_version == '1.0.0' assert msg.name == 'test' assert msg.version_info == Semver(1, 0, 0)
def test_valid_message_with_type_obj(): """ Test creating message using Type object. """ id_ = '12345' msg = Message({'@type': Type.from_str(TEST_TYPE), '@id': id_}) assert msg.type == TEST_TYPE assert msg.id == id_ assert msg.doc_uri == 'test_type/' assert msg.protocol == 'protocol' assert msg.version == '1.0' assert msg.normalized_version == '1.0.0' assert msg.name == 'test' assert msg.version_info == Semver(1, 0, 0)
def test_message_serialization(): """ Test deserializing and serializing a message """ msg = Message.deserialize('{"@type": "%s"}' % TEST_TYPE) assert msg.type == TEST_TYPE assert msg.id is not None assert msg.doc_uri == 'test_type/' assert msg.protocol == 'protocol' assert msg.version == '1.0' assert msg.name == 'test' assert msg.version_info == Semver(1, 0, 0) assert msg.serialize() == \ '{"@type": "%s", "@id": "%s"}' % (TEST_TYPE, msg.id)
def test_valid_message(): """Test basic message creation and member access.""" id_ = "12345" msg = Message.parse_obj({"@type": TEST_TYPE, "@id": id_}) assert msg.type == TEST_TYPE assert msg.id == id_ assert msg.type.doc_uri == "test_type/" assert msg.type.protocol == "protocol" assert msg.type.version == "1.0" assert msg.type.normalized_version == "1.0.0" assert msg.type.name == "test" assert msg.type.version_info == MsgVersion(1, 0, 0) assert len(msg) == 2
async def test_dispatching_no_handler(): """Test that routing works in agent.""" dispatcher = Dispatcher() async def route_gets_called(_msg): pass dispatcher.add(MsgType("test_protocol/1.0/testing_type"), route_gets_called) test_msg = Message.parse_obj( {"@type": "test_protocol/4.0/other_type", "test": "test"} ) with pytest.raises(NoRegisteredHandlerException): await dispatcher.dispatch(test_msg)
async def test_dispatching_selection_message_too_old(): """ Test that routing works in agent. """ dispatcher = Dispatcher() dispatcher.add_handler(Handler( Type.from_str('test_protocol/3.0/testing_type'), lambda msg: msg )) dispatcher.add_handler(Handler( Type.from_str('test_protocol/2.0/testing_type'), lambda msg: msg )) test_msg = Message({ '@type': 'test_protocol/1.0/testing_type', 'test': 'test' }) with pytest.raises(NoRegisteredHandlerException): await dispatcher.dispatch(test_msg)
async def test_dispatching_no_handler(): """ Test that routing works in agent. """ dispatcher = Dispatcher() async def route_gets_called(_msg): pass dispatcher.add_handler(Handler( Type.from_str('test_protocol/1.0/testing_type'), route_gets_called )) test_msg = Message({ '@type': 'test_protocol/4.0/other_type', 'test': 'test' }) with pytest.raises(NoRegisteredHandlerException): await dispatcher.dispatch(test_msg)
async def test_module_routing_no_matching_version(): """ Test that routing to a module works. """ dispatcher = Dispatcher() called_event = asyncio.Event() class TestModule(Module): DOC_URI = '' PROTOCOL = 'test_protocol' VERSION = '1.0' async def testing_type(self, msg, *args, **kwargs): kwargs['event'].set() mod = TestModule() dispatcher.route_module(mod) test_msg = Message({ '@type': 'test_protocol/3.0/testing_type', 'test': 'test' }) with pytest.raises(NoRegisteredRouteException): await dispatcher.dispatch(test_msg, event=called_event)
async def test_module_routing_minor_version_different(): """ Test that routing to a module works. """ dispatcher = Dispatcher() called_event = asyncio.Event() class TestModule(Module): DOC_URI = '' PROTOCOL = 'test_protocol' VERSION = '1.4' async def testing_type(self, msg, *args, **kwargs): kwargs['event'].set() mod = TestModule() dispatcher.route_module(mod) test_msg = Message({ '@type': 'test_protocol/1.0/testing_type', 'test': 'test' }) await dispatcher.dispatch(test_msg, event=called_event) assert called_event.is_set()
def test_pretty_print(): """ Assert pretty print is returning something crazy. """ assert isinstance(Message({'@type': TEST_TYPE}).pretty_print(), str)
def test_bad_message_no_type(): """ Test no type in message raises error. """ with pytest.raises(InvalidMessage): Message({'test': 'test'})
def test_bad_serialized_message(): """ Test bad serialized message raises an error on deserialze. """ with pytest.raises(InvalidMessage): Message.deserialize('asdf')
def test_bad_message_no_type(): """Test no type in message raises error.""" with pytest.raises(ValueError): Message.parse_obj({"test": "test"})
def test_id_generated(): """ Test ID is generated for message where one is not specified. """ msg = Message({'@type': TEST_TYPE}) assert msg.type == TEST_TYPE assert msg.id is not None
def test_pretty_print(): """Assert pretty print is returning something crazy.""" assert isinstance( Message.parse_obj({ "@type": TEST_TYPE }).pretty_print(), str)
def test_bad_message_id(id_): """Test message with bad message id""" with pytest.raises(ValueError): Message.parse_obj({"@type": TEST_TYPE, "@id": id_})
def test_bad_message_id(id_): """ Test message with bad message id """ with pytest.raises(InvalidMessage): Message({'@type': TEST_TYPE, '@id': id_})
def test_bad_serialized_message(): """Test bad serialized message raises an error on deserialze.""" with pytest.raises(ValueError): Message.deserialize("asdf")
def test_bad_message_type(type_str): """ Test bad message types raise InvalidMessage """ with pytest.raises(InvalidType): Message({'@type': type_str})
def test_message_getitem(): msg = Message(**{"@id": "test", "@type": "doc/protocol/1.0/name"}) assert msg["@id"] == "test" == msg.id assert msg["@type"] == "doc/protocol/1.0/name" == msg.type