Skip to content

Commit 5be96b9

Browse files
authored
Unit test fixes to carry pytest options through all tests (#1696)
1 parent 776dd59 commit 5be96b9

File tree

7 files changed

+36
-22
lines changed

7 files changed

+36
-22
lines changed

tests/conftest.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
REDIS_INFO = {}
1313
default_redis_url = "redis://localhost:6379/9"
14-
default_redismod_url = "redis://localhost:36379/9"
1514

1615
default_redismod_url = "redis://localhost:36379"
1716

@@ -44,10 +43,13 @@ def pytest_sessionstart(session):
4443
REDIS_INFO["version"] = version
4544
REDIS_INFO["arch_bits"] = arch_bits
4645

47-
# module info
48-
redismod_url = session.config.getoption("--redismod-url")
49-
info = _get_info(redismod_url)
50-
REDIS_INFO["modules"] = info["modules"]
46+
# module info, if the second redis is running
47+
try:
48+
redismod_url = session.config.getoption("--redismod-url")
49+
info = _get_info(redismod_url)
50+
REDIS_INFO["modules"] = info["modules"]
51+
except redis.exceptions.ConnectionError:
52+
pass
5153

5254

5355
def skip_if_server_version_lt(min_version):
@@ -72,7 +74,11 @@ def skip_unless_arch_bits(arch_bits):
7274

7375

7476
def skip_ifmodversion_lt(min_version: str, module_name: str):
75-
modules = REDIS_INFO["modules"]
77+
try:
78+
modules = REDIS_INFO["modules"]
79+
except KeyError:
80+
return pytest.mark.skipif(True,
81+
reason="Redis server does not have modules")
7682
if modules == []:
7783
return pytest.mark.skipif(True, reason="No redis modules found")
7884

@@ -218,7 +224,7 @@ def mock_cluster_resp_slaves(request, **kwargs):
218224
def master_host(request):
219225
url = request.config.getoption("--redis-url")
220226
parts = urlparse(url)
221-
yield parts.hostname
227+
yield parts.hostname, parts.port
222228

223229

224230
def wait_for_command(client, monitor, command):

tests/test_connection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ def test_invalid_response(r):
1818

1919

2020
@skip_if_server_version_lt('4.0.0')
21+
@pytest.mark.redismod
2122
def test_loaded_modules(r, modclient):
2223
assert r.loaded_modules == []
2324
assert 'rejson' in modclient.loaded_modules.keys()
2425

2526

2627
@skip_if_server_version_lt('4.0.0')
28+
@pytest.mark.redismod
2729
def test_loading_external_modules(r, modclient):
2830
def inner():
2931
pass

tests/test_connection_pool.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ def test_connection_creation(self):
4444
assert connection.kwargs == connection_kwargs
4545

4646
def test_multiple_connections(self, master_host):
47-
connection_kwargs = {'host': master_host}
47+
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
4848
pool = self.get_pool(connection_kwargs=connection_kwargs)
4949
c1 = pool.get_connection('_')
5050
c2 = pool.get_connection('_')
5151
assert c1 != c2
5252

5353
def test_max_connections(self, master_host):
54-
connection_kwargs = {'host': master_host}
54+
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
5555
pool = self.get_pool(max_connections=2,
5656
connection_kwargs=connection_kwargs)
5757
pool.get_connection('_')
@@ -60,7 +60,7 @@ def test_max_connections(self, master_host):
6060
pool.get_connection('_')
6161

6262
def test_reuse_previously_released_connection(self, master_host):
63-
connection_kwargs = {'host': master_host}
63+
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
6464
pool = self.get_pool(connection_kwargs=connection_kwargs)
6565
c1 = pool.get_connection('_')
6666
pool.release(c1)
@@ -103,22 +103,23 @@ def get_pool(self, connection_kwargs=None, max_connections=10, timeout=20):
103103
return pool
104104

105105
def test_connection_creation(self, master_host):
106-
connection_kwargs = {'foo': 'bar', 'biz': 'baz', 'host': master_host}
106+
connection_kwargs = {'foo': 'bar', 'biz': 'baz',
107+
'host': master_host[0], 'port': master_host[1]}
107108
pool = self.get_pool(connection_kwargs=connection_kwargs)
108109
connection = pool.get_connection('_')
109110
assert isinstance(connection, DummyConnection)
110111
assert connection.kwargs == connection_kwargs
111112

112113
def test_multiple_connections(self, master_host):
113-
connection_kwargs = {'host': master_host}
114+
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
114115
pool = self.get_pool(connection_kwargs=connection_kwargs)
115116
c1 = pool.get_connection('_')
116117
c2 = pool.get_connection('_')
117118
assert c1 != c2
118119

119120
def test_connection_pool_blocks_until_timeout(self, master_host):
120121
"When out of connections, block for timeout seconds, then raise"
121-
connection_kwargs = {'host': master_host}
122+
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
122123
pool = self.get_pool(max_connections=1, timeout=0.1,
123124
connection_kwargs=connection_kwargs)
124125
pool.get_connection('_')
@@ -134,7 +135,7 @@ def test_connection_pool_blocks_until_conn_available(self, master_host):
134135
When out of connections, block until another connection is released
135136
to the pool
136137
"""
137-
connection_kwargs = {'host': master_host}
138+
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
138139
pool = self.get_pool(max_connections=1, timeout=2,
139140
connection_kwargs=connection_kwargs)
140141
c1 = pool.get_connection('_')
@@ -149,7 +150,7 @@ def target():
149150
assert time.time() - start >= 0.1
150151

151152
def test_reuse_previously_released_connection(self, master_host):
152-
connection_kwargs = {'host': master_host}
153+
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
153154
pool = self.get_pool(connection_kwargs=connection_kwargs)
154155
c1 = pool.get_connection('_')
155156
pool.release(c1)

tests/test_monitor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ def test_wait_command_not_found(self, r):
99
assert response is None
1010

1111
def test_response_values(self, r):
12+
db = r.connection_pool.connection_kwargs.get('db', 0)
1213
with r.monitor() as m:
1314
r.ping()
1415
response = wait_for_command(r, m, 'PING')
1516
assert isinstance(response['time'], float)
16-
assert response['db'] == 9
17+
assert response['db'] == db
1718
assert response['client_type'] in ('tcp', 'unix')
1819
assert isinstance(response['client_address'], str)
1920
assert isinstance(response['client_port'], str)

tests/test_multiprocessing.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_close_connection_in_child(self, master_host):
3535
A connection owned by a parent and closed by a child doesn't
3636
destroy the file descriptors so a parent can still use it.
3737
"""
38-
conn = Connection(host=master_host)
38+
conn = Connection(host=master_host[0], port=master_host[1])
3939
conn.send_command('ping')
4040
assert conn.read_response() == b'PONG'
4141

@@ -61,7 +61,7 @@ def test_close_connection_in_parent(self, master_host):
6161
A connection owned by a parent is unusable by a child if the parent
6262
(the owning process) closes the connection.
6363
"""
64-
conn = Connection(host=master_host)
64+
conn = Connection(host=master_host[0], port=master_host[1])
6565
conn.send_command('ping')
6666
assert conn.read_response() == b'PONG'
6767

@@ -89,7 +89,9 @@ def test_pool(self, max_connections, master_host):
8989
A child will create its own connections when using a pool created
9090
by a parent.
9191
"""
92-
pool = ConnectionPool.from_url('redis://{}'.format(master_host),
92+
pool = ConnectionPool.from_url('redis://{}:{}'.format(master_host[0],
93+
master_host[1],
94+
),
9395
max_connections=max_connections)
9496

9597
conn = pool.get_connection('ping')
@@ -124,7 +126,8 @@ def test_close_pool_in_main(self, max_connections, master_host):
124126
A child process that uses the same pool as its parent isn't affected
125127
when the parent disconnects all connections within the pool.
126128
"""
127-
pool = ConnectionPool.from_url('redis://{}'.format(master_host),
129+
pool = ConnectionPool.from_url('redis://{}:{}'.format(master_host[0],
130+
master_host[1]),
128131
max_connections=max_connections)
129132

130133
conn = pool.get_connection('ping')

tests/test_pubsub.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,8 @@ def exception_handler(ex, pubsub, thread):
575575
class TestPubSubDeadlock:
576576
@pytest.mark.timeout(30, method='thread')
577577
def test_pubsub_deadlock(self, master_host):
578-
pool = redis.ConnectionPool(host=master_host)
578+
pool = redis.ConnectionPool(host=master_host[0],
579+
port=master_host[1])
579580
r = redis.Redis(connection_pool=pool)
580581

581582
for i in range(60):

tests/test_sentinel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@pytest.fixture(scope="module")
1212
def master_ip(master_host):
13-
yield socket.gethostbyname(master_host)
13+
yield socket.gethostbyname(master_host[0])
1414

1515

1616
class SentinelTestClient:

0 commit comments

Comments
 (0)