Skip to content

Commit 0dcb09f

Browse files
authored
Merge pull request #2846 from juannyG/master
Address flaky transaction tests
2 parents 4dd8432 + 2bf863e commit 0dcb09f

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

.github/workflows/start_mongo.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ mongodb_dir=$(find ${PWD}/ -type d -name "mongodb-linux-x86_64*")
66

77
mkdir $mongodb_dir/data
88

9-
$mongodb_dir/bin/mongod --dbpath $mongodb_dir/data --logpath $mongodb_dir/mongodb.log --fork --replSet mongoengine
9+
args=(--dbpath $mongodb_dir/data --logpath $mongodb_dir/mongodb.log --fork --replSet mongoengine)
10+
if (( $(echo "$MONGODB > 3.8" | bc -l) )); then
11+
args+=(--setParameter maxTransactionLockRequestTimeoutMillis=1000)
12+
fi
13+
14+
$mongodb_dir/bin/mongod "${args[@]}"
15+
1016
if (( $(echo "$MONGODB < 6.0" | bc -l) )); then
1117
mongo --verbose --eval "rs.initiate()"
1218
mongo --quiet --eval "rs.status().ok"

entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
mongod --replSet mongoengine --fork --logpath=/var/log/mongodb.log
44
mongo db --eval "rs.initiate()"
55
mongod --shutdown
6-
mongod --replSet mongoengine --bind_ip 0.0.0.0
6+
mongod --replSet mongoengine --bind_ip 0.0.0.0 --setParameter maxTransactionLockRequestTimeoutMillis=1000

tests/test_context_managers.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -708,20 +708,30 @@ class B(Document):
708708
B.drop_collection()
709709
b_doc = B.objects.create(name="b")
710710

711-
with pytest.raises(TestRollbackError):
712-
with run_in_transaction():
713-
a_doc.update(name="trx-parent")
714-
try:
715-
with run_in_transaction():
716-
b_doc.update(name="trx-child")
717-
raise TestRollbackError()
718-
except TestRollbackError as exc:
719-
# at this stage, the parent transaction is still there
720-
assert A.objects.get(id=a_doc.id).name == "trx-parent"
721-
raise exc
711+
def run_tx():
712+
try:
713+
with run_in_transaction():
714+
a_doc.update(name="trx-parent")
715+
try:
716+
with run_in_transaction():
717+
b_doc.update(name="trx-child")
718+
raise TestRollbackError()
719+
except TestRollbackError as exc:
720+
# at this stage, the parent transaction is still there
721+
assert A.objects.get(id=a_doc.id).name == "trx-parent"
722+
raise exc
723+
except OperationError as op_failure:
724+
"""
725+
See thread safety test below for more details about TransientTransactionError handling
726+
"""
727+
if "TransientTransactionError" in str(op_failure):
728+
logging.warning("TransientTransactionError - retrying...")
729+
run_tx()
722730
else:
723-
# makes sure it enters the except above
724-
assert False
731+
raise op_failure
732+
733+
with pytest.raises(TestRollbackError):
734+
run_tx()
725735

726736
assert A.objects.get(id=a_doc.id).name == "a"
727737
assert B.objects.get(id=b_doc.id).name == "b"

0 commit comments

Comments
 (0)