mirror of
https://github.com/django/django.git
synced 2025-12-15 21:45:20 +00:00
Fixed #33616 -- Allowed registering callbacks that can fail in transaction.on_commit().
Thanks David Wobrock and Mariusz Felisiak for reviews.
This commit is contained in:
parent
be63c78760
commit
4a1150b41d
7 changed files with 140 additions and 15 deletions
|
|
@ -43,6 +43,47 @@ class TestConnectionOnCommit(TransactionTestCase):
|
|||
self.do(1)
|
||||
self.assertDone([1])
|
||||
|
||||
def test_robust_if_no_transaction(self):
|
||||
def robust_callback():
|
||||
raise ForcedError("robust callback")
|
||||
|
||||
with self.assertLogs("django.db.backends.base", "ERROR") as cm:
|
||||
transaction.on_commit(robust_callback, robust=True)
|
||||
self.do(1)
|
||||
|
||||
self.assertDone([1])
|
||||
log_record = cm.records[0]
|
||||
self.assertEqual(
|
||||
log_record.getMessage(),
|
||||
"Error calling TestConnectionOnCommit.test_robust_if_no_transaction."
|
||||
"<locals>.robust_callback in on_commit() (robust callback).",
|
||||
)
|
||||
self.assertIsNotNone(log_record.exc_info)
|
||||
raised_exception = log_record.exc_info[1]
|
||||
self.assertIsInstance(raised_exception, ForcedError)
|
||||
self.assertEqual(str(raised_exception), "robust callback")
|
||||
|
||||
def test_robust_transaction(self):
|
||||
def robust_callback():
|
||||
raise ForcedError("robust callback")
|
||||
|
||||
with self.assertLogs("django.db.backends", "ERROR") as cm:
|
||||
with transaction.atomic():
|
||||
transaction.on_commit(robust_callback, robust=True)
|
||||
self.do(1)
|
||||
|
||||
self.assertDone([1])
|
||||
log_record = cm.records[0]
|
||||
self.assertEqual(
|
||||
log_record.getMessage(),
|
||||
"Error calling TestConnectionOnCommit.test_robust_transaction.<locals>."
|
||||
"robust_callback in on_commit() during transaction (robust callback).",
|
||||
)
|
||||
self.assertIsNotNone(log_record.exc_info)
|
||||
raised_exception = log_record.exc_info[1]
|
||||
self.assertIsInstance(raised_exception, ForcedError)
|
||||
self.assertEqual(str(raised_exception), "robust callback")
|
||||
|
||||
def test_delays_execution_until_after_transaction_commit(self):
|
||||
with transaction.atomic():
|
||||
self.do(1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue