Ejemplo n.º 1
0
async def test_Bus_bind_ok(bus, test_signal, else_signal):
    receiver1 = Receiver(agent=None,
                         handler=Handler(),
                         signal=test_signal,
                         timeout=60)
    receiver2 = Receiver(agent=None,
                         handler=Handler(),
                         signal=test_signal,
                         timeout=60)
    receiver3 = Receiver(agent=None,
                         handler=Handler(),
                         signal=else_signal,
                         timeout=60)

    await bus.bind_receiver(receiver1)
    await bus.bind_receiver(receiver2)
    await bus.bind_receiver(receiver3)

    assert bus.receivers == {
        test_signal.name: [receiver1, receiver2],
        else_signal.name: [receiver3],
    }

    assert [x[0][0] for x in bus.bind.call_args_list
            ] == ['TEST:test_signal:*', 'TEST:else_signal:*']
Ejemplo n.º 2
0
async def test_Receiver_ok(test_signal):
    receiver = Receiver(agent=None,
                        handler=lambda: 1,
                        signal=test_signal,
                        timeout=60)
    assert 'Receiver' in str(receiver)
    assert receiver.agent is None
    assert receiver.handler() == 1
    assert receiver.signal is test_signal
    assert receiver.timeout == 60
Ejemplo n.º 3
0
async def test_Bus_broadcast_ok_sync(bus, test_signal):
    handler = MagicMock(return_value=1, **{'__qualname__': 'qname'})
    receiver = Receiver(agent=None,
                        handler=handler,
                        signal=test_signal,
                        timeout=60)
    assert await bus.broadcast(receiver, test_signal, 'test', {'uuid': 1}) == 1
Ejemplo n.º 4
0
async def test_Bus_broadcast_fail_type_failed(bus, test_signal):
    handler = MagicMock(return_value=1, **{'__qualname__': 'qname'})
    receiver = Receiver(agent=None,
                        handler=handler,
                        signal=test_signal,
                        timeout=60)
    receiver.handler.side_effect = TypeError()

    ret = await bus.broadcast(receiver, test_signal, 'test', {'uuid': 1})

    assert ret is None
    bus.log.exception.assert_called()
Ejemplo n.º 5
0
async def test_Bus_handle_signal_ok(bus):
    some_signal = Signal(name='some_signal', providing_args=[])
    receiver = Receiver(agent=None,
                        handler=Handler(),
                        signal=some_signal,
                        timeout=60)
    bus.receivers[some_signal.name] = [receiver]
    bus.broadcast = AsyncMock()

    await bus.handle_signal(some_signal, 'test', None, {})

    bus.broadcast.assert_called_with(receiver, some_signal, 'test', {})
Ejemplo n.º 6
0
async def test_Bus_broadcast_fail_timeout(bus, test_signal):
    handler = Handler(return_value=1, **{'__qualname__': 'qname'})
    receiver = Receiver(agent=None,
                        handler=handler,
                        signal=test_signal,
                        timeout=.01)

    async def foo(*args, **kwargs):
        await asyncio.sleep(1)

    receiver.handler.side_effect = foo

    ret = await bus.broadcast(receiver, test_signal, 'test', {'uuid': 1})

    assert ret is None
    bus.log.error.assert_called()
Ejemplo n.º 7
0
async def test_Bus_handle_signal_ok_response(bus):
    uid = str(uuid.uuid4().hex)
    some_signal = Signal(name='some_signal', providing_args=[])
    handler = Handler(**{'__qualname__': 'qname'})
    receiver = Receiver(agent=None,
                        handler=handler,
                        signal=some_signal,
                        timeout=60)
    bus.receivers[some_signal.name] = [receiver]
    bus.broadcast = AsyncMock(return_value=42)
    bus.send = AsyncMock()

    await bus.handle_signal(some_signal, 'test', uid, {})

    bus.broadcast.assert_called_with(receiver, some_signal, 'test', {})
    bus.send.assert_called_with(f'{bus.prefix}:response:{bus.uid}#{uid}',
                                f'{{"{receiver.key}":42}}')