Ejemplo n.º 1
0
    async def run() -> None:
        '''The main method that will be called by the entrypoint file.
        Do multiple RPC calls and display the result in console.
        '''

        broker: Symbios = Symbios(host=os.environ['RMQ_HOST'],
                                  vhost=os.environ['RMQ_VHOST'],
                                  user=os.environ['RMQ_USERNAME'],
                                  password=os.environ['RMQ_PASSWORD'])
        t0: float = time.time()

        print(f'{datetime.now()} > [*] Sending requests '
              f'and waiting for responses...')
        tasks_queue: List[Coroutine] = broker.rpc.multi_calls([
            broker.rpc.call(SendingMessage('1+1'),
                            routing_key=os.environ['RPC_QUEUE']),
            broker.rpc.call(SendingMessage('4*5'),
                            routing_key=os.environ['RPC_QUEUE']),
            broker.rpc.call(SendingMessage('6*4'),
                            routing_key=os.environ['RPC_QUEUE']),
            broker.rpc.call(SendingMessage('10*5'),
                            routing_key=os.environ['RPC_QUEUE']),
            broker.rpc.call(SendingMessage('2-6'),
                            routing_key=os.environ['RPC_QUEUE']),
        ])

        for task in tasks_queue:
            print(f'{datetime.now()} > [x] Received response '
                  f'from {task.get_coro()}. '
                  f'Message body: {(await task).deserialized}')

        print(f'{datetime.now()} > [x] End of process. '
              f'Total duration: {round(time.time() - t0, 2)}s')
Ejemplo n.º 2
0
    def test_sending_message(self, body: Any) -> None:
        '''Test the SendingMessage class atrributes integrity.
        '''

        message: SendingMessage = SendingMessage(body)

        assert message.body == body
Ejemplo n.º 3
0
        async def test() -> None:
            mocked_channel(await symbios.channel)

            message: SendingMessage = SendingMessage('Lapin')
            ack: EmitACK = await symbios.emit(message,
                                              routing_key='symbios_tests')

            assert isinstance(ack, EmitACK)
Ejemplo n.º 4
0
        async def test() -> None:
            mocked_symbios.event_loop.create_task(
                init_rpc_server(mocked_symbios))

            rpc: RPC = RPC(mocked_symbios)

            res = await rpc.call(SendingMessage('HI'),
                                 routing_key='symbios_tests')

            assert res.body == 'RPC_ACK'
Ejemplo n.º 5
0
    async def _on_receive_handler(broker: Symbios,
                                  message: IncomingMessage) -> None:
        print(f'{datetime.now()} > [x] Received requests. '
              f'Message body: {message.deserialized}.')
        res: float = await Main._compute(message.deserialized)

        await broker.emit(
            SendingMessage(res),
            routing_key=message.props.reply_to,
            props=Props(correlation_id=message.props.correlation_id),
        )
        print(f'{datetime.now()} > [x] Response sent.')
Ejemplo n.º 6
0
        async def test() -> None:
            mocked_symbios.event_loop.create_task(
                init_rpc_server(mocked_symbios))
            rpc: RPC = RPC(mocked_symbios)

            task_queue = rpc.multi_calls([
                rpc.call(SendingMessage('HI'), routing_key='symbios_tests')
                for _ in range(1)
            ])

            for task in task_queue:
                await task
                assert task.result().body == 'RPC_ACK'
Ejemplo n.º 7
0
        async def test() -> None:
            producer: Producer = Producer(
                symbios=symbios,
                exchange=exchange,
                routing_key=routing_key,
                props=props,
                mandatory=mandatory,
                immediate=immediate,
                midd_library=symbios._midd_library,
            )

            mocked_channel(await producer.symbios.channel)

            message: SendingMessage = SendingMessage('lapin')

            ack: EmitACK = await producer.emit(message)

            assert isinstance(ack, EmitACK)
Ejemplo n.º 8
0
class TestProducer:
    '''The Producer class tests.
    '''

    CONTENT_TYPE_DATASET: List[Tuple[SendingMessage, str]] = [
        (SendingMessage(''), 'text/plain'),
        (SendingMessage(3.14), 'text/plain'),
        (SendingMessage(42), 'text/plain'),
        (SendingMessage({}), 'application/json'),
    ]

    EMIT_DATASET: List[Tuple[Exchange, str, Props, bool, bool]] = [
        (
            Exchange('symbios_test_direct', exchange_type=Exchange.DIRECT),
            'symbios_test',
            Props(),
            False,
            False,
        ),
        (
            Exchange('symbios_test_fanout', exchange_type=Exchange.FANOUT),
            'symbios_test',
            Props(),
            False,
            False,
        ),
        (
            Exchange('symbios_test_topic', exchange_type=Exchange.TOPIC),
            'symbios_test',
            Props(),
            False,
            False,
        ),
        (
            Exchange('symbios_test_headers', exchange_type=Exchange.HEADERS),
            'symbios_test',
            Props(),
            False,
            False,
        ),
    ]

    @pytest.mark.parametrize(
        'exchange, routing_key, props, mandatory, immediate', EMIT_DATASET)
    def test_emit(
        self,
        symbios: Symbios,
        mocked_channel,
        run_async: Callable,
        exchange: Exchange,
        routing_key: str,
        props: Props,
        mandatory: bool,
        immediate: bool,
    ) -> None:
        '''Test the producer emitter.
        '''
        async def test() -> None:
            producer: Producer = Producer(
                symbios=symbios,
                exchange=exchange,
                routing_key=routing_key,
                props=props,
                mandatory=mandatory,
                immediate=immediate,
                midd_library=symbios._midd_library,
            )

            mocked_channel(await producer.symbios.channel)

            message: SendingMessage = SendingMessage('lapin')

            ack: EmitACK = await producer.emit(message)

            assert isinstance(ack, EmitACK)

        run_async(test)

    @pytest.mark.parametrize('message, content_type', CONTENT_TYPE_DATASET)
    def test__determine_content_type(self, symbios: Symbios,
                                     message: SendingMessage,
                                     content_type: str) -> None:
        '''Test the producer content_type determiner.
        '''

        producer: Producer = Producer(symbios=symbios,
                                      midd_library=symbios._midd_library)

        assert producer._determine_content_type(message) == content_type
Ejemplo n.º 9
0
 async def listen_handler(symbios_sck, message):
     await symbios_sck.emit(
         SendingMessage('RPC_ACK'),
         routing_key=message.props.reply_to,
         props=Props(correlation_id=message.props.correlation_id),
     )