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

Commit f77a06b

Browse files
pfreixespopravich
authored andcommitted
Some small MR issues fixed
1 parent eaede3d commit f77a06b

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Samuel Colvin
2121
Ihor Gorobets
2222
Thanos Lefteris
2323
Ilya Goncharov
24+
Pau Freixes

aioredis/connection.py

+4
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ def create_connection(address, *, db=None, password=None, ssl=None,
6767
"""
6868
assert isinstance(address, (tuple, list, str)), "tuple or str expected"
6969

70+
if timeout is not None and timeout <= 0:
71+
raise ValueError("Timeout has to be None or a number greater than 0")
72+
7073
if isinstance(address, (list, tuple)):
7174
host, port = address
7275
logger.debug("Creating tcp connection to %r", address)
76+
print(asyncio.open_connection)
7377
reader, writer = yield from asyncio.wait_for(asyncio.open_connection(
7478
host, port, ssl=ssl, loop=loop), timeout, loop=loop)
7579
sock = writer.transport.get_extra_info('socket')

aioredis/pool.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@asyncio.coroutine
1616
def create_pool(address, *, db=0, password=None, ssl=None, encoding=None,
1717
minsize=1, maxsize=10, commands_factory=_NOTSET,
18-
loop=None, timeout_create_connection=None):
18+
loop=None, create_connection_timeout=None):
1919
# FIXME: rewrite docstring
2020
"""Creates Redis Pool.
2121
@@ -39,7 +39,7 @@ def create_pool(address, *, db=0, password=None, ssl=None, encoding=None,
3939
pool = RedisPool(address, db, password, encoding,
4040
minsize=minsize, maxsize=maxsize,
4141
commands_factory=commands_factory,
42-
timeout_create_connection=timeout_create_connection,
42+
create_connection_timeout=create_connection_timeout,
4343
ssl=ssl, loop=loop)
4444
try:
4545
yield from pool._fill_free(override_min=False)
@@ -57,7 +57,7 @@ class RedisPool:
5757

5858
def __init__(self, address, db=0, password=None, encoding=None,
5959
*, minsize, maxsize, commands_factory, ssl=None,
60-
timeout_create_connection=None, loop=None):
60+
create_connection_timeout=None, loop=None):
6161
assert isinstance(minsize, int) and minsize >= 0, (
6262
"minsize must be int >= 0", minsize, type(minsize))
6363
assert maxsize is not None, "Arbitrary pool size is disallowed."
@@ -74,7 +74,7 @@ def __init__(self, address, db=0, password=None, encoding=None,
7474
self._encoding = encoding
7575
self._minsize = minsize
7676
self._factory = commands_factory
77-
self._timeout_create_connection = timeout_create_connection
77+
self._create_connection_timeout = create_connection_timeout
7878
self._loop = loop
7979
self._pool = collections.deque(maxlen=maxsize)
8080
self._used = set()

docs/api_reference.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Connection usage is as simple as:
6767
:param timeout: Max time used to open a connection, otherwise
6868
raise `asyncio.TimeoutError` exception.
6969
``None`` by default
70-
:type timeout: float or None
70+
:type timeout: float greater than 0 or None
7171

7272
:return: :class:`RedisConnection` instance.
7373

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

249249
.. versionchanged:: v0.3.1
250-
``timeout_create_connection`` argument added.
250+
``create_connection_timeout`` argument added.
251251

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

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

290290
:return: :class:`RedisPool` instance.
291291

tests/connection_test.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import asyncio
33
import sys
44

5-
from aioredis.util import async_task
5+
from unittest.mock import patch
66

7+
from aioredis.util import async_task
78
from aioredis import (
89
ConnectionClosedError,
910
ProtocolError,
@@ -34,7 +35,18 @@ def test_connect_tcp(request, create_connection, loop, server):
3435

3536
@pytest.mark.run_loop
3637
def test_connect_tcp_timeout(request, create_connection, loop, server):
37-
with pytest.raises(asyncio.TimeoutError):
38+
with patch('aioredis.connection.asyncio.open_connection') as\
39+
open_conn_mock:
40+
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2,
41+
loop=loop)
42+
with pytest.raises(asyncio.TimeoutError):
43+
yield from create_connection(
44+
server.tcp_address, loop=loop, timeout=0.1)
45+
46+
47+
@pytest.mark.run_loop
48+
def test_connect_tcp_invalid_timeout(request, create_connection, loop, server):
49+
with pytest.raises(ValueError):
3850
yield from create_connection(
3951
server.tcp_address, loop=loop, timeout=0)
4052

@@ -54,9 +66,13 @@ def test_connect_unixsocket(create_connection, loop, server):
5466
@pytest.mark.skipif(sys.platform == 'win32',
5567
reason="No unixsocket on Windows")
5668
def test_connect_unixsocket_timeout(create_connection, loop, server):
57-
with pytest.raises(asyncio.TimeoutError):
58-
yield from create_connection(
59-
server.unixsocket, db=0, loop=loop, timeout=0)
69+
with patch('aioredis.connection.asyncio.open_unix_connection') as\
70+
open_conn_mock:
71+
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2,
72+
loop=loop)
73+
with pytest.raises(asyncio.TimeoutError):
74+
yield from create_connection(
75+
server.unixsocket, db=0, loop=loop, timeout=0.1)
6076

6177

6278
def test_global_loop(create_connection, loop, server):

tests/pool_test.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import pytest
33

4+
from unittest.mock import patch
5+
46
from aioredis import (
57
RedisPool,
68
ReplyError,
@@ -59,10 +61,14 @@ def test_maxsize(maxsize, create_pool, loop, server):
5961

6062
@pytest.mark.run_loop
6163
def test_create_connection_timeout(create_pool, loop, server):
62-
with pytest.raises(asyncio.TimeoutError):
63-
yield from create_pool(
64-
server.tcp_address, loop=loop,
65-
timeout_create_connection=0)
64+
with patch('asyncio.open_connection') as\
65+
open_conn_mock:
66+
open_conn_mock.side_effect = lambda *a, **kw: asyncio.sleep(0.2,
67+
loop=loop)
68+
with pytest.raises(asyncio.TimeoutError):
69+
yield from create_pool(
70+
server.tcp_address, loop=loop,
71+
create_connection_timeout=0.1)
6672

6773

6874
def test_no_yield_from(pool):

0 commit comments

Comments
 (0)