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
Additional test for cachedList
#11246
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 an additional test for the `cachedList` method decorator. |
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
from unittest import mock | ||
|
||
from twisted.internet import defer, reactor | ||
from twisted.internet.defer import Deferred | ||
|
||
from synapse.api.errors import SynapseError | ||
from synapse.logging.context import ( | ||
|
@@ -703,6 +704,48 @@ async def list_fn(self, args1, arg2): | |
obj.mock.assert_called_once_with((40,), 2) | ||
self.assertEqual(r, {10: "fish", 40: "gravy"}) | ||
|
||
def test_concurrent_lookups(self): | ||
"""All concurrent lookups should get the same result""" | ||
|
||
class Cls: | ||
def __init__(self): | ||
self.mock = mock.Mock() | ||
|
||
@descriptors.cached() | ||
def fn(self, arg1): | ||
pass | ||
|
||
@descriptors.cachedList("fn", "args1") | ||
def list_fn(self, args1) -> "Deferred[dict]": | ||
return self.mock(args1) | ||
|
||
obj = Cls() | ||
deferred_result = Deferred() | ||
obj.mock.return_value = deferred_result | ||
|
||
# start off several concurrent lookups of the same key | ||
d1 = obj.list_fn([10]) | ||
d2 = obj.list_fn([10]) | ||
d3 = obj.list_fn([10]) | ||
|
||
# the mock should have been called exactly once | ||
obj.mock.assert_called_once_with((10,)) | ||
obj.mock.reset_mock() | ||
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. Is there a reason for the reset? Just tidying up? 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. mostly that. |
||
|
||
# ... and none of the calls should yet be complete | ||
self.assertFalse(d1.called) | ||
self.assertFalse(d2.called) | ||
self.assertFalse(d3.called) | ||
|
||
# complete the lookup. @cachedList functions need to complete with a map | ||
# of input->result | ||
deferred_result.callback({10: "peas"}) | ||
|
||
# ... which should give the right result to all the callers | ||
self.assertEqual(self.successResultOf(d1), {10: "peas"}) | ||
self.assertEqual(self.successResultOf(d2), {10: "peas"}) | ||
self.assertEqual(self.successResultOf(d3), {10: "peas"}) | ||
|
||
@defer.inlineCallbacks | ||
def test_invalidate(self): | ||
"""Make sure that invalidation callbacks are 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.
I don't think this is used---or is the
"fn"
on 718 referring to this somehow?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.
yes, it is. an
@cachedList
function is alwaysan extension of an@cached
function - it doesn't actually have a cache of its own.So if you don't really care about having a non-list version, you still have to have a stub, just to hold the cache.
There are a few instances of this in the codebase, though there they throw
NotImplementedError
.