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
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
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
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)
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
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)
class Client: def __init__(self, pool_size=4): self._pool = MemcachePool( minsize=pool_size, maxsize=pool_size, loop=loop, **mcache_params) @acquire @asyncio.coroutine def acquire_wait_release(self, conn): assert self._pool.size() <= pool_size yield from asyncio.sleep(random.uniform(0.01, 0.02), loop=loop) return "foo"
class Client: def __init__(self, pool_size=4): self._pool = MemcachePool(minsize=pool_size, maxsize=pool_size, loop=loop, **mcache_params) @acquire @asyncio.coroutine def acquire_wait_release(self, conn): assert self._pool.size() <= pool_size yield from asyncio.sleep(random.uniform(0.01, 0.02), loop=loop) return "foo"
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
def test_pool_creation(self): pool = MemcachePool('localhost', 11211, minsize=1, maxsize=5, loop=self.loop) self.assertEqual(pool.size(), 0) self.assertEqual(pool._minsize, 1)
def test_pool_creation(mcache_params, loop): pool = MemcachePool(minsize=1, maxsize=5, loop=loop, **mcache_params) assert pool.size() == 0 assert pool._minsize == 1