Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Some small MR issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
pfreixes authored and popravich committed Jun 21, 2017
1 parent eaede3d commit f77a06b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Samuel Colvin
Ihor Gorobets
Thanos Lefteris
Ilya Goncharov
Pau Freixes
4 changes: 4 additions & 0 deletions aioredis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ def create_connection(address, *, db=None, password=None, ssl=None,
"""
assert isinstance(address, (tuple, list, str)), "tuple or str expected"

if timeout is not None and timeout <= 0:
raise ValueError("Timeout has to be None or a number greater than 0")

if isinstance(address, (list, tuple)):
host, port = address
logger.debug("Creating tcp connection to %r", address)
print(asyncio.open_connection)
reader, writer = yield from asyncio.wait_for(asyncio.open_connection(
host, port, ssl=ssl, loop=loop), timeout, loop=loop)
sock = writer.transport.get_extra_info('socket')
Expand Down
8 changes: 4 additions & 4 deletions aioredis/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@asyncio.coroutine
def create_pool(address, *, db=0, password=None, ssl=None, encoding=None,
minsize=1, maxsize=10, commands_factory=_NOTSET,
loop=None, timeout_create_connection=None):
loop=None, create_connection_timeout=None):
# FIXME: rewrite docstring
"""Creates Redis Pool.
Expand All @@ -39,7 +39,7 @@ def create_pool(address, *, db=0, password=None, ssl=None, encoding=None,
pool = RedisPool(address, db, password, encoding,
minsize=minsize, maxsize=maxsize,
commands_factory=commands_factory,
timeout_create_connection=timeout_create_connection,
create_connection_timeout=create_connection_timeout,
ssl=ssl, loop=loop)
try:
yield from pool._fill_free(override_min=False)
Expand All @@ -57,7 +57,7 @@ class RedisPool:

def __init__(self, address, db=0, password=None, encoding=None,
*, minsize, maxsize, commands_factory, ssl=None,
timeout_create_connection=None, loop=None):
create_connection_timeout=None, loop=None):
assert isinstance(minsize, int) and minsize >= 0, (
"minsize must be int >= 0", minsize, type(minsize))
assert maxsize is not None, "Arbitrary pool size is disallowed."
Expand All @@ -74,7 +74,7 @@ def __init__(self, address, db=0, password=None, encoding=None,
self._encoding = encoding
self._minsize = minsize
self._factory = commands_factory
self._timeout_create_connection = timeout_create_connection
self._create_connection_timeout = create_connection_timeout
self._loop = loop
self._pool = collections.deque(maxlen=maxsize)
self._used = set()
Expand Down
8 changes: 4 additions & 4 deletions docs/api_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Connection usage is as simple as:
:param timeout: Max time used to open a connection, otherwise
raise `asyncio.TimeoutError` exception.
``None`` by default
:type timeout: float or None
:type timeout: float greater than 0 or None

:return: :class:`RedisConnection` instance.

Expand Down Expand Up @@ -247,7 +247,7 @@ The library provides connections pool. The basic usage is as follows:
*commands_factory* argument is deprecated and will be removed in *v0.3*.

.. versionchanged:: v0.3.1
``timeout_create_connection`` argument added.
``create_connection_timeout`` argument added.

:param address: An address where to connect. Can be a (host, port) tuple or
unix domain socket path string.
Expand Down Expand Up @@ -282,10 +282,10 @@ The library provides connections pool. The basic usage is as follows:
(uses :func:`asyncio.get_event_loop` if not specified).
:type loop: :ref:`EventLoop<asyncio-event-loop>`

:param timeout_create_connection: Max time used to open a connection,
:param create_connection_timeout: Max time used to open a connection,
otherwise raise an `asyncio.TimeoutError`.
``None`` by default.
:type timeout_create_connection: float or None
:type create_connection_timeout: float greater than 0 or None

:return: :class:`RedisPool` instance.

Expand Down
26 changes: 21 additions & 5 deletions tests/connection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import asyncio
import sys

from aioredis.util import async_task
from unittest.mock import patch

from aioredis.util import async_task
from aioredis import (
ConnectionClosedError,
ProtocolError,
Expand Down Expand Up @@ -34,7 +35,18 @@ def test_connect_tcp(request, create_connection, loop, server):

@pytest.mark.run_loop
def test_connect_tcp_timeout(request, create_connection, loop, server):
with pytest.raises(asyncio.TimeoutError):
with patch('aioredis.connection.asyncio.open_connection') as\
open_conn_mock:
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2,
loop=loop)
with pytest.raises(asyncio.TimeoutError):
yield from create_connection(
server.tcp_address, loop=loop, timeout=0.1)


@pytest.mark.run_loop
def test_connect_tcp_invalid_timeout(request, create_connection, loop, server):
with pytest.raises(ValueError):
yield from create_connection(
server.tcp_address, loop=loop, timeout=0)

Expand All @@ -54,9 +66,13 @@ def test_connect_unixsocket(create_connection, loop, server):
@pytest.mark.skipif(sys.platform == 'win32',
reason="No unixsocket on Windows")
def test_connect_unixsocket_timeout(create_connection, loop, server):
with pytest.raises(asyncio.TimeoutError):
yield from create_connection(
server.unixsocket, db=0, loop=loop, timeout=0)
with patch('aioredis.connection.asyncio.open_unix_connection') as\
open_conn_mock:
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2,
loop=loop)
with pytest.raises(asyncio.TimeoutError):
yield from create_connection(
server.unixsocket, db=0, loop=loop, timeout=0.1)


def test_global_loop(create_connection, loop, server):
Expand Down
14 changes: 10 additions & 4 deletions tests/pool_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import pytest

from unittest.mock import patch

from aioredis import (
RedisPool,
ReplyError,
Expand Down Expand Up @@ -59,10 +61,14 @@ def test_maxsize(maxsize, create_pool, loop, server):

@pytest.mark.run_loop
def test_create_connection_timeout(create_pool, loop, server):
with pytest.raises(asyncio.TimeoutError):
yield from create_pool(
server.tcp_address, loop=loop,
timeout_create_connection=0)
with patch('asyncio.open_connection') as\
open_conn_mock:
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2,
loop=loop)
with pytest.raises(asyncio.TimeoutError):
yield from create_pool(
server.tcp_address, loop=loop,
create_connection_timeout=0.1)


def test_no_yield_from(pool):
Expand Down

0 comments on commit f77a06b

Please sign in to comment.