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

Commit 9cbe401

Browse files
committed
more tests
1 parent 3e025c9 commit 9cbe401

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

tests/util/caches/test_deferred_cache.py

+93
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,99 @@ 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+
get_d.addCallback(check1)
57+
58+
# now fire off all the deferreds
59+
origin_d.callback(99)
60+
self.assertEqual(self.successResultOf(origin_d), 99)
61+
self.assertEqual(self.successResultOf(set_d), 99)
62+
self.assertEqual(self.successResultOf(get_d), 99)
63+
64+
def test_callbacks(self):
65+
"""Invalidation callbacks are called at the right time"""
66+
cache = DeferredCache("test")
67+
callbacks = set()
68+
69+
# start with an entry, with a callback
70+
cache.prefill("k1", 10, callback=lambda: callbacks.add("prefill"))
71+
72+
# now replace that entry with a pending result
73+
origin_d = defer.Deferred()
74+
set_d = cache.set("k1", origin_d, callback=lambda: callbacks.add("set"))
75+
76+
# ... and also make a get request
77+
get_d = cache.get("k1", callback=lambda: callbacks.add("get"))
78+
79+
# we don't expect the invalidation callback for the original value to have
80+
# been called yet, even though get() will now return a different result.
81+
# I'm not sure if that is by design or not.
82+
self.assertSetEqual(callbacks, set())
83+
84+
# now fire off all the deferreds
85+
origin_d.callback(20)
86+
self.assertEqual(self.successResultOf(set_d), 20)
87+
self.assertEqual(self.successResultOf(get_d), 20)
88+
89+
# now the original invalidation callback should have been called, but none of
90+
# the others
91+
self.assertSetEqual(callbacks, {"prefill"})
92+
callbacks.clear()
93+
94+
# another update should invalidate both the previous results
95+
cache.prefill("k1", 30)
96+
self.assertSetEqual(callbacks, {"set", "get"})
97+
98+
def test_set_fail(self):
99+
cache = DeferredCache("test")
100+
callbacks = set()
101+
102+
# start with an entry, with a callback
103+
cache.prefill("k1", 10, callback=lambda: callbacks.add("prefill"))
104+
105+
# now replace that entry with a pending result
106+
origin_d = defer.Deferred()
107+
set_d = cache.set("k1", origin_d, callback=lambda: callbacks.add("set"))
108+
109+
# ... and also make a get request
110+
get_d = cache.get("k1", callback=lambda: callbacks.add("get"))
111+
112+
# none of the callbacks should have been called yet
113+
self.assertSetEqual(callbacks, set())
114+
115+
# oh noes! fails!
116+
e = Exception("oops")
117+
origin_d.errback(e)
118+
self.assertIs(self.failureResultOf(set_d, Exception).value, e)
119+
self.assertIs(self.failureResultOf(get_d, Exception).value, e)
120+
121+
# the callbacks for the failed requests should have been called.
122+
# I'm not sure if this is deliberate or not.
123+
self.assertSetEqual(callbacks, {"get", "set"})
124+
callbacks.clear()
125+
126+
# the old value should still be returned now?
127+
get_d2 = cache.get("k1", callback=lambda: callbacks.add("get2"))
128+
self.assertEqual(self.successResultOf(get_d2), 10)
129+
130+
# replacing the value now should run the callbacks for those requests
131+
# which got the original result
132+
cache.prefill("k1", 30)
133+
self.assertSetEqual(callbacks, {"prefill", "get2"})
134+
42135
def test_get_immediate(self):
43136
cache = DeferredCache("test")
44137
d1 = defer.Deferred()

tests/util/caches/test_descriptors.py

+51
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,56 @@ 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.assertSetEqual(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.assertSetEqual(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+
133184
def test_cache_logcontexts(self):
134185
"""Check that logcontexts are set and restored correctly when
135186
using the cache."""

0 commit comments

Comments
 (0)