This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add tests for database transaction callbacks #12198
Merged
squahtx
merged 4 commits into
develop
from
squah/add_database_transaction_callback_tests
Mar 15, 2022
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add tests for database transaction callbacks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,18 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from synapse.storage.database import make_tuple_comparison_clause | ||
from typing import Callable, NoReturn, Tuple | ||
from unittest.mock import Mock, call | ||
|
||
from twisted.test.proto_helpers import MemoryReactor | ||
|
||
from synapse.server import HomeServer | ||
from synapse.storage.database import ( | ||
DatabasePool, | ||
LoggingTransaction, | ||
make_tuple_comparison_clause, | ||
) | ||
from synapse.util import Clock | ||
|
||
from tests import unittest | ||
|
||
|
@@ -22,3 +33,103 @@ def test_native_tuple_comparison(self): | |
clause, args = make_tuple_comparison_clause([("a", 1), ("b", 2)]) | ||
self.assertEqual(clause, "(a,b) > (?,?)") | ||
self.assertEqual(args, [1, 2]) | ||
|
||
|
||
class CallbacksTestCase(unittest.HomeserverTestCase): | ||
"""Tests for transaction callbacks.""" | ||
|
||
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None: | ||
self.store = hs.get_datastores().main | ||
self.db_pool: DatabasePool = self.store.db_pool | ||
|
||
def _run_interaction( | ||
self, func: Callable[[LoggingTransaction], object] | ||
) -> Tuple[Mock, Mock]: | ||
"""Run the given function in a database transaction, with callbacks registered. | ||
|
||
Args: | ||
func: The function to be run in a transaction. The transaction will be | ||
retried if `func` raises an `OperationalError`. | ||
|
||
Returns: | ||
Two mocks, which were registered as an `after_callback` and an | ||
`exception_callback` respectively, on every transaction attempt. | ||
""" | ||
after_callback = Mock() | ||
exception_callback = Mock() | ||
|
||
def _test_txn(txn: LoggingTransaction) -> None: | ||
txn.call_after(after_callback, 123, 456, extra=789) | ||
txn.call_on_exception(exception_callback, 987, 654, extra=321) | ||
func(txn) | ||
|
||
try: | ||
self.get_success_or_raise( | ||
self.db_pool.runInteraction("test_transaction", _test_txn) | ||
) | ||
except Exception: | ||
pass | ||
|
||
return after_callback, exception_callback | ||
|
||
def test_after_callback(self) -> None: | ||
"""Test that the after callback is called when a transaction succeeds.""" | ||
after_callback, exception_callback = self._run_interaction(lambda txn: None) | ||
|
||
after_callback.assert_called_once_with(123, 456, extra=789) | ||
exception_callback.assert_not_called() | ||
|
||
def test_exception_callback(self) -> None: | ||
"""Test that the exception callback is called when a transaction fails.""" | ||
after_callback, exception_callback = self._run_interaction(lambda txn: 1 / 0) | ||
|
||
after_callback.assert_not_called() | ||
exception_callback.assert_called_once_with(987, 654, extra=321) | ||
|
||
def test_failed_retry(self) -> None: | ||
"""Test that the exception callback is called for every failed attempt.""" | ||
|
||
def _test_txn(txn: LoggingTransaction) -> NoReturn: | ||
"""Simulate a retryable failure on every attempt.""" | ||
raise self.db_pool.engine.module.OperationalError() | ||
|
||
after_callback, exception_callback = self._run_interaction(_test_txn) | ||
|
||
after_callback.assert_not_called() | ||
exception_callback.assert_has_calls( | ||
[ | ||
call(987, 654, extra=321), | ||
call(987, 654, extra=321), | ||
call(987, 654, extra=321), | ||
call(987, 654, extra=321), | ||
call(987, 654, extra=321), | ||
call(987, 654, extra=321), | ||
] | ||
) | ||
self.assertEqual(exception_callback.call_count, 6) # no additional calls | ||
|
||
def test_successful_retry(self) -> None: | ||
"""Test callbacks for a failed transaction followed by a successful attempt.""" | ||
first_attempt = True | ||
|
||
def _test_txn(txn: LoggingTransaction) -> None: | ||
"""Simulate a retryable failure on the first attempt only.""" | ||
nonlocal first_attempt | ||
if first_attempt: | ||
first_attempt = False | ||
raise self.db_pool.engine.module.OperationalError() | ||
else: | ||
return None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could probably be simplified with Similarly for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know that was possible. That's a lot nicer, thank you! |
||
|
||
after_callback, exception_callback = self._run_interaction(_test_txn) | ||
|
||
# Calling both `after_callback`s when the first attempt failed is rather | ||
# dubious (#12184). But let's document the behaviour in a test. | ||
after_callback.assert_has_calls( | ||
[ | ||
call(123, 456, extra=789), | ||
call(123, 456, extra=789), | ||
] | ||
) | ||
self.assertEqual(after_callback.call_count, 2) # no additional calls | ||
exception_callback.assert_not_called() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL that there can be additional calls beyond what you give...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this surprising too. I was hoping there would be an assert-these-calls-and-only-these-calls method.