Skip to content

Commit cbc816c

Browse files
authored
Fix flaky tx timeout IT (#669)
Test would fail on leader switches and similar because of incorrect error handling.
1 parent a18640a commit cbc816c

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

tests/neo4j/test_tx_func_run.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,33 +203,49 @@ def run(tx):
203203
self.assertEqual(res, list(map(types.CypherInt, range(1, 5))))
204204

205205
def test_tx_timeout(self):
206+
class WrappedError(Exception):
207+
def __init__(self, inner):
208+
super().__init__()
209+
self.inner = inner
210+
206211
# TODO: remove this block once all languages work
207212
if get_driver_name() in ["javascript", "java"]:
208213
self.skipTest("Query update2 does not time out.")
209214
if get_driver_name() in ["dotnet"]:
210215
self.skipTest("Backend crashes.")
211216

217+
lock_error_code = "Neo.ClientError.Transaction.LockClientStopped"
218+
212219
def create(tx):
213220
summary = tx.run("MERGE (:Node)").consume()
214221
return summary.database
215222

216223
def update1(tx):
217224
tx.run("MATCH (a:Node) SET a.property = 1").consume()
218225

219-
with self.assertRaises(types.FrontendError):
226+
with self.assertRaises(Exception) as e:
220227
self._session2.execute_write(update2, timeout=250)
228+
inner = e.exception
229+
if isinstance(inner, WrappedError):
230+
inner = inner.inner
231+
if (
232+
not isinstance(inner, types.DriverError)
233+
or inner.code != lock_error_code
234+
):
235+
# This is not the error we are looking for. Maybe there was a
236+
# leader election or so. Give the driver the chance to retry.
237+
raise inner
221238

222-
def update2(tx):
223239
nonlocal exc
224-
with self.assertRaises(types.DriverError) as e:
240+
exc = inner
241+
242+
def update2(tx):
243+
inner = None
244+
try:
225245
tx.run("MATCH (a:Node) SET a.property = 2").consume()
226-
exc = e.exception
227-
if exc.code == "Neo.ClientError.Transaction.LockClientStopped":
228-
# This is the error we are looking for. Maybe there was a
229-
# leader election or so. Give the driver the chance to retry.
230-
raise ApplicationCodeError("Stop, hammer time!")
231-
else:
232-
raise exc
246+
except types.DriverError as e:
247+
inner = e
248+
raise WrappedError(inner)
233249

234250
exc = None
235251

@@ -240,9 +256,7 @@ def update2(tx):
240256
)
241257
self._session1.execute_write(update1)
242258
self.assertIsInstance(exc, types.DriverError)
243-
244-
self.assertEqual(exc.code,
245-
"Neo.ClientError.Transaction.LockClientStopped")
259+
self.assertEqual(exc.code, lock_error_code)
246260
if get_driver_name() in ["python"]:
247-
self.assertEqual(exc.errorType,
248-
"<class 'neo4j.exceptions.ClientError'>")
261+
error_type = "<class 'neo4j.exceptions.ClientError'>"
262+
self.assertEqual(exc.errorType, error_type)

0 commit comments

Comments
 (0)