Skip to content

Commit

Permalink
more tests and coverage up to 90%
Browse files Browse the repository at this point in the history
  • Loading branch information
Microndgt committed Aug 22, 2017
1 parent 836f7f9 commit d6c4246
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
3 changes: 2 additions & 1 deletion aiossdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .connection import create_connection, SSDBConnection
from .errors import SSDBError, ReplyError, ConnectionClosedError, ProtocolError
from .errors import SSDBError, ReplyError, ConnectionClosedError, ProtocolError, PoolClosedError
from .parser import SSDBParser
from .pool import create_pool, SSDBConnectionPool
from .client import Client

__version__ = '0.0.1'
__author__ = "Kevin Du"
Expand Down
1 change: 1 addition & 0 deletions aiossdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ def close(self):
if self._pool:
self._pool.close()
yield from self._pool.wait_closed()
self._pool = None
18 changes: 9 additions & 9 deletions aiossdb/log.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
import sys
# import os
# import sys
import logging


logger = logging.getLogger('aiossdb')

if os.environ.get("AIOSSDB_DEBUG"):
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(stream=sys.stderr)
handler.setFormatter(logging.Formatter(
"%(asctime)s %(name)s %(levelname)s %(message)s"))
logger.addHandler(handler)
os.environ["AIOSSDB_DEBUG"] = ""
# if os.environ.get("AIOSSDB_DEBUG"):
# logger.setLevel(logging.DEBUG)
# handler = logging.StreamHandler(stream=sys.stderr)
# handler.setFormatter(logging.Formatter(
# "%(asctime)s %(name)s %(levelname)s %(message)s"))
# logger.addHandler(handler)
# os.environ["AIOSSDB_DEBUG"] = ""
1 change: 1 addition & 0 deletions aiossdb/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def _do_close(self):
yield from asyncio.gather(*waiters, loop=self._loop)
self._closed = True

@asyncio.coroutine
def wait_closed(self):
"""等待直到连接池关闭,这里要等待的是所有连接池连接的关闭期物Future的完成"""
yield from asyncio.shield(self._waiter, loop=self._loop)
Expand Down
24 changes: 24 additions & 0 deletions tests/client_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from aiossdb import Client


@pytest.mark.asyncio
async def test_create_client(event_loop):
c = Client(loop=event_loop)
assert c._pool is None
pool = await c.get_pool()
assert pool is c._pool
assert c._pool.maxsize == c.max_connection
await c.close()
assert c._pool is None


@pytest.mark.asyncio
async def test_execute_command(event_loop):
c = Client(loop=event_loop)
await c.set('a', 1)
res = await c.get('a')
assert res[0] == '1'
assert c._pool is not None
await c.close()
assert c._pool is None
42 changes: 41 additions & 1 deletion tests/connection_pool_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from aiossdb import SSDBConnectionPool, SSDBConnection, ReplyError
from aiossdb import SSDBConnectionPool, SSDBConnection, ReplyError, PoolClosedError


def _assert_defaults(pool):
Expand All @@ -9,6 +9,7 @@ def _assert_defaults(pool):
assert pool.size == 1
assert pool.freesize == 1
assert pool._waiter is None
assert str(pool) == '<SSDBConnectionPool [size:[1:10], free:1]>'


@pytest.mark.asyncio
Expand All @@ -34,6 +35,7 @@ async def test_get_connection(pool):

@pytest.mark.asyncio
async def test_execute_commands(pool):
await pool.auth('')
await pool.execute('set', 'a', 1)

res = await pool.execute('get', 'a')
Expand Down Expand Up @@ -75,4 +77,42 @@ async def test_more_connections(pool):
assert pool.freesize == 2
assert pool.size == 2

conn1.close()
await conn1.wait_closed()

pool._drop_closed()

assert pool.freesize == 1
assert pool.size == 1

pool.close()
await pool.wait_closed()

assert pool.closed

with pytest.raises(PoolClosedError):
await pool.release(conn2)


@pytest.mark.asyncio
async def test_close_pool(pool):

conn, addr = await pool.get_connection()
await pool.release(conn)
assert pool.freesize == 1
assert pool.size == 1

conn.close()
await conn.wait_closed()

conn, addr = await pool.get_connection()
assert pool.freesize == 0
assert pool.size == 1

pool.close()
await pool.wait_closed()

assert pool.closed

with pytest.raises(PoolClosedError):
await pool.new_connection()
2 changes: 1 addition & 1 deletion tests/connection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ async def test_execute_commands(create_connection, event_loop, local_server):

conn = await create_connection(address, loop=event_loop, password='')

assert not conn.closed
assert not conn.closed

0 comments on commit d6c4246

Please sign in to comment.