Ejemplo n.º 1
0
def test_stopped(create_connection, server, loop):
    sub = yield from create_connection(server.tcp_address, loop=loop)
    pub = yield from create_connection(server.tcp_address, loop=loop)

    mpsc = Receiver(loop=loop)
    yield from sub.execute_pubsub('subscribe', mpsc.channel('channel:1'))
    assert mpsc.is_active
    mpsc.stop()

    with pytest.logs('aioredis', 'DEBUG') as cm:
        yield from pub.execute('publish', 'channel:1', b'Hello')
        yield from asyncio.sleep(0, loop=loop)

    assert len(cm.output) == 1
    # Receiver must have 1 EndOfStream message
    warn_messaege = (
        "WARNING:aioredis:Pub/Sub listener message after stop: "
        "sender: <_Sender name:b'channel:1', is_pattern:False, receiver:"
        "<Receiver is_active:True, senders:1, qsize:1>>, data: b'Hello'")
    assert cm.output == [warn_messaege]

    assert (yield from mpsc.get()) is None
    with pytest.raises(ChannelClosedError):
        yield from mpsc.get()
    res = yield from mpsc.wait_message()
    assert res is False
Ejemplo n.º 2
0
def test_decode_message_error(loop):
    mpsc = Receiver(loop)
    ch = mpsc.channel('channel:1')

    ch.put_nowait(b'{"hello": "world"}')
    unexpected = (mock.ANY, {'hello': 'world'})
    with pytest.raises(TypeError):
        assert (yield from mpsc.get(decoder=json.loads)) == unexpected

    ch = mpsc.pattern('*')
    ch.put_nowait((b'channel', b'{"hello": "world"}'))
    unexpected = (mock.ANY, b'channel', {'hello': 'world'})
    with pytest.raises(TypeError):
        assert (yield from mpsc.get(decoder=json.loads)) == unexpected
Ejemplo n.º 3
0
def test_decode_message_for_pattern(loop):
    mpsc = Receiver(loop)
    ch = mpsc.pattern('*')
    ch.put_nowait((b'channel', b'Some data'))

    res = yield from mpsc.get(encoding='utf-8')
    assert isinstance(res[0], _Sender)
    assert res[1] == (b'channel', 'Some data')

    ch.put_nowait((b'channel', '{"hello": "world"}'))
    res = yield from mpsc.get(decoder=json.loads)
    assert isinstance(res[0], _Sender)
    assert res[1] == (b'channel', {'hello': 'world'})

    ch.put_nowait((b'channel', b'{"hello": "world"}'))
    res = yield from mpsc.get(encoding='utf-8', decoder=json.loads)
    assert isinstance(res[0], _Sender)
    assert res[1] == (b'channel', {'hello': 'world'})
Ejemplo n.º 4
0
def test_decode_message(loop):
    mpsc = Receiver(loop)
    ch = mpsc.channel('channel:1')
    ch.put_nowait(b'Some data')

    res = yield from mpsc.get(encoding='utf-8')
    assert isinstance(res[0], _Sender)
    assert res[1] == 'Some data'

    ch.put_nowait('{"hello": "world"}')
    res = yield from mpsc.get(decoder=json.loads)
    assert isinstance(res[0], _Sender)
    assert res[1] == {'hello': 'world'}

    ch.put_nowait(b'{"hello": "world"}')
    res = yield from mpsc.get(encoding='utf-8', decoder=json.loads)
    assert isinstance(res[0], _Sender)
    assert res[1] == {'hello': 'world'}
Ejemplo n.º 5
0
def test_unsubscribe(create_connection, server, loop):
    sub = yield from create_connection(server.tcp_address, loop=loop)
    pub = yield from create_connection(server.tcp_address, loop=loop)

    mpsc = Receiver(loop=loop)
    yield from sub.execute_pubsub('subscribe',
                                  mpsc.channel('channel:1'),
                                  mpsc.channel('channel:3'))
    res = yield from pub.execute("publish", "channel:3", "Hello world")
    assert res == 1
    res = yield from pub.execute("publish", "channel:1", "Hello world")
    assert res == 1
    assert mpsc.is_active

    assert (yield from mpsc.wait_message()) is True
    ch, msg = yield from mpsc.get()
    assert ch.name == b'channel:3'
    assert not ch.is_pattern
    assert msg == b"Hello world"

    assert (yield from mpsc.wait_message()) is True
    ch, msg = yield from mpsc.get()
    assert ch.name == b'channel:1'
    assert not ch.is_pattern
    assert msg == b"Hello world"

    yield from sub.execute_pubsub('unsubscribe', 'channel:1')
    assert mpsc.is_active

    res = yield from pub.execute("publish", "channel:3", "message")
    assert res == 1
    assert (yield from mpsc.wait_message()) is True
    ch, msg = yield from mpsc.get()
    assert ch.name == b'channel:3'
    assert not ch.is_pattern
    assert msg == b"message"

    yield from sub.execute_pubsub('unsubscribe', 'channel:3')
    assert not mpsc.is_active
    res = yield from mpsc.get()
    assert res is None
Ejemplo n.º 6
0
def test_subscriptions(create_connection, server, loop):
    sub = yield from create_connection(server.tcp_address, loop=loop)
    pub = yield from create_connection(server.tcp_address, loop=loop)

    mpsc = Receiver(loop=loop)
    yield from sub.execute_pubsub('subscribe', mpsc.channel('channel:1'),
                                  mpsc.channel('channel:3'))
    res = yield from pub.execute("publish", "channel:3", "Hello world")
    assert res == 1
    res = yield from pub.execute("publish", "channel:1", "Hello world")
    assert res == 1
    assert mpsc.is_active

    ch, msg = yield from mpsc.get()
    assert ch.name == b'channel:3'
    assert not ch.is_pattern
    assert msg == b"Hello world"

    ch, msg = yield from mpsc.get()
    assert ch.name == b'channel:1'
    assert not ch.is_pattern
    assert msg == b"Hello world"
Ejemplo n.º 7
0
async def test_unsubscribe(create_connection, server):
    sub = await create_connection(server.tcp_address)
    pub = await create_connection(server.tcp_address)

    mpsc = Receiver()
    await sub.execute_pubsub(
        "subscribe", mpsc.channel("channel:1"), mpsc.channel("channel:3")
    )
    res = await pub.execute("publish", "channel:3", "Hello world")
    assert res == 1
    res = await pub.execute("publish", "channel:1", "Hello world")
    assert res == 1
    assert mpsc.is_active

    assert (await mpsc.wait_message()) is True
    ch, msg = await mpsc.get()
    assert ch.name == b"channel:3"
    assert not ch.is_pattern
    assert msg == b"Hello world"

    assert (await mpsc.wait_message()) is True
    ch, msg = await mpsc.get()
    assert ch.name == b"channel:1"
    assert not ch.is_pattern
    assert msg == b"Hello world"

    await sub.execute_pubsub("unsubscribe", "channel:1")
    assert mpsc.is_active

    res = await pub.execute("publish", "channel:3", "message")
    assert res == 1
    assert (await mpsc.wait_message()) is True
    ch, msg = await mpsc.get()
    assert ch.name == b"channel:3"
    assert not ch.is_pattern
    assert msg == b"message"

    waiter = asyncio.ensure_future(mpsc.get())
    await sub.execute_pubsub("unsubscribe", "channel:3")
    assert not mpsc.is_active
    assert await waiter is None
Ejemplo n.º 8
0
async def test_unsubscribe(create_connection, server, loop):
    sub = await create_connection(server.tcp_address, loop=loop)
    pub = await create_connection(server.tcp_address, loop=loop)

    mpsc = Receiver(loop=loop)
    await sub.execute_pubsub('subscribe',
                             mpsc.channel('channel:1'),
                             mpsc.channel('channel:3'))
    res = await pub.execute("publish", "channel:3", "Hello world")
    assert res == 1
    res = await pub.execute("publish", "channel:1", "Hello world")
    assert res == 1
    assert mpsc.is_active

    assert (await mpsc.wait_message()) is True
    ch, msg = await mpsc.get()
    assert ch.name == b'channel:3'
    assert not ch.is_pattern
    assert msg == b"Hello world"

    assert (await mpsc.wait_message()) is True
    ch, msg = await mpsc.get()
    assert ch.name == b'channel:1'
    assert not ch.is_pattern
    assert msg == b"Hello world"

    await sub.execute_pubsub('unsubscribe', 'channel:1')
    assert mpsc.is_active

    res = await pub.execute("publish", "channel:3", "message")
    assert res == 1
    assert (await mpsc.wait_message()) is True
    ch, msg = await mpsc.get()
    assert ch.name == b'channel:3'
    assert not ch.is_pattern
    assert msg == b"message"

    waiter = asyncio.ensure_future(mpsc.get(), loop=loop)
    await sub.execute_pubsub('unsubscribe', 'channel:3')
    assert not mpsc.is_active
    assert await waiter is None