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_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.º 3
0
async def test_wait_message(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'))
    fut = asyncio.ensure_future(mpsc.wait_message(), loop=loop)
    assert not fut.done()
    await asyncio.sleep(0, loop=loop)
    assert not fut.done()

    await pub.execute('publish', 'channel:1', 'hello')
    await asyncio.sleep(0, loop=loop)  # read in connection
    await asyncio.sleep(0, loop=loop)  # call Future.set_result
    assert fut.done()
    res = await fut
    assert res is True
Ejemplo n.º 4
0
async def test_wait_message(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"))
    fut = asyncio.ensure_future(mpsc.wait_message())
    assert not fut.done()
    await asyncio.sleep(0)
    assert not fut.done()

    await pub.execute("publish", "channel:1", "hello")
    await asyncio.sleep(0)  # read in connection
    await asyncio.sleep(0)  # call Future.set_result
    assert fut.done()
    res = await fut
    assert res is True
Ejemplo n.º 5
0
def test_wait_message(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'))
    fut = async_task(mpsc.wait_message(), loop=loop)
    assert not fut.done()
    yield from asyncio.sleep(0, loop=loop)
    assert not fut.done()

    yield from pub.execute('publish', 'channel:1', 'hello')
    yield from asyncio.sleep(0, loop=loop)  # read in connection
    yield from asyncio.sleep(0, loop=loop)  # call Future.set_result
    assert fut.done()
    res = yield from fut
    assert res is True
Ejemplo n.º 6
0
async def test_wait_message(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'))
    fut = asyncio.ensure_future(mpsc.wait_message(), loop=loop)
    assert not fut.done()
    await asyncio.sleep(0, loop=loop)
    assert not fut.done()

    await pub.execute('publish', 'channel:1', 'hello')
    await asyncio.sleep(0, loop=loop)  # read in connection
    await asyncio.sleep(0, loop=loop)  # call Future.set_result
    assert fut.done()
    res = await fut
    assert res is True