Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Make ObservableDeferred.observe() always return deferred. #6291

Merged
merged 3 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6291.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change cache descriptors to always return deferreds.
7 changes: 2 additions & 5 deletions synapse/util/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,8 @@ def errback(f):

deferred.addCallbacks(callback, errback)

def observe(self):
def observe(self) -> defer.Deferred:
"""Observe the underlying deferred.

Can return either a deferred if the underlying deferred is still pending
(or has failed), or the actual value. Callers may need to use maybeDeferred.
babolivier marked this conversation as resolved.
Show resolved Hide resolved
"""
if not self._result:
d = defer.Deferred()
Expand All @@ -105,7 +102,7 @@ def remove(r):
return d
else:
success, res = self._result
return res if success else defer.fail(res)
return defer.succeed(res) if success else defer.fail(res)

def observers(self):
return self._observers
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test__base.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def func(self, key):

a.func.prefill(("foo",), ObservableDeferred(d))

self.assertEquals(a.func("foo"), d.result)
self.assertEquals(a.func("foo").result, d.result)
self.assertEquals(callcount[0], 0)

@defer.inlineCallbacks
Expand Down
4 changes: 2 additions & 2 deletions tests/util/caches/test_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,14 @@ def fn(self, arg1, arg2):

obj.mock.return_value = ["spam", "eggs"]
r = obj.fn(1, 2)
self.assertEqual(r, ["spam", "eggs"])
self.assertEqual(r.result, ["spam", "eggs"])
obj.mock.assert_called_once_with(1, 2)
obj.mock.reset_mock()

# a call with different params should call the mock again
obj.mock.return_value = ["chips"]
r = obj.fn(1, 3)
self.assertEqual(r, ["chips"])
self.assertEqual(r.result, ["chips"])
obj.mock.assert_called_once_with(1, 3)
obj.mock.reset_mock()

Expand Down