Skip to content

Commit 9c63038

Browse files
authored
Merge pull request #4 from strollby/feat/async-fix
Reducing the flakiness of transaction
2 parents 2d0acbf + eb31d14 commit 9c63038

3 files changed

Lines changed: 8 additions & 20 deletions

File tree

mongoengine/signals.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def _fail(self, *args, **kwargs):
3838
)
3939

4040
send = lambda *a, **kw: None # noqa
41+
42+
async def send_async(self, *a, **kw):
43+
"""Fake send_async as no-op fallback when blinker is unavailable."""
44+
4145
connect = disconnect = has_receivers_for = receivers_for = (
4246
temporarily_connected_to
4347
) = _fail

tests/asynchronous/test_context_managers.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from pymongo.errors import OperationFailure, InvalidOperation
7+
from pymongo.read_concern import ReadConcern
78

89
from mongoengine import *
910
from mongoengine.asynchronous import async_register_connection, async_get_db, async_connect
@@ -367,8 +368,6 @@ async def test_updating_a_document_within_a_transaction_that_fails(self):
367368
class A(Document):
368369
name = StringField()
369370

370-
await A.adrop_collection()
371-
372371
a_doc = await A.aobjects.create(name="a")
373372

374373
with pytest.raises(TestRollbackError):
@@ -385,8 +384,6 @@ async def test_creating_a_document_within_a_transaction(self):
385384
class A(Document):
386385
name = StringField()
387386

388-
await A.adrop_collection()
389-
390387
# ensure the collection is created (needed for transaction with MongoDB <= 4.2)
391388
await A.aobjects.create(name="test")
392389
await A.aobjects.delete()
@@ -407,7 +404,6 @@ async def test_creating_a_document_within_a_transaction_that_fails(self):
407404
class A(Document):
408405
name = StringField()
409406

410-
await A.adrop_collection()
411407
# ensure a collection is created (needed for transaction with MongoDB <= 4.2)
412408
await A.aobjects.create(name="test")
413409
await A.aobjects.delete()
@@ -455,19 +451,15 @@ async def test_collection_creation_via_upserts_across_databases_in_transaction(s
455451
class A(Document):
456452
name = StringField()
457453

458-
await A.adrop_collection()
459-
460454
a_doc = await A.aobjects.create(name="a")
461455

462456
class B(Document):
463457
meta = {"db_alias": "test2"}
464458
name = StringField()
465459

466-
await B.adrop_collection()
467-
468460
b_doc = await B.aobjects.create(name="b")
469461

470-
async with run_in_transaction():
462+
async with run_in_transaction(transaction_kwargs={"read_concern": ReadConcern("local")}):
471463
await a_doc.aupdate(name="a3")
472464
with switch_db(A, "test2"):
473465
await a_doc.aupdate(name="a4", upsert=True)

tests/synchronous/test_context_managers.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88
from pymongo.errors import OperationFailure
9+
from pymongo.read_concern import ReadConcern
910

1011
from mongoengine import *
1112
from mongoengine.session import _get_session
@@ -396,8 +397,6 @@ def test_updating_a_document_within_a_transaction_that_fails(self):
396397
class A(Document):
397398
name = StringField()
398399

399-
A.drop_collection()
400-
401400
a_doc = A.objects.create(name="a")
402401

403402
with pytest.raises(TestRollbackError):
@@ -414,8 +413,6 @@ def test_creating_a_document_within_a_transaction(self):
414413
class A(Document):
415414
name = StringField()
416415

417-
A.drop_collection()
418-
419416
# ensure collection is created (needed for transaction with MongoDB <= 4.2)
420417
A.objects.create(name="test")
421418
A.objects.delete()
@@ -436,7 +433,6 @@ def test_creating_a_document_within_a_transaction_that_fails(self):
436433
class A(Document):
437434
name = StringField()
438435

439-
A.drop_collection()
440436
# ensure a collection is created (needed for transaction with MongoDB <= 4.2)
441437
A.objects.create(name="test")
442438
A.objects.delete()
@@ -484,19 +480,15 @@ def test_collection_creation_via_upserts_across_databases_in_transaction(self):
484480
class A(Document):
485481
name = StringField()
486482

487-
A.drop_collection()
488-
489483
a_doc = A.objects.create(name="a")
490484

491485
class B(Document):
492486
meta = {"db_alias": "test2"}
493487
name = StringField()
494488

495-
B.drop_collection()
496-
497489
b_doc = B.objects.create(name="b")
498490

499-
with run_in_transaction():
491+
with run_in_transaction(transaction_kwargs={"read_concern": ReadConcern("local")}):
500492
a_doc.update(name="a3")
501493
with switch_db(A, "test2"):
502494
a_doc.update(name="a4", upsert=True)

0 commit comments

Comments
 (0)