Skip to content

Commit

Permalink
Fixing tests in python 3.8.1. Filed issue with pytest-mock: pytest-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcharnock committed Jan 12, 2020
1 parent d7a2520 commit 43e84a1
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions tests/transports/test_pool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import sys
from asyncio import Future
from concurrent.futures.thread import ThreadPoolExecutor
from concurrent.futures import wait
Expand Down Expand Up @@ -215,9 +216,21 @@ async def test_checkout_checkin_threaded(
Note that this will not grow the pool. See the doc string for TransportPool
"""
mocker.spy(redis_pool, "grow")
mocker.spy(redis_pool, "_create_transport")
mocker.spy(redis_pool, "_close_transport")

# mocker.spy in pytest-mock runs afoul of this change in 3.8.1
# https://bugs.python.org/issue38857
# We therefore use mocker.spy for python 3.7, or the new AsyncMock in 3.8.
# See: https://github.com/pytest-dev/pytest-mock/issues/178
if sys.version_info >= (3, 8):
from unittest.mock import AsyncMock

redis_pool.grow = AsyncMock(wraps=redis_pool.grow)
redis_pool._create_transport = AsyncMock(wraps=redis_pool._create_transport)
redis_pool._close_transport = AsyncMock(wraps=redis_pool._close_transport)
else:
mocker.spy(redis_pool, "grow")
mocker.spy(redis_pool, "_create_transport")
mocker.spy(redis_pool, "_close_transport")

async def _check_in_out():
transport = await redis_pool.checkout()
Expand Down Expand Up @@ -247,9 +260,20 @@ async def test_checkout_checkin_asyncio(
Unlike using threads, this should grow the pool
"""
mocker.spy(redis_pool, "grow")
mocker.spy(redis_pool, "_create_transport")
mocker.spy(redis_pool, "_close_transport")
# mocker.spy in pytest-mock runs afoul of this change in 3.8.1
# https://bugs.python.org/issue38857
# We therefore use mocker.spy for python 3.7, or the new AsyncMock in 3.8
# See: https://github.com/pytest-dev/pytest-mock/issues/178
if sys.version_info >= (3, 8):
from unittest.mock import AsyncMock

redis_pool.grow = AsyncMock(wraps=redis_pool.grow)
redis_pool._create_transport = AsyncMock(wraps=redis_pool._create_transport)
redis_pool._close_transport = AsyncMock(wraps=redis_pool._close_transport)
else:
mocker.spy(redis_pool, "grow")
mocker.spy(redis_pool, "_create_transport")
mocker.spy(redis_pool, "_close_transport")

async def _check_in_out():
transport = await redis_pool.checkout()
Expand Down

0 comments on commit 43e84a1

Please sign in to comment.