Skip to content

Commit ee26243

Browse files
committed
Address flaky transaction tests
Resolves #2841 * Fix test that was source of most failures * Update max transaction lock time for local container entrypoint * Update max transaction lock time for github action --- The majority of failures appeared to be attributed with the `test_exception_in_child_of_a_nested_transaction_rolls_parent_back` unit test. To resolve this test, I borrowed the approach taken by the test just below it: `test_exception_in_parent_of_nested_transaction_after_child_completed_only_rolls_parent_back` which was already creating a function for the work under test and simply trapping the TransientTractionError. After resolving this, I found that the max transaction lock request timeout parameter needed to be modified. After doing so and letting the test suite run indefinitely unless error, test failures appeared to have ceased.
1 parent 75e7779 commit ee26243

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

.github/workflows/start_mongo.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ 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+
$mongodb_dir/bin/mongod \
10+
--dbpath $mongodb_dir/data \
11+
--logpath $mongodb_dir/mongodb.log \
12+
--fork \
13+
--replSet \
14+
--setParameter maxTransactionLockRequestTimeoutMillis=1000 \
15+
mongoengine
16+
1017
if (( $(echo "$MONGODB < 6.0" | bc -l) )); then
1118
mongo --verbose --eval "rs.initiate()"
1219
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)