Skip to content

Commit

Permalink
fix caplog tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Ilyushenkov committed Dec 12, 2023
1 parent d6f87fa commit 2a8164c
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions tests/unit_tests/aioredis_cluster/test_pooler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ def create_pool_mock():
return mocked


async def test_ensure_pool__identical_address():
async def test_ensure_pool__identical_address(add_async_finalizer):
mocked_create_pool = mock.AsyncMock(
return_value=create_pool_mock(),
)
pooler = Pooler(mocked_create_pool)
add_async_finalizer(lambda: pooler.close())

result = await pooler.ensure_pool(Address("localhost", 1234))

Expand All @@ -32,11 +33,12 @@ async def test_ensure_pool__identical_address():
assert mocked_create_pool.call_count == 1


async def test_ensure_pool__multiple():
async def test_ensure_pool__multiple(add_async_finalizer):
pools = [object(), object(), object()]
mocked_create_pool = mock.AsyncMock(side_effect=pools)

pooler = Pooler(mocked_create_pool)
add_async_finalizer(lambda: pooler.close())

result1 = await pooler.ensure_pool(Address("localhost", 1234))
result2 = await pooler.ensure_pool(Address("localhost", 4321))
Expand All @@ -55,7 +57,7 @@ async def test_ensure_pool__multiple():
)


async def test_ensure_pool__only_one():
async def test_ensure_pool__only_one(add_async_finalizer):
event_loop = asyncio.get_running_loop()
pools = {
("h1", 1): create_pool_mock(),
Expand All @@ -71,6 +73,7 @@ async def create_pool_se(addr):
mocked_create_pool = mock.AsyncMock(side_effect=create_pool_se)

pooler = Pooler(mocked_create_pool)
add_async_finalizer(lambda: pooler.close())

tasks = []
for i in range(10):
Expand All @@ -88,11 +91,12 @@ async def create_pool_se(addr):
assert mocked_create_pool.call_count == 2


async def test_ensure_pool__error():
async def test_ensure_pool__error(add_async_finalizer):
pools = [RuntimeError(), object()]
mocked_create_pool = mock.AsyncMock(side_effect=pools)

pooler = Pooler(mocked_create_pool)
add_async_finalizer(lambda: pooler.close())

addr = Address("localhost", 1234)
with pytest.raises(RuntimeError):
Expand All @@ -117,7 +121,7 @@ async def test_close__empty_pooler():
assert pooler.closed is True


async def test_close__with_pools(mocker):
async def test_close__with_pools(mocker, add_async_finalizer):
addrs_pools = [
(Address("h1", 1), create_pool_mock()),
(Address("h2", 2), create_pool_mock()),
Expand All @@ -127,6 +131,7 @@ async def test_close__with_pools(mocker):
mocked_create_pool = mock.AsyncMock(side_effect=pools)

pooler = Pooler(mocked_create_pool)
add_async_finalizer(lambda: pooler.close())

result1 = await pooler.ensure_pool(addrs[0])
result2 = await pooler.ensure_pool(addrs[1])
Expand All @@ -143,7 +148,7 @@ async def test_close__with_pools(mocker):
result2.wait_closed.assert_called_once()


async def test_reap_pools(mocker):
async def test_reap_pools(mocker, add_async_finalizer):
addrs_pools = [
(Address("h1", 1), create_pool_mock()),
(Address("h2", 2), create_pool_mock()),
Expand All @@ -153,6 +158,7 @@ async def test_reap_pools(mocker):
mocked_create_pool = mock.AsyncMock(side_effect=pools)

pooler = Pooler(mocked_create_pool, reap_frequency=-1)
add_async_finalizer(lambda: pooler.close())

# create pools
await pooler.ensure_pool(addrs[0])
Expand All @@ -176,8 +182,9 @@ async def test_reap_pools(mocker):
assert len(pooler._nodes) == 0


async def test_reaper(mocker):
async def test_reaper(mocker, add_async_finalizer):
pooler = Pooler(mock.AsyncMock(), reap_frequency=0)
add_async_finalizer(lambda: pooler.close())

assert pooler._reap_calls == 0

Expand All @@ -194,17 +201,19 @@ async def test_reaper(mocker):
assert pooler._reaper_task.cancelled() is True


async def test_add_pubsub_channel__no_addr():
async def test_add_pubsub_channel__no_addr(add_async_finalizer):
pooler = Pooler(mock.AsyncMock(), reap_frequency=-1)
add_async_finalizer(lambda: pooler.close())

addr = Address("h1", 1234)
result = pooler.add_pubsub_channel(addr, b"channel", is_pattern=False)

assert result is False


async def test_add_pubsub_channel():
async def test_add_pubsub_channel(add_async_finalizer):
pooler = Pooler(mock.AsyncMock(return_value=create_pool_mock()), reap_frequency=-1)
add_async_finalizer(lambda: pooler.close())

addr1 = Address("h1", 1234)
addr2 = Address("h2", 1234)
Expand Down Expand Up @@ -237,15 +246,17 @@ async def test_add_pubsub_channel():
assert (b"ch3", False) in collected_channels


async def test_remove_pubsub_channel__no_addr():
async def test_remove_pubsub_channel__no_addr(add_async_finalizer):
pooler = Pooler(mock.AsyncMock(), reap_frequency=-1)
add_async_finalizer(lambda: pooler.close())

result = pooler.remove_pubsub_channel(b"channel", is_pattern=False)
assert result is False


async def test_remove_pubsub_channel():
async def test_remove_pubsub_channel(add_async_finalizer):
pooler = Pooler(mock.AsyncMock(), reap_frequency=-1)
add_async_finalizer(lambda: pooler.close())

addr1 = Address("h1", 1234)
addr2 = Address("h2", 1234)
Expand Down Expand Up @@ -275,8 +286,9 @@ async def test_remove_pubsub_channel():
assert len(pooler._pubsub_channels) == 0


async def test_get_pubsub_addr():
async def test_get_pubsub_addr(add_async_finalizer):
pooler = Pooler(mock.AsyncMock(), reap_frequency=-1)
add_async_finalizer(lambda: pooler.close())

addr1 = Address("h1", 1234)
addr2 = Address("h2", 1234)
Expand All @@ -297,11 +309,12 @@ async def test_get_pubsub_addr():
assert result4 == addr2


async def test_ensure_pool__create_pubsub_addr_set():
async def test_ensure_pool__create_pubsub_addr_set(add_async_finalizer):
addr1 = Address("h1", 1234)
addr2 = Address("h2", 1234)

pooler = Pooler(mock.AsyncMock(return_value=create_pool_mock()))
add_async_finalizer(lambda: pooler.close())

assert len(pooler._pubsub_addrs) == 0

Expand All @@ -320,9 +333,10 @@ async def test_ensure_pool__create_pubsub_addr_set():
assert len(pooler._pubsub_addrs[addr1]) == 1


async def test_reap_pools__cleanup_channels():
async def test_reap_pools__cleanup_channels(add_async_finalizer):
pool_factory = mock.AsyncMock(return_value=mock.Mock(AbcPool))
pooler = Pooler(pool_factory, reap_frequency=-1)
add_async_finalizer(lambda: pooler.close())

addr1 = Address("h1", 1)
addr2 = Address("h2", 2)
Expand All @@ -346,12 +360,14 @@ async def test_reap_pools__cleanup_channels():
assert len(pooler._pubsub_channels) == 0


async def test_close_only():
async def test_close_only(add_async_finalizer):
pool1 = create_pool_mock()
pool2 = create_pool_mock()
pool3 = create_pool_mock()
mocked_create_pool = mock.AsyncMock(side_effect=[pool1, pool2, pool3])
pooler = Pooler(mocked_create_pool)
add_async_finalizer(lambda: pooler.close())

addr1 = Address("h1", 1)
addr2 = Address("h2", 2)

Expand Down

0 comments on commit 2a8164c

Please sign in to comment.