Exemplo n.º 1
0
def test_pool_size_growth(create_pool, server, loop):
    pool = yield from create_pool(
        server.tcp_address,
        loop=loop,
        minsize=1, maxsize=1)

    done = set()
    tasks = []

    @asyncio.coroutine
    def task1(i):
        with (yield from pool):
            assert pool.size <= pool.maxsize
            assert pool.freesize == 0
            yield from asyncio.sleep(0.2, loop=loop)
            done.add(i)

    @asyncio.coroutine
    def task2():
        with (yield from pool):
            assert pool.size <= pool.maxsize
            assert pool.freesize >= 0
            assert done == {0, 1}

    for _ in range(2):
        tasks.append(async_task(task1(_), loop=loop))
    tasks.append(async_task(task2(), loop=loop))
    yield from asyncio.gather(*tasks, loop=loop)
Exemplo n.º 2
0
def test_finished_waiter_cancelled(loop):
    lock = Lock(loop=loop)

    ta = async_task(lock.acquire(), loop=loop)
    yield from asyncio.sleep(0, loop=loop)
    assert lock.locked()

    tb = async_task(lock.acquire(), loop=loop)
    yield from asyncio.sleep(0, loop=loop)
    assert len(lock._waiters) == 1

    # Create a second waiter, wake up the first, and cancel it.
    # Without the fix, the second was not woken up and the lock
    # will never be locked
    async_task(lock.acquire(), loop=loop)
    yield from asyncio.sleep(0, loop=loop)
    lock.release()
    tb.cancel()

    yield from asyncio.sleep(0, loop=loop)
    assert ta.done()
    assert tb.cancelled()

    yield from asyncio.sleep(0, loop=loop)
    assert lock.locked()
Exemplo n.º 3
0
def test_close_cancelled_pubsub_channel(redis, loop):
    ch, = yield from redis.subscribe('chan:1')

    @asyncio.coroutine
    def waiter(ch):
        with pytest.raises(asyncio.CancelledError):
            yield from ch.wait_message()

    tsk = async_task(waiter(ch), loop=loop)
    yield from asyncio.sleep(0, loop=loop)
    tsk.cancel()
Exemplo n.º 4
0
def test_close_pubsub_patterns(redis, loop):
    ch, = yield from redis.psubscribe('chan:*')

    @asyncio.coroutine
    def waiter(ch):
        assert not (yield from ch.wait_message())

    tsk = async_task(waiter(ch), loop=loop)
    redis.close()
    yield from redis.wait_closed()
    yield from tsk
Exemplo n.º 5
0
def test_publish(create_connection, redis, server, loop):
    out = asyncio.Queue(loop=loop)
    fut = create_future(loop=loop)
    conn = yield from create_connection(server.tcp_address, loop=loop)
    sub = async_task(_reader('chan:1', out, fut, conn), loop=loop)

    yield from fut
    yield from redis.publish('chan:1', 'Hello')
    msg = yield from out.get()
    assert msg == b'Hello'

    sub.cancel()
Exemplo n.º 6
0
    def test_cancel_wait_closed(self):
        # Regression test: Don't throw error if wait_closed() is cancelled.
        address = ('localhost', self.redis_port)
        conn = yield from self.create_connection(address, loop=self.loop)
        reader_task = conn._reader_task
        conn.close()
        task = async_task(conn.wait_closed(), loop=self.loop)

        # Make sure the task is cancelled
        # after it has been started by the loop.
        self.loop.call_soon(task.cancel)

        yield from conn.wait_closed()
        self.assertTrue(reader_task.done())
Exemplo n.º 7
0
def test_publish_json(create_connection, redis, server, loop):
    out = asyncio.Queue(loop=loop)
    fut = create_future(loop=loop)
    conn = yield from create_connection(server.tcp_address, loop=loop)
    sub = async_task(_reader('chan:1', out, fut, conn), loop=loop)

    yield from fut

    res = yield from redis.publish_json('chan:1', {"Hello": "world"})
    assert res == 1  # recievers

    msg = yield from out.get()
    assert msg == b'{"Hello": "world"}'
    sub.cancel()
Exemplo n.º 8
0
def test_cancel_wait_closed(create_connection, loop, server):
    # Regression test: Don't throw error if wait_closed() is cancelled.
    address = server.tcp_address
    conn = yield from create_connection(address, loop=loop)
    reader_task = conn._reader_task
    conn.close()
    task = async_task(conn.wait_closed(), loop=loop)

    # Make sure the task is cancelled
    # after it has been started by the loop.
    loop.call_soon(task.cancel)

    yield from conn.wait_closed()
    assert reader_task.done()
Exemplo n.º 9
0
def test_cancel_wait_closed(create_connection, loop, server):
    # Regression test: Don't throw error if wait_closed() is cancelled.
    address = server.tcp_address
    conn = yield from create_connection(address, loop=loop)
    reader_task = conn._reader_task
    conn.close()
    task = async_task(conn.wait_closed(), loop=loop)

    # Make sure the task is cancelled
    # after it has been started by the loop.
    loop.call_soon(task.cancel)

    yield from conn.wait_closed()
    assert reader_task.done()
Exemplo n.º 10
0
    def test_cancel_wait_closed(self):
        # Regression test: Don't throw error if wait_closed() is cancelled.
        address = ('localhost', self.redis_port)
        conn = yield from self.create_connection(address, loop=self.loop)
        reader_task = conn._reader_task
        conn.close()
        task = async_task(conn.wait_closed(), loop=self.loop)

        # Make sure the task is cancelled
        # after it has been started by the loop.
        self.loop.call_soon(task.cancel)

        yield from conn.wait_closed()
        self.assertTrue(reader_task.done())
Exemplo n.º 11
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 = Listener(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
Exemplo n.º 12
0
def test_channel_get_after_close(create_redis, loop, server):
    sub = yield from create_redis(server.tcp_address, loop=loop)
    pub = yield from create_redis(server.tcp_address, loop=loop)
    ch, = yield from sub.subscribe('chan:1')

    @asyncio.coroutine
    def waiter():
        while True:
            msg = yield from ch.get()
            if msg is None:
                break
            assert msg == b'message'

    tsk = async_task(waiter(), loop=loop)

    yield from pub.publish('chan:1', 'message')
    sub.close()
    yield from tsk
Exemplo n.º 13
0
def test_operations(pool_or_redis, test_case, pool_size, loop):
    repeat = 100
    redis = yield from pool_or_redis(pool_size)
    done, pending = yield from asyncio.wait(
        [async_task(test_case(redis, i, loop), loop=loop)
         for i in range(repeat)], loop=loop)

    assert not pending
    success = 0
    failures = []
    for fut in done:
        exc = fut.exception()
        if exc is None:
            success += 1
        else:
            failures.append(exc)
    assert repeat == success, failures
    assert not failures