Skip to content

Commit 05fe946

Browse files
committed
Fix unit tests
1 parent ad3ab57 commit 05fe946

File tree

8 files changed

+76
-62
lines changed

8 files changed

+76
-62
lines changed

neo4j/_async_compat/shims/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ async def wait_for(fut, timeout):
9191
# and https://github.com/python/cpython/pull/28149
9292

9393
# We got cancelled, but we are already done. Therefore,
94-
# we defer the cancellation until the task yields to the
95-
# event loop the next time.
94+
# we defer the cancellation until next time the task yields
95+
# to the event loop.
9696
asyncio.current_task().cancel()
9797
# [/PATCH]
9898
return fut.result()

tests/integration/mixed/test_async_cancellation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,21 @@ async def _do_the_read(session_, i=1):
9999
raise
100100

101101

102-
REPEATS = 1000
102+
REPETITIONS = 1000
103103

104104

105105
@mark_async_test
106106
@pytest.mark.parametrize(("i", "read_func", "waits", "cancel_count"), (
107107
(
108-
f"{i + 1:0{len(str(REPEATS))}}/{REPEATS}",
108+
f"{i + 1:0{len(str(REPETITIONS))}}/{REPETITIONS}",
109109
random.choice((
110110
_do_the_read, _do_the_read_tx_context, _do_the_read_explicit_tx,
111111
_do_the_read_tx_func
112112
)),
113113
random.randint(0, 1000),
114114
random.randint(1, 20),
115115
)
116-
for i in range(REPEATS) # repeats
116+
for i in range(REPETITIONS)
117117
))
118118
async def test_async_cancellation(
119119
uri, auth, mocker, read_func, waits, cancel_count, i
@@ -175,7 +175,7 @@ async def test_async_cancellation(
175175
assert bookmarks != new_bookmarks
176176

177177

178-
SESSION_REPEATS = 100
178+
SESSION_REPETITIONS = 100
179179
READS_PER_SESSION = 20
180180

181181

@@ -187,7 +187,7 @@ async def test_async_cancellation_does_not_leak(uri, auth):
187187
# driver needs to cope with a single connection in the pool!
188188
max_connection_pool_size=1,
189189
) as driver:
190-
for session_number in range(SESSION_REPEATS):
190+
for session_number in range(SESSION_REPETITIONS):
191191
async with driver.session() as session:
192192
for read_number in range(READS_PER_SESSION):
193193
read_func = random.choice((

tests/unit/async_/io/test_direct.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class AsyncQuickConnection:
5252
def __init__(self, socket):
5353
self.socket = socket
5454
self.address = socket.getpeername()
55+
self.local_port = self.address[1]
5556

5657
@property
5758
def is_reset(self):

tests/unit/async_/work/test_transaction.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ async def test_transaction_context_when_committing(
4040
):
4141
on_closed = mocker.AsyncMock()
4242
on_error = mocker.AsyncMock()
43-
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error)
43+
on_cancel = mocker.Mock()
44+
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error,
45+
on_cancel)
4446
mock_commit = mocker.patch.object(tx, "_commit", wraps=tx._commit)
4547
mock_rollback = mocker.patch.object(tx, "_rollback", wraps=tx._rollback)
4648
async with tx as tx_:
@@ -70,7 +72,9 @@ async def test_transaction_context_with_explicit_rollback(
7072
):
7173
on_closed = mocker.AsyncMock()
7274
on_error = mocker.AsyncMock()
73-
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error)
75+
on_cancel = mocker.Mock()
76+
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error,
77+
on_cancel)
7478
mock_commit = mocker.patch.object(tx, "_commit", wraps=tx._commit)
7579
mock_rollback = mocker.patch.object(tx, "_rollback", wraps=tx._rollback)
7680
async with tx as tx_:
@@ -99,7 +103,9 @@ class OopsError(RuntimeError):
99103

100104
on_closed = MagicMock()
101105
on_error = MagicMock()
102-
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error)
106+
on_cancel = MagicMock()
107+
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error,
108+
on_cancel)
103109
mock_commit = mocker.patch.object(tx, "_commit", wraps=tx._commit)
104110
mock_rollback = mocker.patch.object(tx, "_rollback", wraps=tx._rollback)
105111
with pytest.raises(OopsError):
@@ -117,7 +123,9 @@ class OopsError(RuntimeError):
117123
async def test_transaction_run_takes_no_query_object(async_fake_connection):
118124
on_closed = MagicMock()
119125
on_error = MagicMock()
120-
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error)
126+
on_cancel = MagicMock()
127+
tx = AsyncTransaction(async_fake_connection, 2, on_closed, on_error,
128+
on_cancel)
121129
with pytest.raises(ValueError):
122130
await tx.run(Query("RETURN 1"))
123131

@@ -128,7 +136,7 @@ async def test_transaction_rollbacks_on_open_connections(
128136
):
129137
tx = AsyncTransaction(
130138
async_fake_connection, 2, lambda *args, **kwargs: None,
131-
lambda *args, **kwargs: None
139+
lambda *args, **kwargs: None, lambda *args, **kwargs: None
132140
)
133141
async with tx as tx_:
134142
async_fake_connection.is_reset_mock.return_value = False
@@ -145,7 +153,7 @@ async def test_transaction_no_rollback_on_reset_connections(
145153
):
146154
tx = AsyncTransaction(
147155
async_fake_connection, 2, lambda *args, **kwargs: None,
148-
lambda *args, **kwargs: None
156+
lambda *args, **kwargs: None, lambda *args, **kwargs: None
149157
)
150158
async with tx as tx_:
151159
async_fake_connection.is_reset_mock.return_value = True
@@ -162,7 +170,7 @@ async def test_transaction_no_rollback_on_closed_connections(
162170
):
163171
tx = AsyncTransaction(
164172
async_fake_connection, 2, lambda *args, **kwargs: None,
165-
lambda *args, **kwargs: None
173+
lambda *args, **kwargs: None, lambda *args, **kwargs: None
166174
)
167175
async with tx as tx_:
168176
async_fake_connection.closed.return_value = True
@@ -181,7 +189,7 @@ async def test_transaction_no_rollback_on_defunct_connections(
181189
):
182190
tx = AsyncTransaction(
183191
async_fake_connection, 2, lambda *args, **kwargs: None,
184-
lambda *args, **kwargs: None
192+
lambda *args, **kwargs: None, lambda *args, **kwargs: None
185193
)
186194
async with tx as tx_:
187195
async_fake_connection.defunct.return_value = True

tests/unit/mixed/async_compat/test_concurrency.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,13 @@ async def test_async_r_lock_acquire_cancellation(waits):
176176
async def acquire_task():
177177
while True:
178178
count = lock._count
179-
cancellation = None
180-
try:
181-
print("try")
182-
await lock.acquire(timeout=0.1)
183-
print("acquired")
184-
except asyncio.CancelledError as exc:
185-
print("cancelled")
186-
cancellation = exc
187-
188-
if cancellation is not None:
189-
assert lock._count == count
190-
raise cancellation
179+
await lock.acquire(timeout=0.1)
191180
assert lock._count == count + 1
181+
try:
182+
await asyncio.sleep(0)
183+
except asyncio.CancelledError:
184+
raise
185+
assert count < 50 # safety guard, we shouldn't ever get there!
192186

193187
fut = asyncio.ensure_future(acquire_task())
194188
for _ in range(waits):

tests/unit/mixed/async_compat/test_shims.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,33 @@
2626
from ...._async_compat import mark_async_test
2727

2828

29+
async def _check_wait_for(wait_for_, should_propagate_cancellation):
30+
inner = asyncio.get_event_loop().create_future()
31+
outer = wait_for_(inner, 0.1)
32+
outer_future = asyncio.ensure_future(outer)
33+
await asyncio.sleep(0)
34+
inner.set_result(None) # inner is done
35+
outer_future.cancel() # AND outer got cancelled
36+
37+
if should_propagate_cancellation:
38+
with pytest.raises(asyncio.CancelledError):
39+
await outer_future
40+
else:
41+
await outer_future
42+
43+
2944
@pytest.mark.skipif(
3045
sys.version_info < (3, 8),
3146
reason="wait_for is only broken in Python 3.8+"
3247
)
3348
@mark_async_test
3449
async def test_wait_for_shim_is_necessary_starting_from_3x8():
3550
# when this tests fails, the shim became superfluous
36-
inner = asyncio.get_event_loop().create_future()
37-
outer = asyncio.wait_for(inner, 0.1)
38-
outer_future = asyncio.ensure_future(outer)
39-
await asyncio.sleep(0)
40-
inner.set_result(None) # inner is done
41-
outer_future.cancel() # AND outer got cancelled
42-
43-
# this should propagate the cancellation, but it's broken :/
44-
await outer_future
51+
await _check_wait_for(
52+
asyncio.wait_for,
53+
# this should propagate the cancellation, but it's broken :/
54+
should_propagate_cancellation=False
55+
)
4556

4657

4758
@pytest.mark.skipif(
@@ -50,25 +61,16 @@ async def test_wait_for_shim_is_necessary_starting_from_3x8():
5061
)
5162
@mark_async_test
5263
async def test_wait_for_shim_is_not_necessary_prior_to_3x8():
53-
inner = asyncio.get_event_loop().create_future()
54-
outer = asyncio.wait_for(inner, 0.1)
55-
outer_future = asyncio.ensure_future(outer)
56-
await asyncio.sleep(0)
57-
inner.set_result(None) # inner is done
58-
outer_future.cancel() # AND outer got cancelled
59-
60-
with pytest.raises(asyncio.CancelledError):
61-
await outer_future
64+
await _check_wait_for(
65+
asyncio.wait_for,
66+
should_propagate_cancellation=True
67+
)
6268

6369

6470
@mark_async_test
6571
async def test_wait_for_shim_propagates_cancellation():
66-
inner = asyncio.get_event_loop().create_future()
67-
outer = shims.wait_for(inner, 0.1)
68-
outer_future = asyncio.ensure_future(outer)
69-
await asyncio.sleep(0)
70-
inner.set_result(None) # inner is done
71-
outer_future.cancel() # AND outer got cancelled
72-
73-
with pytest.raises(asyncio.CancelledError):
74-
await outer_future
72+
# shim should always work regardless of the Python version
73+
await _check_wait_for(
74+
shims.wait_for,
75+
should_propagate_cancellation=True
76+
)

tests/unit/sync/io/test_direct.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class QuickConnection:
5252
def __init__(self, socket):
5353
self.socket = socket
5454
self.address = socket.getpeername()
55+
self.local_port = self.address[1]
5556

5657
@property
5758
def is_reset(self):

tests/unit/sync/work/test_transaction.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def test_transaction_context_when_committing(
4040
):
4141
on_closed = mocker.Mock()
4242
on_error = mocker.Mock()
43-
tx = Transaction(fake_connection, 2, on_closed, on_error)
43+
on_cancel = mocker.Mock()
44+
tx = Transaction(fake_connection, 2, on_closed, on_error,
45+
on_cancel)
4446
mock_commit = mocker.patch.object(tx, "_commit", wraps=tx._commit)
4547
mock_rollback = mocker.patch.object(tx, "_rollback", wraps=tx._rollback)
4648
with tx as tx_:
@@ -70,7 +72,9 @@ def test_transaction_context_with_explicit_rollback(
7072
):
7173
on_closed = mocker.Mock()
7274
on_error = mocker.Mock()
73-
tx = Transaction(fake_connection, 2, on_closed, on_error)
75+
on_cancel = mocker.Mock()
76+
tx = Transaction(fake_connection, 2, on_closed, on_error,
77+
on_cancel)
7478
mock_commit = mocker.patch.object(tx, "_commit", wraps=tx._commit)
7579
mock_rollback = mocker.patch.object(tx, "_rollback", wraps=tx._rollback)
7680
with tx as tx_:
@@ -99,7 +103,9 @@ class OopsError(RuntimeError):
99103

100104
on_closed = MagicMock()
101105
on_error = MagicMock()
102-
tx = Transaction(fake_connection, 2, on_closed, on_error)
106+
on_cancel = MagicMock()
107+
tx = Transaction(fake_connection, 2, on_closed, on_error,
108+
on_cancel)
103109
mock_commit = mocker.patch.object(tx, "_commit", wraps=tx._commit)
104110
mock_rollback = mocker.patch.object(tx, "_rollback", wraps=tx._rollback)
105111
with pytest.raises(OopsError):
@@ -117,7 +123,9 @@ class OopsError(RuntimeError):
117123
def test_transaction_run_takes_no_query_object(fake_connection):
118124
on_closed = MagicMock()
119125
on_error = MagicMock()
120-
tx = Transaction(fake_connection, 2, on_closed, on_error)
126+
on_cancel = MagicMock()
127+
tx = Transaction(fake_connection, 2, on_closed, on_error,
128+
on_cancel)
121129
with pytest.raises(ValueError):
122130
tx.run(Query("RETURN 1"))
123131

@@ -128,7 +136,7 @@ def test_transaction_rollbacks_on_open_connections(
128136
):
129137
tx = Transaction(
130138
fake_connection, 2, lambda *args, **kwargs: None,
131-
lambda *args, **kwargs: None
139+
lambda *args, **kwargs: None, lambda *args, **kwargs: None
132140
)
133141
with tx as tx_:
134142
fake_connection.is_reset_mock.return_value = False
@@ -145,7 +153,7 @@ def test_transaction_no_rollback_on_reset_connections(
145153
):
146154
tx = Transaction(
147155
fake_connection, 2, lambda *args, **kwargs: None,
148-
lambda *args, **kwargs: None
156+
lambda *args, **kwargs: None, lambda *args, **kwargs: None
149157
)
150158
with tx as tx_:
151159
fake_connection.is_reset_mock.return_value = True
@@ -162,7 +170,7 @@ def test_transaction_no_rollback_on_closed_connections(
162170
):
163171
tx = Transaction(
164172
fake_connection, 2, lambda *args, **kwargs: None,
165-
lambda *args, **kwargs: None
173+
lambda *args, **kwargs: None, lambda *args, **kwargs: None
166174
)
167175
with tx as tx_:
168176
fake_connection.closed.return_value = True
@@ -181,7 +189,7 @@ def test_transaction_no_rollback_on_defunct_connections(
181189
):
182190
tx = Transaction(
183191
fake_connection, 2, lambda *args, **kwargs: None,
184-
lambda *args, **kwargs: None
192+
lambda *args, **kwargs: None, lambda *args, **kwargs: None
185193
)
186194
with tx as tx_:
187195
fake_connection.defunct.return_value = True

0 commit comments

Comments
 (0)