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

Commit 6d3905c

Browse files
committed
Add some more tests
1 parent 1f42697 commit 6d3905c

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

tests/util/caches/test_deferred_cache.py

+95
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,101 @@ def test_hit(self):
3939

4040
self.assertEquals(self.successResultOf(cache.get("foo")), 123)
4141

42+
def test_hit_deferred(self):
43+
cache = DeferredCache("test")
44+
origin_d = defer.Deferred()
45+
set_d = cache.set("k1", origin_d)
46+
47+
# get should return an incomplete deferred
48+
get_d = cache.get("k1")
49+
self.assertFalse(get_d.called)
50+
51+
# add a callback that will make sure that the set_d gets called before the get_d
52+
def check1(r):
53+
self.assertTrue(set_d.called)
54+
return r
55+
56+
# TODO: Actually ObservableDeferred *doesn't* run its tests in order on py3.8.
57+
# maybe we should fix that?
58+
# get_d.addCallback(check1)
59+
60+
# now fire off all the deferreds
61+
origin_d.callback(99)
62+
self.assertEqual(self.successResultOf(origin_d), 99)
63+
self.assertEqual(self.successResultOf(set_d), 99)
64+
self.assertEqual(self.successResultOf(get_d), 99)
65+
66+
def test_callbacks(self):
67+
"""Invalidation callbacks are called at the right time"""
68+
cache = DeferredCache("test")
69+
callbacks = set()
70+
71+
# start with an entry, with a callback
72+
cache.prefill("k1", 10, callback=lambda: callbacks.add("prefill"))
73+
74+
# now replace that entry with a pending result
75+
origin_d = defer.Deferred()
76+
set_d = cache.set("k1", origin_d, callback=lambda: callbacks.add("set"))
77+
78+
# ... and also make a get request
79+
get_d = cache.get("k1", callback=lambda: callbacks.add("get"))
80+
81+
# we don't expect the invalidation callback for the original value to have
82+
# been called yet, even though get() will now return a different result.
83+
# I'm not sure if that is by design or not.
84+
self.assertEqual(callbacks, set())
85+
86+
# now fire off all the deferreds
87+
origin_d.callback(20)
88+
self.assertEqual(self.successResultOf(set_d), 20)
89+
self.assertEqual(self.successResultOf(get_d), 20)
90+
91+
# now the original invalidation callback should have been called, but none of
92+
# the others
93+
self.assertEqual(callbacks, {"prefill"})
94+
callbacks.clear()
95+
96+
# another update should invalidate both the previous results
97+
cache.prefill("k1", 30)
98+
self.assertEqual(callbacks, {"set", "get"})
99+
100+
def test_set_fail(self):
101+
cache = DeferredCache("test")
102+
callbacks = set()
103+
104+
# start with an entry, with a callback
105+
cache.prefill("k1", 10, callback=lambda: callbacks.add("prefill"))
106+
107+
# now replace that entry with a pending result
108+
origin_d = defer.Deferred()
109+
set_d = cache.set("k1", origin_d, callback=lambda: callbacks.add("set"))
110+
111+
# ... and also make a get request
112+
get_d = cache.get("k1", callback=lambda: callbacks.add("get"))
113+
114+
# none of the callbacks should have been called yet
115+
self.assertEqual(callbacks, set())
116+
117+
# oh noes! fails!
118+
e = Exception("oops")
119+
origin_d.errback(e)
120+
self.assertIs(self.failureResultOf(set_d, Exception).value, e)
121+
self.assertIs(self.failureResultOf(get_d, Exception).value, e)
122+
123+
# the callbacks for the failed requests should have been called.
124+
# I'm not sure if this is deliberate or not.
125+
self.assertEqual(callbacks, {"get", "set"})
126+
callbacks.clear()
127+
128+
# the old value should still be returned now?
129+
get_d2 = cache.get("k1", callback=lambda: callbacks.add("get2"))
130+
self.assertEqual(self.successResultOf(get_d2), 10)
131+
132+
# replacing the value now should run the callbacks for those requests
133+
# which got the original result
134+
cache.prefill("k1", 30)
135+
self.assertEqual(callbacks, {"prefill", "get2"})
136+
42137
def test_get_immediate(self):
43138
cache = DeferredCache("test")
44139
d1 = defer.Deferred()

tests/util/caches/test_descriptors.py

+52
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
import logging
17+
from typing import Set
1718

1819
import mock
1920

@@ -130,6 +131,57 @@ def fn(self, arg1):
130131
d = obj.fn(1)
131132
self.failureResultOf(d, SynapseError)
132133

134+
def test_cache_with_async_exception(self):
135+
"""The wrapped function returns a failure
136+
"""
137+
138+
class Cls:
139+
result = None
140+
call_count = 0
141+
142+
@cached()
143+
def fn(self, arg1):
144+
self.call_count += 1
145+
return self.result
146+
147+
obj = Cls()
148+
callbacks = set() # type: Set[str]
149+
150+
# set off an asynchronous request
151+
obj.result = origin_d = defer.Deferred()
152+
153+
d1 = obj.fn(1, on_invalidate=lambda: callbacks.add("d1"))
154+
self.assertFalse(d1.called)
155+
156+
# a second request should also return a deferred, but should not call the
157+
# function itself.
158+
d2 = obj.fn(1, on_invalidate=lambda: callbacks.add("d2"))
159+
self.assertFalse(d2.called)
160+
self.assertEqual(obj.call_count, 1)
161+
162+
# no callbacks yet
163+
self.assertEqual(callbacks, set())
164+
165+
# the original request fails
166+
e = Exception("bzz")
167+
origin_d.errback(e)
168+
169+
# ... which should cause the lookups to fail similarly
170+
self.assertIs(self.failureResultOf(d1, Exception).value, e)
171+
self.assertIs(self.failureResultOf(d2, Exception).value, e)
172+
173+
# ... and the callbacks to have been, uh, called.
174+
self.assertEqual(callbacks, {"d1", "d2"})
175+
176+
# ... leaving the cache empty
177+
self.assertEqual(len(obj.fn.cache.cache), 0)
178+
179+
# and a second call should work as normal
180+
obj.result = defer.succeed(100)
181+
d3 = obj.fn(1)
182+
self.assertEqual(self.successResultOf(d3), 100)
183+
self.assertEqual(obj.call_count, 2)
184+
133185
def test_cache_logcontexts(self):
134186
"""Check that logcontexts are set and restored correctly when
135187
using the cache."""

0 commit comments

Comments
 (0)