summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandy <andy@whiskeymedia.com>2013-06-04 17:23:22 -0400
committerandy <andy@whiskeymedia.com>2013-06-04 17:23:22 -0400
commit4b0e43eedc457b7f3c41885427c3fc5bd4ade7d1 (patch)
tree7c8f2072e2d06cf5539fa643906d420ebecc250f
parent60704548fa7f941b27db307c0a191aa4b6e993a5 (diff)
downloadredis-py-4b0e43eedc457b7f3c41885427c3fc5bd4ade7d1.tar.gz
connection pool tests
-rw-r--r--redis/_compat.py2
-rw-r--r--tests/test_connection_pool.py (renamed from tests/connection_pool.py)53
2 files changed, 23 insertions, 32 deletions
diff --git a/redis/_compat.py b/redis/_compat.py
index 564ade7..38a7316 100644
--- a/redis/_compat.py
+++ b/redis/_compat.py
@@ -6,6 +6,7 @@ if sys.version_info[0] < 3:
from urlparse import urlparse
from itertools import imap, izip
from string import letters as ascii_letters
+ from Queue import Queue
try:
from cStringIO import StringIO as BytesIO
except ImportError:
@@ -30,6 +31,7 @@ else:
from urllib.parse import urlparse
from io import BytesIO
from string import ascii_letters
+ from queue import Queue
iteritems = lambda x: iter(x.items())
iterkeys = lambda x: iter(x.keys())
diff --git a/tests/connection_pool.py b/tests/test_connection_pool.py
index 673c2f7..ae256aa 100644
--- a/tests/connection_pool.py
+++ b/tests/test_connection_pool.py
@@ -1,7 +1,10 @@
import os
-import unittest
-
+import pytest
import redis
+import time
+
+from threading import Thread
+from redis._compat import Queue
class DummyConnection(object):
@@ -10,7 +13,7 @@ class DummyConnection(object):
self.pid = os.getpid()
-class ConnectionPoolTestCase(unittest.TestCase):
+class TestConnectionPoolCase(object):
def get_pool(self, connection_info=None, max_connections=None):
connection_info = connection_info or {'a': 1, 'b': 2, 'c': 3}
pool = redis.ConnectionPool(
@@ -22,35 +25,30 @@ class ConnectionPoolTestCase(unittest.TestCase):
connection_info = {'foo': 'bar', 'biz': 'baz'}
pool = self.get_pool(connection_info=connection_info)
connection = pool.get_connection('_')
- self.assertEquals(connection.kwargs, connection_info)
+ assert connection.kwargs == connection_info
def test_multiple_connections(self):
pool = self.get_pool()
c1 = pool.get_connection('_')
c2 = pool.get_connection('_')
- self.assert_(c1 != c2)
+ assert c1 != c2
def test_max_connections(self):
pool = self.get_pool(max_connections=2)
pool.get_connection('_')
pool.get_connection('_')
- self.assertRaises(redis.ConnectionError, pool.get_connection, '_')
+ with pytest.raises(redis.ConnectionError):
+ pool.get_connection('_')
- def test_blocking_max_connections(self):
- pool = self.get_pool(max_connections=2)
- pool.get_connection('_')
- pool.get_connection('_')
- self.assertRaises(redis.ConnectionError, pool.get_connection, '_')
-
- def test_release(self):
+ def test_reuse_previously_released_connection(self):
pool = self.get_pool()
c1 = pool.get_connection('_')
pool.release(c1)
c2 = pool.get_connection('_')
- self.assertEquals(c1, c2)
+ assert c1 == c2
-class BlockingConnectionPoolTestCase(unittest.TestCase):
+class TestBlockingConnectionPool(object):
def get_pool(self, connection_info=None, max_connections=10, timeout=20):
connection_info = connection_info or {'a': 1, 'b': 2, 'c': 3}
pool = redis.BlockingConnectionPool(connection_class=DummyConnection,
@@ -62,26 +60,16 @@ class BlockingConnectionPoolTestCase(unittest.TestCase):
connection_info = {'foo': 'bar', 'biz': 'baz'}
pool = self.get_pool(connection_info=connection_info)
connection = pool.get_connection('_')
- self.assertEquals(connection.kwargs, connection_info)
+ assert connection.kwargs == connection_info
def test_multiple_connections(self):
pool = self.get_pool()
c1 = pool.get_connection('_')
c2 = pool.get_connection('_')
- self.assert_(c1 != c2)
+ assert c1 != c2
def test_max_connections_blocks(self):
"""Getting a connection should block for until available."""
-
- import time
- from threading import Thread
-
- # We use a queue for cross thread communication within the unit test.
- try: # Python 3
- from queue import Queue
- except ImportError:
- from Queue import Queue
-
q = Queue()
q.put_nowait('Not yet got')
pool = self.get_pool(max_connections=2, timeout=5)
@@ -94,13 +82,13 @@ class BlockingConnectionPoolTestCase(unittest.TestCase):
# Blocks while non available.
time.sleep(0.05)
c3 = q.get_nowait()
- self.assertEquals(c3, 'Not yet got')
+ assert c3 == 'Not yet got'
# Then got when available.
pool.release(c1)
time.sleep(0.05)
c3 = q.get_nowait()
- self.assertEquals(c1, c3)
+ assert c1 == c3
def test_max_connections_timeout(self):
"""Getting a connection raises ``ConnectionError`` after timeout."""
@@ -108,11 +96,12 @@ class BlockingConnectionPoolTestCase(unittest.TestCase):
pool = self.get_pool(max_connections=2, timeout=0.1)
pool.get_connection('_')
pool.get_connection('_')
- self.assertRaises(redis.ConnectionError, pool.get_connection, '_')
+ with pytest.raises(redis.ConnectionError):
+ pool.get_connection('_')
- def test_release(self):
+ def test_reuse_previously_released_connection(self):
pool = self.get_pool()
c1 = pool.get_connection('_')
pool.release(c1)
c2 = pool.get_connection('_')
- self.assertEquals(c1, c2)
+ assert c1 == c2