Skip to content

Commit

Permalink
Unit test fixes to carry pytest options through all tests (#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
chayim authored Nov 10, 2021
1 parent 776dd59 commit 5be96b9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
20 changes: 13 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

REDIS_INFO = {}
default_redis_url = "redis://localhost:6379/9"
default_redismod_url = "redis://localhost:36379/9"

default_redismod_url = "redis://localhost:36379"

Expand Down Expand Up @@ -44,10 +43,13 @@ def pytest_sessionstart(session):
REDIS_INFO["version"] = version
REDIS_INFO["arch_bits"] = arch_bits

# module info
redismod_url = session.config.getoption("--redismod-url")
info = _get_info(redismod_url)
REDIS_INFO["modules"] = info["modules"]
# module info, if the second redis is running
try:
redismod_url = session.config.getoption("--redismod-url")
info = _get_info(redismod_url)
REDIS_INFO["modules"] = info["modules"]
except redis.exceptions.ConnectionError:
pass


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


def skip_ifmodversion_lt(min_version: str, module_name: str):
modules = REDIS_INFO["modules"]
try:
modules = REDIS_INFO["modules"]
except KeyError:
return pytest.mark.skipif(True,
reason="Redis server does not have modules")
if modules == []:
return pytest.mark.skipif(True, reason="No redis modules found")

Expand Down Expand Up @@ -218,7 +224,7 @@ def mock_cluster_resp_slaves(request, **kwargs):
def master_host(request):
url = request.config.getoption("--redis-url")
parts = urlparse(url)
yield parts.hostname
yield parts.hostname, parts.port


def wait_for_command(client, monitor, command):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ def test_invalid_response(r):


@skip_if_server_version_lt('4.0.0')
@pytest.mark.redismod
def test_loaded_modules(r, modclient):
assert r.loaded_modules == []
assert 'rejson' in modclient.loaded_modules.keys()


@skip_if_server_version_lt('4.0.0')
@pytest.mark.redismod
def test_loading_external_modules(r, modclient):
def inner():
pass
Expand Down
17 changes: 9 additions & 8 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ def test_connection_creation(self):
assert connection.kwargs == connection_kwargs

def test_multiple_connections(self, master_host):
connection_kwargs = {'host': master_host}
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
pool = self.get_pool(connection_kwargs=connection_kwargs)
c1 = pool.get_connection('_')
c2 = pool.get_connection('_')
assert c1 != c2

def test_max_connections(self, master_host):
connection_kwargs = {'host': master_host}
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
pool = self.get_pool(max_connections=2,
connection_kwargs=connection_kwargs)
pool.get_connection('_')
Expand All @@ -60,7 +60,7 @@ def test_max_connections(self, master_host):
pool.get_connection('_')

def test_reuse_previously_released_connection(self, master_host):
connection_kwargs = {'host': master_host}
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
pool = self.get_pool(connection_kwargs=connection_kwargs)
c1 = pool.get_connection('_')
pool.release(c1)
Expand Down Expand Up @@ -103,22 +103,23 @@ def get_pool(self, connection_kwargs=None, max_connections=10, timeout=20):
return pool

def test_connection_creation(self, master_host):
connection_kwargs = {'foo': 'bar', 'biz': 'baz', 'host': master_host}
connection_kwargs = {'foo': 'bar', 'biz': 'baz',
'host': master_host[0], 'port': master_host[1]}
pool = self.get_pool(connection_kwargs=connection_kwargs)
connection = pool.get_connection('_')
assert isinstance(connection, DummyConnection)
assert connection.kwargs == connection_kwargs

def test_multiple_connections(self, master_host):
connection_kwargs = {'host': master_host}
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
pool = self.get_pool(connection_kwargs=connection_kwargs)
c1 = pool.get_connection('_')
c2 = pool.get_connection('_')
assert c1 != c2

def test_connection_pool_blocks_until_timeout(self, master_host):
"When out of connections, block for timeout seconds, then raise"
connection_kwargs = {'host': master_host}
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
pool = self.get_pool(max_connections=1, timeout=0.1,
connection_kwargs=connection_kwargs)
pool.get_connection('_')
Expand All @@ -134,7 +135,7 @@ def test_connection_pool_blocks_until_conn_available(self, master_host):
When out of connections, block until another connection is released
to the pool
"""
connection_kwargs = {'host': master_host}
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
pool = self.get_pool(max_connections=1, timeout=2,
connection_kwargs=connection_kwargs)
c1 = pool.get_connection('_')
Expand All @@ -149,7 +150,7 @@ def target():
assert time.time() - start >= 0.1

def test_reuse_previously_released_connection(self, master_host):
connection_kwargs = {'host': master_host}
connection_kwargs = {'host': master_host[0], 'port': master_host[1]}
pool = self.get_pool(connection_kwargs=connection_kwargs)
c1 = pool.get_connection('_')
pool.release(c1)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ def test_wait_command_not_found(self, r):
assert response is None

def test_response_values(self, r):
db = r.connection_pool.connection_kwargs.get('db', 0)
with r.monitor() as m:
r.ping()
response = wait_for_command(r, m, 'PING')
assert isinstance(response['time'], float)
assert response['db'] == 9
assert response['db'] == db
assert response['client_type'] in ('tcp', 'unix')
assert isinstance(response['client_address'], str)
assert isinstance(response['client_port'], str)
Expand Down
11 changes: 7 additions & 4 deletions tests/test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_close_connection_in_child(self, master_host):
A connection owned by a parent and closed by a child doesn't
destroy the file descriptors so a parent can still use it.
"""
conn = Connection(host=master_host)
conn = Connection(host=master_host[0], port=master_host[1])
conn.send_command('ping')
assert conn.read_response() == b'PONG'

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

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

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

conn = pool.get_connection('ping')
Expand Down
3 changes: 2 additions & 1 deletion tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,8 @@ def exception_handler(ex, pubsub, thread):
class TestPubSubDeadlock:
@pytest.mark.timeout(30, method='thread')
def test_pubsub_deadlock(self, master_host):
pool = redis.ConnectionPool(host=master_host)
pool = redis.ConnectionPool(host=master_host[0],
port=master_host[1])
r = redis.Redis(connection_pool=pool)

for i in range(60):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@pytest.fixture(scope="module")
def master_ip(master_host):
yield socket.gethostbyname(master_host)
yield socket.gethostbyname(master_host[0])


class SentinelTestClient:
Expand Down

0 comments on commit 5be96b9

Please sign in to comment.