Merge pull request #6291 from matrix-org/erikj/fix_cache_descriptor

Make ObservableDeferred.observe() always return deferred.
This commit is contained in:
Erik Johnston 2019-10-30 14:06:34 +01:00 committed by GitHub
commit e5c3a99091
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 7 deletions

1
changelog.d/6291.misc Normal file
View File

@ -0,0 +1 @@
Change cache descriptors to always return deferreds.

View File

@ -86,11 +86,12 @@ class ObservableDeferred(object):
deferred.addCallbacks(callback, errback) deferred.addCallbacks(callback, errback)
def observe(self): def observe(self) -> defer.Deferred:
"""Observe the underlying deferred. """Observe the underlying deferred.
Can return either a deferred if the underlying deferred is still pending This returns a brand new deferred that is resolved when the underlying
(or has failed), or the actual value. Callers may need to use maybeDeferred. deferred is resolved. Interacting with the returned deferred does not
effect the underdlying deferred.
""" """
if not self._result: if not self._result:
d = defer.Deferred() d = defer.Deferred()
@ -105,7 +106,7 @@ class ObservableDeferred(object):
return d return d
else: else:
success, res = self._result 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): def observers(self):
return self._observers return self._observers

View File

@ -197,7 +197,7 @@ class CacheDecoratorTestCase(unittest.TestCase):
a.func.prefill(("foo",), ObservableDeferred(d)) 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) self.assertEquals(callcount[0], 0)
@defer.inlineCallbacks @defer.inlineCallbacks

View File

@ -310,14 +310,14 @@ class DescriptorTestCase(unittest.TestCase):
obj.mock.return_value = ["spam", "eggs"] obj.mock.return_value = ["spam", "eggs"]
r = obj.fn(1, 2) 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.assert_called_once_with(1, 2)
obj.mock.reset_mock() obj.mock.reset_mock()
# a call with different params should call the mock again # a call with different params should call the mock again
obj.mock.return_value = ["chips"] obj.mock.return_value = ["chips"]
r = obj.fn(1, 3) r = obj.fn(1, 3)
self.assertEqual(r, ["chips"]) self.assertEqual(r.result, ["chips"])
obj.mock.assert_called_once_with(1, 3) obj.mock.assert_called_once_with(1, 3)
obj.mock.reset_mock() obj.mock.reset_mock()