Exemple #1
0
def test_pool_clear(mcache_params, loop):
    pool = MemcachePool(minsize=1, maxsize=5, loop=loop, **mcache_params)
    conn = yield from pool.acquire()
    pool.release(conn)
    assert pool.size() == 1
    yield from pool.clear()
    assert pool._pool.qsize() == 0
Exemple #2
0
 def test_pool_acquire_release(self):
     pool = MemcachePool('localhost', 11211,
                         minsize=1, maxsize=5, loop=self.loop)
     conn = yield from pool.acquire()
     self.assertIsInstance(conn.reader, asyncio.StreamReader)
     self.assertIsInstance(conn.writer, asyncio.StreamWriter)
     pool.release(conn)
Exemple #3
0
def test_pool_clear(mcache_params, loop):
    pool = MemcachePool(minsize=1, maxsize=5, loop=loop, **mcache_params)
    conn = yield from pool.acquire()
    pool.release(conn)
    assert pool.size() == 1
    yield from pool.clear()
    assert pool._pool.qsize() == 0
Exemple #4
0
 def test_pool_clear(self):
     pool = MemcachePool('localhost', 11211,
                         minsize=1, maxsize=5, loop=self.loop)
     conn = yield from pool.acquire()
     pool.release(conn)
     self.assertEqual(pool.size(), 1)
     yield from pool.clear()
     self.assertEqual(pool._pool.qsize(), 0)
Exemple #5
0
def test_bad_connection(mcache_params, loop):
    pool = MemcachePool(minsize=5, maxsize=1, loop=loop, **mcache_params)
    pool._host = "INVALID_HOST"
    assert pool.size() == 0
    with pytest.raises(Exception):
        conn = yield from pool.acquire()
        assert isinstance(conn.reader, asyncio.StreamReader)
        assert isinstance(conn.writer, asyncio.StreamWriter)
        pool.release(conn)
    assert pool.size() == 0
Exemple #6
0
def test_bad_connection(mcache_params, loop):
    pool = MemcachePool(minsize=5, maxsize=1, loop=loop, **mcache_params)
    pool._host = "INVALID_HOST"
    assert pool.size() == 0
    with pytest.raises(Exception):
        conn = yield from pool.acquire()
        assert isinstance(conn.reader, asyncio.StreamReader)
        assert isinstance(conn.writer, asyncio.StreamWriter)
        pool.release(conn)
    assert pool.size() == 0
Exemple #7
0
def test_pool_acquire_release2(mcache_params, loop):
    pool = MemcachePool(minsize=1, maxsize=5, loop=loop, **mcache_params)
    reader, writer = yield from asyncio.open_connection(
        mcache_params['host'], mcache_params['port'], loop=loop)
    # put dead connection to the pool
    writer.close()
    reader.feed_eof()
    conn = _connection(reader, writer)
    yield from pool._pool.put(conn)
    conn = yield from pool.acquire()
    assert isinstance(conn.reader, asyncio.StreamReader)
    assert isinstance(conn.writer, asyncio.StreamWriter)
Exemple #8
0
def test_pool_acquire_release2(mcache_params, loop):
    pool = MemcachePool(minsize=1, maxsize=5, loop=loop, **mcache_params)
    reader, writer = yield from asyncio.open_connection(
        mcache_params['host'], mcache_params['port'], loop=loop)
    # put dead connection to the pool
    writer.close()
    reader.feed_eof()
    conn = _connection(reader, writer)
    yield from pool._pool.put(conn)
    conn = yield from pool.acquire()
    assert isinstance(conn.reader, asyncio.StreamReader)
    assert isinstance(conn.writer, asyncio.StreamWriter)
Exemple #9
0
 def test_pool_acquire_release2(self):
     pool = MemcachePool('localhost', 11211,
                         minsize=1, maxsize=5, loop=self.loop)
     reader, writer = yield from asyncio.open_connection(
         'localhost', 11211, loop=self.loop)
     # put dead connection to the pool
     writer.close()
     reader.feed_eof()
     conn = _connection(reader, writer)
     yield from pool._pool.put(conn)
     conn = yield from pool.acquire()
     self.assertIsInstance(conn.reader, asyncio.StreamReader)
     self.assertIsInstance(conn.writer, asyncio.StreamWriter)
Exemple #10
0
def test_acquire_dont_create_new_connection_if_have_conn_in_pool(mcache_params,
                                                                 loop):
    pool = MemcachePool(minsize=1, maxsize=5, loop=loop, **mcache_params)
    assert pool.size() == 0

    # Add a valid connection
    _conn = yield from pool._create_new_conn()
    yield from pool._pool.put(_conn)
    assert pool.size() == 1

    conn = yield from pool.acquire()
    assert conn is _conn
    assert pool.size() == 1
Exemple #11
0
def test_acquire_dont_create_new_connection_if_have_conn_in_pool(mcache_params,
                                                                 loop):
    pool = MemcachePool(minsize=1, maxsize=5, loop=loop, **mcache_params)
    assert pool.size() == 0

    # Add a valid connection
    _conn = yield from pool._create_new_conn()
    yield from pool._pool.put(_conn)
    assert pool.size() == 1

    conn = yield from pool.acquire()
    assert conn is _conn
    assert pool.size() == 1
Exemple #12
0
def test_pool_is_full(mcache_params, loop):
    pool = MemcachePool(minsize=1, maxsize=2, loop=loop, **mcache_params)
    conn = yield from pool.acquire()

    # put garbage to the pool make it look like full
    mocked_conns = [_connection(0, 0), _connection(1, 1)]
    yield from pool._pool.put(mocked_conns[0])
    yield from pool._pool.put(mocked_conns[1])

    # try to return connection back
    assert pool.size() == 3
    pool.release(conn)
    assert pool.size() == 2
Exemple #13
0
    def test_pool_is_full(self):
        pool = MemcachePool('localhost', 11211,
                            minsize=1, maxsize=2, loop=self.loop)
        conn = yield from pool.acquire()

        # put garbage to the pool make it look like full
        mocked_conns = [_connection(0, 0), _connection(1, 1)]
        yield from pool._pool.put(mocked_conns[0])
        yield from pool._pool.put(mocked_conns[1])

        # try to return connection back
        self.assertEqual(pool.size(), 3)
        pool.release(conn)
        self.assertEqual(pool.size(), 2)
Exemple #14
0
def test_acquire_limit_maxsize(mcache_params,
                               loop):
    pool = MemcachePool(minsize=1, maxsize=1, loop=loop, **mcache_params)
    assert pool.size() == 0

    # Create up to max connections
    _conn = yield from pool.acquire()
    assert pool.size() == 1
    pool.release(_conn)

    @asyncio.coroutine
    def acquire_wait_release():
        conn = yield from pool.acquire()
        assert conn is _conn
        yield from asyncio.sleep(0.01, loop=loop)
        assert len(pool._in_use) == 1
        assert pool.size() == 1
        assert pool._pool.qsize() == 0
        pool.release(conn)

    yield from asyncio.gather(*([acquire_wait_release()] * 3), loop=loop)
    assert pool.size() == 1
    assert len(pool._in_use) == 0
Exemple #15
0
def test_pool_acquire_release(mcache_params, loop):
    pool = MemcachePool(minsize=1, maxsize=5, loop=loop, **mcache_params)
    conn = yield from pool.acquire()
    assert isinstance(conn.reader, asyncio.StreamReader)
    assert isinstance(conn.writer, asyncio.StreamWriter)
    pool.release(conn)
Exemple #16
0
def test_0_minsize(mcache_params, loop):
    pool = MemcachePool(minsize=0, maxsize=5, loop=loop, **mcache_params)
    conn = yield from pool.acquire()
    assert isinstance(conn.reader, asyncio.StreamReader)
    assert isinstance(conn.writer, asyncio.StreamWriter)
    pool.release(conn)