Exemple #1
0
    def test_req_reply_1_1_1_timeout(self):
        """
        Tests request/reply 1_1_1 with a timeout and a late reply
        """
        def config_router(composer: Composer):
            def reply(*args, **kwargs):  # do i need the *args **kwargs ??
                time.sleep(timeout_duration * 1.5)
                composer.send_reply(message=reply_msg,
                                    request_envelope=request_env)

            composer.interface.router = MagicMock()
            composer.interface.router.route_callback.side_effect = reply
            return composer

        def config_dealer(composer: Composer):
            composer.interface.router = MagicMock()
            return composer

        def assert_dealer(composer: Composer):
            cb = ReactorCommand.create_callback(callback=StateInput.TIMEOUT,
                                                envelope=request_env)
            composer.interface.router.route_callback.assert_any_call(cb)

        def assert_router(composer: Composer):
            cb = ReactorCommand.create_callback(callback=StateInput.REQUEST,
                                                envelope=request_env,
                                                header=dealer_id)
            composer.interface.router.route_callback.assert_called_once_with(
                cb)

        timeout_duration = 0.5

        dealer_id = vk1
        dealer_sk = sk1
        router_sk = sk2
        router_url = URLS[1]

        request_env = random_envelope(sk=dealer_sk)
        reply_msg = random_msg()

        dealer = MPComposer(name='DEALER',
                            sk=sk1,
                            config_fn=config_dealer,
                            assert_fn=assert_dealer)
        router = MPComposer(config_fn=config_router,
                            assert_fn=assert_router,
                            name='ROUTER',
                            sk=router_sk)

        dealer.add_dealer(url=router_url)
        router.add_router(url=router_url)

        time.sleep(0.2)

        dealer.send_request_env(url=router_url,
                                envelope=request_env,
                                timeout=timeout_duration)

        self.start()
Exemple #2
0
    def test_req_reply_1_1_1(self):
        """
        Tests request/reply 1_1_1
        """
        def config_router(composer: Composer):
            from unittest.mock import MagicMock

            def reply(*args, **kwargs):  # do i need the *args **kwargs ??
                composer.send_reply(message=reply_msg,
                                    request_envelope=request_env)

            composer.manager.router = MagicMock()
            composer.manager.router.route_callback.side_effect = reply
            return composer

        def config_dealer(composer: Composer):
            from unittest.mock import MagicMock
            composer.manager.router = MagicMock()
            return composer

        def assert_dealer(composer: Composer):
            from cilantro.messages.reactor.reactor_command import ReactorCommand

            args = composer.manager.router.route_callback.call_args_list
            # assert len(args) == 1, "dealer's route_callback should of only been called once (with the reply env)"

            reply_callback_found = False
            for call in args:
                callback_cmd = call[0][0]
                assert isinstance(
                    callback_cmd, ReactorCommand
                ), "arg of route_callback should be a ReactorCommand"
                if callback_cmd.envelope and callback_cmd.envelope.message == reply_msg:
                    reply_callback_found = True
                    break
                    # assert callback_cmd.envelope.message == reply_msg, "Callback's envelope's message should be the reply_msg"
            assert reply_callback_found, "Reply callback {} not found in call args {}".format(
                reply_msg, args)

        def assert_router(composer: Composer):
            from cilantro.protocol.states.decorators import StateInput
            from cilantro.messages.reactor.reactor_command import ReactorCommand
            cb = ReactorCommand.create_callback(callback=StateInput.REQUEST,
                                                envelope=request_env,
                                                header=dealer_id)
            composer.manager.router.route_callback.assert_called_with(cb)

        dealer_id = vk1
        dealer_sk = sk1
        router_sk = sk2
        router_vk = vk2

        request_env = random_envelope(sk=dealer_sk)
        reply_msg = random_msg()

        dealer = MPComposer(name='DEALER',
                            sk=sk1,
                            config_fn=config_dealer,
                            assert_fn=assert_dealer)
        router = MPComposer(config_fn=config_router,
                            assert_fn=assert_router,
                            name='ROUTER',
                            sk=router_sk)

        dealer.add_dealer(vk=router_vk)
        router.add_router(vk=router_vk)

        time.sleep(5.0)

        dealer.send_request_env(vk=router_vk, envelope=request_env)

        self.start()
Exemple #3
0
    def test_req_reply_1_1_1(self):
        """
        Tests request/reply 1_1_1
        """
        def config_router(composer: Composer):
            def reply(*args, **kwargs):  # do i need the *args **kwargs ??
                composer.send_reply(message=reply_msg,
                                    request_envelope=request_env)

            composer.interface.router = MagicMock()
            composer.interface.router.route_callback.side_effect = reply
            return composer

        def config_dealer(composer: Composer):
            composer.interface.router = MagicMock()
            return composer

        def assert_dealer(composer: Composer):
            args = composer.interface.router.route_callback.call_args_list

            assert len(
                args
            ) == 1, "dealer's route_callback should of only been called once (with the reply env)"

            call = args[0]
            callback_cmd = call[0][0]

            assert isinstance(
                callback_cmd, ReactorCommand
            ), "arg of route_callback should be a ReactorCommand"
            assert callback_cmd.envelope.message == reply_msg, "Callback's envelope's message should be the reply_msg"

        def assert_router(composer: Composer):
            cb = ReactorCommand.create_callback(callback=StateInput.REQUEST,
                                                envelope=request_env,
                                                header=dealer_id)
            composer.interface.router.route_callback.assert_called_once_with(
                cb)

        dealer_id = vk1
        dealer_sk = sk1
        router_sk = sk2
        router_url = URLS[1]

        request_env = random_envelope(sk=dealer_sk)
        reply_msg = random_msg()

        dealer = MPComposer(name='DEALER',
                            sk=sk1,
                            config_fn=config_dealer,
                            assert_fn=assert_dealer)
        router = MPComposer(config_fn=config_router,
                            assert_fn=assert_router,
                            name='ROUTER',
                            sk=router_sk)

        dealer.add_dealer(url=router_url)
        router.add_router(url=router_url)

        time.sleep(0.2)

        dealer.send_request_env(url=router_url, envelope=request_env)

        self.start()