def test_pubsub_1_1_2_mult_filters(self): """ Tests pub/sub 1-1 (one sub one pub) with 2 message each on a different filter """ def configure(composer: Composer): composer.interface.router = MagicMock() return composer def run_assertions(composer: Composer): cb1 = ReactorCommand.create_callback(callback=StateInput.INPUT, envelope=env1) cb2 = ReactorCommand.create_callback(callback=StateInput.INPUT, envelope=env2) composer.interface.router.route_callback.assert_has_calls( [call(cb1), call(cb2)], any_order=True) env1 = random_envelope() env2 = random_envelope() filter1 = FILTERS[0] filter2 = FILTERS[1] sub = MPComposer(config_fn=configure, assert_fn=run_assertions, name='** SUB', sk=sk1) pub = MPComposer(name='++ PUB', sk=sk2) sub.add_sub(url=URL, filter=filter2) sub.add_sub(url=URL, filter=filter1) pub.add_pub(url=URL) time.sleep(0.2) pub.send_pub_env(filter=filter1, envelope=env1) pub.send_pub_env(filter=filter2, envelope=env2) self.start()
def test_pubsub_1_1_1_with_unauth_pub(self): """ Tests pub/sub 1-1 (one sub one pub) with one message, and one unauthorized veriying key """ def config_sub(composer: Composer): from unittest.mock import MagicMock composer.manager.router = MagicMock() return composer def assert_sub(composer: Composer): from cilantro.messages.reactor.reactor_command import ReactorCommand from cilantro.protocol.states.decorators import StateInput from unittest.mock import call # cb = ReactorCommand.create_callback(callback=StateInput.INPUT, envelope=env) # composer.interface.router.route_callback.assert_called_once_with(cb) expected_cb = call( ReactorCommand.create_callback(callback=StateInput.INPUT, envelope=env)) unexpected_cb = call( ReactorCommand.create_callback( callback=StateInput.LOOKUP_FAILED, envelope=evil_env)) call_args = composer.manager.router.route_callback.call_args_list # # DEBUG STUFF # from cilantro.logger.base import get_logger # l = get_logger("assert sub") # l.debug('EXPECTED') # l.critical(expected_cb) # l.debug('UNEXPECTED') # l.critical(unexpected_cb) # l.important2("got dat call args: {}".format(call_args)) # # END DEBUG STUFF # composer.interface.router.route_callback.assert_has_calls([expected_cb], any_order=True) assert expected_cb in call_args, "Expected callback {} to be in call_args {}".format( expected_cb, call_args) assert unexpected_cb not in call_args, "Did not expect callback {} to be in call_args {}".format( unexpected_cb, call_args) # assert len(call_args) == 2, "route_callback should be called exactly twice, not {} times with {}"\ # .format(len(call_args), call_args) # composer.interface.router.route_callback.assert_has_calls(calls, any_order=True) env = random_envelope() evil_env = random_envelope() sub = MPComposer(config_fn=config_sub, assert_fn=assert_sub, name='** [MN1] SUB', sk=sk1) pub = MPComposer(name='++ [Delegate1] PUB', sk=sk2) no_auth_pub = MPComposer(name='++ [Mr. Evil] PUB', sk=sk_sketch) sub.add_sub(vk=vk2, filter=FILTER) sub.add_sub(vk=vk_sketch, filter=FILTER) pub.add_pub(ip=pub.ip) no_auth_pub.add_pub(ip=no_auth_pub.ip) time.sleep(3.0) pub.send_pub_env(filter=FILTER, envelope=env) no_auth_pub.send_pub_env(filter=FILTER, envelope=evil_env) time.sleep(3.0) # allow PUB envelopes to go through self.start()
def test_pubsub_n_1_n_removesub(self): """ Tests pub/sub n-1, with a sub removing a publisher after its first message """ def configure(composer: Composer): from unittest.mock import MagicMock composer.interface.router.route_callback = MagicMock() return composer def assert_sub(composer: Composer): from cilantro.messages.reactor.reactor_command import ReactorCommand from cilantro.protocol.states.decorators import StateInput from unittest.mock import call callback1 = ReactorCommand.create_callback( callback=StateInput.INPUT, envelope=env1) callback2 = ReactorCommand.create_callback( callback=StateInput.INPUT, envelope=env2) calls = [call(callback1), call(callback2)] call_args = composer.interface.router.route_callback.call_args_list assert len(call_args) == 2, "route_callback should be called exactly twice, not {} times with {}"\ .format(len(call_args), call_args) composer.interface.router.route_callback.assert_has_calls( calls, any_order=True) env1 = random_envelope() env2 = random_envelope() env3 = random_envelope() sub = MPComposer(config_fn=configure, assert_fn=assert_sub, name='** SUB [MN1]', sk=sk1) pub1 = MPComposer(name='++ PUB 1 [Delegate1]', sk=sk2) pub2 = MPComposer(name='++ PUB 2 [Delegate2]', sk=sk3) sub.add_sub(vk=vk2, filter=FILTER) # sub to pub1 sub.add_sub(vk=vk3, filter=FILTER) # sub to pub2 pub1.add_pub(ip=pub1.ip) # Pub on its own URL pub2.add_pub(ip=pub2.ip) # Pub on its own URL time.sleep(3.0) pub1.send_pub_env(filter=FILTER, envelope=env1) pub2.send_pub_env(filter=FILTER, envelope=env2) time.sleep(1.0) # allow messages to go through sub.remove_sub(vk=vk3) # unsub to pub2 time.sleep(1.0) # allow remove_sub_url command to go through pub2.send_pub_env( filter=FILTER, envelope=env3 ) # this should not be recv by sub, as he removed this guy's url time.sleep( 3.0 ) # allow messages to go through before we start checking assertions self.start()