test_descriptors.py 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. # Copyright 2016 OpenMarket Ltd
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from typing import Set
  17. from unittest import mock
  18. from twisted.internet import defer, reactor
  19. from twisted.internet.defer import CancelledError, Deferred
  20. from synapse.api.errors import SynapseError
  21. from synapse.logging.context import (
  22. SENTINEL_CONTEXT,
  23. LoggingContext,
  24. PreserveLoggingContext,
  25. current_context,
  26. make_deferred_yieldable,
  27. )
  28. from synapse.util.caches import descriptors
  29. from synapse.util.caches.descriptors import cached, cachedList, lru_cache
  30. from tests import unittest
  31. from tests.test_utils import get_awaitable_result
  32. logger = logging.getLogger(__name__)
  33. class LruCacheDecoratorTestCase(unittest.TestCase):
  34. def test_base(self):
  35. class Cls:
  36. def __init__(self):
  37. self.mock = mock.Mock()
  38. @lru_cache()
  39. def fn(self, arg1, arg2):
  40. return self.mock(arg1, arg2)
  41. obj = Cls()
  42. obj.mock.return_value = "fish"
  43. r = obj.fn(1, 2)
  44. self.assertEqual(r, "fish")
  45. obj.mock.assert_called_once_with(1, 2)
  46. obj.mock.reset_mock()
  47. # a call with different params should call the mock again
  48. obj.mock.return_value = "chips"
  49. r = obj.fn(1, 3)
  50. self.assertEqual(r, "chips")
  51. obj.mock.assert_called_once_with(1, 3)
  52. obj.mock.reset_mock()
  53. # the two values should now be cached
  54. r = obj.fn(1, 2)
  55. self.assertEqual(r, "fish")
  56. r = obj.fn(1, 3)
  57. self.assertEqual(r, "chips")
  58. obj.mock.assert_not_called()
  59. def run_on_reactor():
  60. d = defer.Deferred()
  61. reactor.callLater(0, d.callback, 0)
  62. return make_deferred_yieldable(d)
  63. class DescriptorTestCase(unittest.TestCase):
  64. @defer.inlineCallbacks
  65. def test_cache(self):
  66. class Cls:
  67. def __init__(self):
  68. self.mock = mock.Mock()
  69. @descriptors.cached()
  70. def fn(self, arg1, arg2):
  71. return self.mock(arg1, arg2)
  72. obj = Cls()
  73. obj.mock.return_value = "fish"
  74. r = yield obj.fn(1, 2)
  75. self.assertEqual(r, "fish")
  76. obj.mock.assert_called_once_with(1, 2)
  77. obj.mock.reset_mock()
  78. # a call with different params should call the mock again
  79. obj.mock.return_value = "chips"
  80. r = yield obj.fn(1, 3)
  81. self.assertEqual(r, "chips")
  82. obj.mock.assert_called_once_with(1, 3)
  83. obj.mock.reset_mock()
  84. # the two values should now be cached
  85. r = yield obj.fn(1, 2)
  86. self.assertEqual(r, "fish")
  87. r = yield obj.fn(1, 3)
  88. self.assertEqual(r, "chips")
  89. obj.mock.assert_not_called()
  90. @defer.inlineCallbacks
  91. def test_cache_num_args(self):
  92. """Only the first num_args arguments should matter to the cache"""
  93. class Cls:
  94. def __init__(self):
  95. self.mock = mock.Mock()
  96. @descriptors.cached(num_args=1)
  97. def fn(self, arg1, arg2):
  98. return self.mock(arg1, arg2)
  99. obj = Cls()
  100. obj.mock.return_value = "fish"
  101. r = yield obj.fn(1, 2)
  102. self.assertEqual(r, "fish")
  103. obj.mock.assert_called_once_with(1, 2)
  104. obj.mock.reset_mock()
  105. # a call with different params should call the mock again
  106. obj.mock.return_value = "chips"
  107. r = yield obj.fn(2, 3)
  108. self.assertEqual(r, "chips")
  109. obj.mock.assert_called_once_with(2, 3)
  110. obj.mock.reset_mock()
  111. # the two values should now be cached; we should be able to vary
  112. # the second argument and still get the cached result.
  113. r = yield obj.fn(1, 4)
  114. self.assertEqual(r, "fish")
  115. r = yield obj.fn(2, 5)
  116. self.assertEqual(r, "chips")
  117. obj.mock.assert_not_called()
  118. @defer.inlineCallbacks
  119. def test_cache_uncached_args(self):
  120. """
  121. Only the arguments not named in uncached_args should matter to the cache
  122. Note that this is identical to test_cache_num_args, but provides the
  123. arguments differently.
  124. """
  125. class Cls:
  126. # Note that it is important that this is not the last argument to
  127. # test behaviour of skipping arguments properly.
  128. @descriptors.cached(uncached_args=("arg2",))
  129. def fn(self, arg1, arg2, arg3):
  130. return self.mock(arg1, arg2, arg3)
  131. def __init__(self):
  132. self.mock = mock.Mock()
  133. obj = Cls()
  134. obj.mock.return_value = "fish"
  135. r = yield obj.fn(1, 2, 3)
  136. self.assertEqual(r, "fish")
  137. obj.mock.assert_called_once_with(1, 2, 3)
  138. obj.mock.reset_mock()
  139. # a call with different params should call the mock again
  140. obj.mock.return_value = "chips"
  141. r = yield obj.fn(2, 3, 4)
  142. self.assertEqual(r, "chips")
  143. obj.mock.assert_called_once_with(2, 3, 4)
  144. obj.mock.reset_mock()
  145. # the two values should now be cached; we should be able to vary
  146. # the second argument and still get the cached result.
  147. r = yield obj.fn(1, 4, 3)
  148. self.assertEqual(r, "fish")
  149. r = yield obj.fn(2, 5, 4)
  150. self.assertEqual(r, "chips")
  151. obj.mock.assert_not_called()
  152. @defer.inlineCallbacks
  153. def test_cache_kwargs(self):
  154. """Test that keyword arguments are treated properly"""
  155. class Cls:
  156. def __init__(self):
  157. self.mock = mock.Mock()
  158. @descriptors.cached()
  159. def fn(self, arg1, kwarg1=2):
  160. return self.mock(arg1, kwarg1=kwarg1)
  161. obj = Cls()
  162. obj.mock.return_value = "fish"
  163. r = yield obj.fn(1, kwarg1=2)
  164. self.assertEqual(r, "fish")
  165. obj.mock.assert_called_once_with(1, kwarg1=2)
  166. obj.mock.reset_mock()
  167. # a call with different params should call the mock again
  168. obj.mock.return_value = "chips"
  169. r = yield obj.fn(1, kwarg1=3)
  170. self.assertEqual(r, "chips")
  171. obj.mock.assert_called_once_with(1, kwarg1=3)
  172. obj.mock.reset_mock()
  173. # the values should now be cached.
  174. r = yield obj.fn(1, kwarg1=2)
  175. self.assertEqual(r, "fish")
  176. # We should be able to not provide kwarg1 and get the cached value back.
  177. r = yield obj.fn(1)
  178. self.assertEqual(r, "fish")
  179. # Keyword arguments can be in any order.
  180. r = yield obj.fn(kwarg1=2, arg1=1)
  181. self.assertEqual(r, "fish")
  182. obj.mock.assert_not_called()
  183. def test_cache_with_sync_exception(self):
  184. """If the wrapped function throws synchronously, things should continue to work"""
  185. class Cls:
  186. @cached()
  187. def fn(self, arg1):
  188. raise SynapseError(100, "mai spoon iz too big!!1")
  189. obj = Cls()
  190. # this should fail immediately
  191. d = obj.fn(1)
  192. self.failureResultOf(d, SynapseError)
  193. # ... leaving the cache empty
  194. self.assertEqual(len(obj.fn.cache.cache), 0)
  195. # and a second call should result in a second exception
  196. d = obj.fn(1)
  197. self.failureResultOf(d, SynapseError)
  198. def test_cache_with_async_exception(self):
  199. """The wrapped function returns a failure"""
  200. class Cls:
  201. result = None
  202. call_count = 0
  203. @cached()
  204. def fn(self, arg1):
  205. self.call_count += 1
  206. return self.result
  207. obj = Cls()
  208. callbacks: Set[str] = set()
  209. # set off an asynchronous request
  210. obj.result = origin_d = defer.Deferred()
  211. d1 = obj.fn(1, on_invalidate=lambda: callbacks.add("d1"))
  212. self.assertFalse(d1.called)
  213. # a second request should also return a deferred, but should not call the
  214. # function itself.
  215. d2 = obj.fn(1, on_invalidate=lambda: callbacks.add("d2"))
  216. self.assertFalse(d2.called)
  217. self.assertEqual(obj.call_count, 1)
  218. # no callbacks yet
  219. self.assertEqual(callbacks, set())
  220. # the original request fails
  221. e = Exception("bzz")
  222. origin_d.errback(e)
  223. # ... which should cause the lookups to fail similarly
  224. self.assertIs(self.failureResultOf(d1, Exception).value, e)
  225. self.assertIs(self.failureResultOf(d2, Exception).value, e)
  226. # ... and the callbacks to have been, uh, called.
  227. self.assertEqual(callbacks, {"d1", "d2"})
  228. # ... leaving the cache empty
  229. self.assertEqual(len(obj.fn.cache.cache), 0)
  230. # and a second call should work as normal
  231. obj.result = defer.succeed(100)
  232. d3 = obj.fn(1)
  233. self.assertEqual(self.successResultOf(d3), 100)
  234. self.assertEqual(obj.call_count, 2)
  235. def test_cache_logcontexts(self):
  236. """Check that logcontexts are set and restored correctly when
  237. using the cache."""
  238. complete_lookup = defer.Deferred()
  239. class Cls:
  240. @descriptors.cached()
  241. def fn(self, arg1):
  242. @defer.inlineCallbacks
  243. def inner_fn():
  244. with PreserveLoggingContext():
  245. yield complete_lookup
  246. return 1
  247. return inner_fn()
  248. @defer.inlineCallbacks
  249. def do_lookup():
  250. with LoggingContext("c1") as c1:
  251. r = yield obj.fn(1)
  252. self.assertEqual(current_context(), c1)
  253. return r
  254. def check_result(r):
  255. self.assertEqual(r, 1)
  256. obj = Cls()
  257. # set off a deferred which will do a cache lookup
  258. d1 = do_lookup()
  259. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  260. d1.addCallback(check_result)
  261. # and another
  262. d2 = do_lookup()
  263. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  264. d2.addCallback(check_result)
  265. # let the lookup complete
  266. complete_lookup.callback(None)
  267. return defer.gatherResults([d1, d2])
  268. def test_cache_logcontexts_with_exception(self):
  269. """Check that the cache sets and restores logcontexts correctly when
  270. the lookup function throws an exception"""
  271. class Cls:
  272. @descriptors.cached()
  273. def fn(self, arg1):
  274. @defer.inlineCallbacks
  275. def inner_fn():
  276. # we want this to behave like an asynchronous function
  277. yield run_on_reactor()
  278. raise SynapseError(400, "blah")
  279. return inner_fn()
  280. @defer.inlineCallbacks
  281. def do_lookup():
  282. with LoggingContext("c1") as c1:
  283. try:
  284. d = obj.fn(1)
  285. self.assertEqual(
  286. current_context(),
  287. SENTINEL_CONTEXT,
  288. )
  289. yield d
  290. self.fail("No exception thrown")
  291. except SynapseError:
  292. pass
  293. self.assertEqual(current_context(), c1)
  294. # the cache should now be empty
  295. self.assertEqual(len(obj.fn.cache.cache), 0)
  296. obj = Cls()
  297. # set off a deferred which will do a cache lookup
  298. d1 = do_lookup()
  299. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  300. return d1
  301. @defer.inlineCallbacks
  302. def test_cache_default_args(self):
  303. class Cls:
  304. def __init__(self):
  305. self.mock = mock.Mock()
  306. @descriptors.cached()
  307. def fn(self, arg1, arg2=2, arg3=3):
  308. return self.mock(arg1, arg2, arg3)
  309. obj = Cls()
  310. obj.mock.return_value = "fish"
  311. r = yield obj.fn(1, 2, 3)
  312. self.assertEqual(r, "fish")
  313. obj.mock.assert_called_once_with(1, 2, 3)
  314. obj.mock.reset_mock()
  315. # a call with same params shouldn't call the mock again
  316. r = yield obj.fn(1, 2)
  317. self.assertEqual(r, "fish")
  318. obj.mock.assert_not_called()
  319. obj.mock.reset_mock()
  320. # a call with different params should call the mock again
  321. obj.mock.return_value = "chips"
  322. r = yield obj.fn(2, 3)
  323. self.assertEqual(r, "chips")
  324. obj.mock.assert_called_once_with(2, 3, 3)
  325. obj.mock.reset_mock()
  326. # the two values should now be cached
  327. r = yield obj.fn(1, 2)
  328. self.assertEqual(r, "fish")
  329. r = yield obj.fn(2, 3)
  330. self.assertEqual(r, "chips")
  331. obj.mock.assert_not_called()
  332. def test_cache_iterable(self):
  333. class Cls:
  334. def __init__(self):
  335. self.mock = mock.Mock()
  336. @descriptors.cached(iterable=True)
  337. def fn(self, arg1, arg2):
  338. return self.mock(arg1, arg2)
  339. obj = Cls()
  340. obj.mock.return_value = ["spam", "eggs"]
  341. r = obj.fn(1, 2)
  342. self.assertEqual(r.result, ["spam", "eggs"])
  343. obj.mock.assert_called_once_with(1, 2)
  344. obj.mock.reset_mock()
  345. # a call with different params should call the mock again
  346. obj.mock.return_value = ["chips"]
  347. r = obj.fn(1, 3)
  348. self.assertEqual(r.result, ["chips"])
  349. obj.mock.assert_called_once_with(1, 3)
  350. obj.mock.reset_mock()
  351. # the two values should now be cached
  352. self.assertEqual(len(obj.fn.cache.cache), 3)
  353. r = obj.fn(1, 2)
  354. self.assertEqual(r.result, ["spam", "eggs"])
  355. r = obj.fn(1, 3)
  356. self.assertEqual(r.result, ["chips"])
  357. obj.mock.assert_not_called()
  358. def test_cache_iterable_with_sync_exception(self):
  359. """If the wrapped function throws synchronously, things should continue to work"""
  360. class Cls:
  361. @descriptors.cached(iterable=True)
  362. def fn(self, arg1):
  363. raise SynapseError(100, "mai spoon iz too big!!1")
  364. obj = Cls()
  365. # this should fail immediately
  366. d = obj.fn(1)
  367. self.failureResultOf(d, SynapseError)
  368. # ... leaving the cache empty
  369. self.assertEqual(len(obj.fn.cache.cache), 0)
  370. # and a second call should result in a second exception
  371. d = obj.fn(1)
  372. self.failureResultOf(d, SynapseError)
  373. def test_invalidate_cascade(self):
  374. """Invalidations should cascade up through cache contexts"""
  375. class Cls:
  376. @cached(cache_context=True)
  377. async def func1(self, key, cache_context):
  378. return await self.func2(key, on_invalidate=cache_context.invalidate)
  379. @cached(cache_context=True)
  380. async def func2(self, key, cache_context):
  381. return self.func3(key, on_invalidate=cache_context.invalidate)
  382. @lru_cache(cache_context=True)
  383. def func3(self, key, cache_context):
  384. self.invalidate = cache_context.invalidate
  385. return 42
  386. obj = Cls()
  387. top_invalidate = mock.Mock()
  388. r = get_awaitable_result(obj.func1("k1", on_invalidate=top_invalidate))
  389. self.assertEqual(r, 42)
  390. obj.invalidate()
  391. top_invalidate.assert_called_once()
  392. def test_cancel(self):
  393. """Test that cancelling a lookup does not cancel other lookups"""
  394. complete_lookup: "Deferred[None]" = Deferred()
  395. class Cls:
  396. @cached()
  397. async def fn(self, arg1):
  398. await complete_lookup
  399. return str(arg1)
  400. obj = Cls()
  401. d1 = obj.fn(123)
  402. d2 = obj.fn(123)
  403. self.assertFalse(d1.called)
  404. self.assertFalse(d2.called)
  405. # Cancel `d1`, which is the lookup that caused `fn` to run.
  406. d1.cancel()
  407. # `d2` should complete normally.
  408. complete_lookup.callback(None)
  409. self.failureResultOf(d1, CancelledError)
  410. self.assertEqual(d2.result, "123")
  411. def test_cancel_logcontexts(self):
  412. """Test that cancellation does not break logcontexts.
  413. * The `CancelledError` must be raised with the correct logcontext.
  414. * The inner lookup must not resume with a finished logcontext.
  415. * The inner lookup must not restore a finished logcontext when done.
  416. """
  417. complete_lookup: "Deferred[None]" = Deferred()
  418. class Cls:
  419. inner_context_was_finished = False
  420. @cached()
  421. async def fn(self, arg1):
  422. await make_deferred_yieldable(complete_lookup)
  423. self.inner_context_was_finished = current_context().finished
  424. return str(arg1)
  425. obj = Cls()
  426. async def do_lookup():
  427. with LoggingContext("c1") as c1:
  428. try:
  429. await obj.fn(123)
  430. self.fail("No CancelledError thrown")
  431. except CancelledError:
  432. self.assertEqual(
  433. current_context(),
  434. c1,
  435. "CancelledError was not raised with the correct logcontext",
  436. )
  437. # suppress the error and succeed
  438. d = defer.ensureDeferred(do_lookup())
  439. d.cancel()
  440. complete_lookup.callback(None)
  441. self.successResultOf(d)
  442. self.assertFalse(
  443. obj.inner_context_was_finished, "Tried to restart a finished logcontext"
  444. )
  445. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  446. class CacheDecoratorTestCase(unittest.HomeserverTestCase):
  447. """More tests for @cached
  448. The following is a set of tests that got lost in a different file for a while.
  449. There are probably duplicates of the tests in DescriptorTestCase. Ideally the
  450. duplicates would be removed and the two sets of classes combined.
  451. """
  452. @defer.inlineCallbacks
  453. def test_passthrough(self):
  454. class A:
  455. @cached()
  456. def func(self, key):
  457. return key
  458. a = A()
  459. self.assertEqual((yield a.func("foo")), "foo")
  460. self.assertEqual((yield a.func("bar")), "bar")
  461. @defer.inlineCallbacks
  462. def test_hit(self):
  463. callcount = [0]
  464. class A:
  465. @cached()
  466. def func(self, key):
  467. callcount[0] += 1
  468. return key
  469. a = A()
  470. yield a.func("foo")
  471. self.assertEqual(callcount[0], 1)
  472. self.assertEqual((yield a.func("foo")), "foo")
  473. self.assertEqual(callcount[0], 1)
  474. @defer.inlineCallbacks
  475. def test_invalidate(self):
  476. callcount = [0]
  477. class A:
  478. @cached()
  479. def func(self, key):
  480. callcount[0] += 1
  481. return key
  482. a = A()
  483. yield a.func("foo")
  484. self.assertEqual(callcount[0], 1)
  485. a.func.invalidate(("foo",))
  486. yield a.func("foo")
  487. self.assertEqual(callcount[0], 2)
  488. def test_invalidate_missing(self):
  489. class A:
  490. @cached()
  491. def func(self, key):
  492. return key
  493. A().func.invalidate(("what",))
  494. @defer.inlineCallbacks
  495. def test_max_entries(self):
  496. callcount = [0]
  497. class A:
  498. @cached(max_entries=10)
  499. def func(self, key):
  500. callcount[0] += 1
  501. return key
  502. a = A()
  503. for k in range(0, 12):
  504. yield a.func(k)
  505. self.assertEqual(callcount[0], 12)
  506. # There must have been at least 2 evictions, meaning if we calculate
  507. # all 12 values again, we must get called at least 2 more times
  508. for k in range(0, 12):
  509. yield a.func(k)
  510. self.assertTrue(
  511. callcount[0] >= 14, msg="Expected callcount >= 14, got %d" % (callcount[0])
  512. )
  513. def test_prefill(self):
  514. callcount = [0]
  515. d = defer.succeed(123)
  516. class A:
  517. @cached()
  518. def func(self, key):
  519. callcount[0] += 1
  520. return d
  521. a = A()
  522. a.func.prefill(("foo",), 456)
  523. self.assertEqual(a.func("foo").result, 456)
  524. self.assertEqual(callcount[0], 0)
  525. @defer.inlineCallbacks
  526. def test_invalidate_context(self):
  527. callcount = [0]
  528. callcount2 = [0]
  529. class A:
  530. @cached()
  531. def func(self, key):
  532. callcount[0] += 1
  533. return key
  534. @cached(cache_context=True)
  535. def func2(self, key, cache_context):
  536. callcount2[0] += 1
  537. return self.func(key, on_invalidate=cache_context.invalidate)
  538. a = A()
  539. yield a.func2("foo")
  540. self.assertEqual(callcount[0], 1)
  541. self.assertEqual(callcount2[0], 1)
  542. a.func.invalidate(("foo",))
  543. yield a.func("foo")
  544. self.assertEqual(callcount[0], 2)
  545. self.assertEqual(callcount2[0], 1)
  546. yield a.func2("foo")
  547. self.assertEqual(callcount[0], 2)
  548. self.assertEqual(callcount2[0], 2)
  549. @defer.inlineCallbacks
  550. def test_eviction_context(self):
  551. callcount = [0]
  552. callcount2 = [0]
  553. class A:
  554. @cached(max_entries=2)
  555. def func(self, key):
  556. callcount[0] += 1
  557. return key
  558. @cached(cache_context=True)
  559. def func2(self, key, cache_context):
  560. callcount2[0] += 1
  561. return self.func(key, on_invalidate=cache_context.invalidate)
  562. a = A()
  563. yield a.func2("foo")
  564. yield a.func2("foo2")
  565. self.assertEqual(callcount[0], 2)
  566. self.assertEqual(callcount2[0], 2)
  567. yield a.func2("foo")
  568. self.assertEqual(callcount[0], 2)
  569. self.assertEqual(callcount2[0], 2)
  570. yield a.func("foo3")
  571. self.assertEqual(callcount[0], 3)
  572. self.assertEqual(callcount2[0], 2)
  573. yield a.func2("foo")
  574. self.assertEqual(callcount[0], 4)
  575. self.assertEqual(callcount2[0], 3)
  576. @defer.inlineCallbacks
  577. def test_double_get(self):
  578. callcount = [0]
  579. callcount2 = [0]
  580. class A:
  581. @cached()
  582. def func(self, key):
  583. callcount[0] += 1
  584. return key
  585. @cached(cache_context=True)
  586. def func2(self, key, cache_context):
  587. callcount2[0] += 1
  588. return self.func(key, on_invalidate=cache_context.invalidate)
  589. a = A()
  590. a.func2.cache.cache = mock.Mock(wraps=a.func2.cache.cache)
  591. yield a.func2("foo")
  592. self.assertEqual(callcount[0], 1)
  593. self.assertEqual(callcount2[0], 1)
  594. a.func2.invalidate(("foo",))
  595. self.assertEqual(a.func2.cache.cache.del_multi.call_count, 1)
  596. yield a.func2("foo")
  597. a.func2.invalidate(("foo",))
  598. self.assertEqual(a.func2.cache.cache.del_multi.call_count, 2)
  599. self.assertEqual(callcount[0], 1)
  600. self.assertEqual(callcount2[0], 2)
  601. a.func.invalidate(("foo",))
  602. self.assertEqual(a.func2.cache.cache.del_multi.call_count, 3)
  603. yield a.func("foo")
  604. self.assertEqual(callcount[0], 2)
  605. self.assertEqual(callcount2[0], 2)
  606. yield a.func2("foo")
  607. self.assertEqual(callcount[0], 2)
  608. self.assertEqual(callcount2[0], 3)
  609. class CachedListDescriptorTestCase(unittest.TestCase):
  610. @defer.inlineCallbacks
  611. def test_cache(self):
  612. class Cls:
  613. def __init__(self):
  614. self.mock = mock.Mock()
  615. @descriptors.cached()
  616. def fn(self, arg1, arg2):
  617. pass
  618. @descriptors.cachedList(cached_method_name="fn", list_name="args1")
  619. async def list_fn(self, args1, arg2):
  620. assert current_context().name == "c1"
  621. # we want this to behave like an asynchronous function
  622. await run_on_reactor()
  623. assert current_context().name == "c1"
  624. return self.mock(args1, arg2)
  625. with LoggingContext("c1") as c1:
  626. obj = Cls()
  627. obj.mock.return_value = {10: "fish", 20: "chips"}
  628. # start the lookup off
  629. d1 = obj.list_fn([10, 20], 2)
  630. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  631. r = yield d1
  632. self.assertEqual(current_context(), c1)
  633. obj.mock.assert_called_once_with({10, 20}, 2)
  634. self.assertEqual(r, {10: "fish", 20: "chips"})
  635. obj.mock.reset_mock()
  636. # a call with different params should call the mock again
  637. obj.mock.return_value = {30: "peas"}
  638. r = yield obj.list_fn([20, 30], 2)
  639. obj.mock.assert_called_once_with({30}, 2)
  640. self.assertEqual(r, {20: "chips", 30: "peas"})
  641. obj.mock.reset_mock()
  642. # all the values should now be cached
  643. r = yield obj.fn(10, 2)
  644. self.assertEqual(r, "fish")
  645. r = yield obj.fn(20, 2)
  646. self.assertEqual(r, "chips")
  647. r = yield obj.fn(30, 2)
  648. self.assertEqual(r, "peas")
  649. r = yield obj.list_fn([10, 20, 30], 2)
  650. obj.mock.assert_not_called()
  651. self.assertEqual(r, {10: "fish", 20: "chips", 30: "peas"})
  652. # we should also be able to use a (single-use) iterable, and should
  653. # deduplicate the keys
  654. obj.mock.reset_mock()
  655. obj.mock.return_value = {40: "gravy"}
  656. iterable = (x for x in [10, 40, 40])
  657. r = yield obj.list_fn(iterable, 2)
  658. obj.mock.assert_called_once_with({40}, 2)
  659. self.assertEqual(r, {10: "fish", 40: "gravy"})
  660. def test_concurrent_lookups(self):
  661. """All concurrent lookups should get the same result"""
  662. class Cls:
  663. def __init__(self):
  664. self.mock = mock.Mock()
  665. @descriptors.cached()
  666. def fn(self, arg1):
  667. pass
  668. @descriptors.cachedList(cached_method_name="fn", list_name="args1")
  669. def list_fn(self, args1) -> "Deferred[dict]":
  670. return self.mock(args1)
  671. obj = Cls()
  672. deferred_result = Deferred()
  673. obj.mock.return_value = deferred_result
  674. # start off several concurrent lookups of the same key
  675. d1 = obj.list_fn([10])
  676. d2 = obj.list_fn([10])
  677. d3 = obj.list_fn([10])
  678. # the mock should have been called exactly once
  679. obj.mock.assert_called_once_with({10})
  680. obj.mock.reset_mock()
  681. # ... and none of the calls should yet be complete
  682. self.assertFalse(d1.called)
  683. self.assertFalse(d2.called)
  684. self.assertFalse(d3.called)
  685. # complete the lookup. @cachedList functions need to complete with a map
  686. # of input->result
  687. deferred_result.callback({10: "peas"})
  688. # ... which should give the right result to all the callers
  689. self.assertEqual(self.successResultOf(d1), {10: "peas"})
  690. self.assertEqual(self.successResultOf(d2), {10: "peas"})
  691. self.assertEqual(self.successResultOf(d3), {10: "peas"})
  692. @defer.inlineCallbacks
  693. def test_invalidate(self):
  694. """Make sure that invalidation callbacks are called."""
  695. class Cls:
  696. def __init__(self):
  697. self.mock = mock.Mock()
  698. @descriptors.cached()
  699. def fn(self, arg1, arg2):
  700. pass
  701. @descriptors.cachedList(cached_method_name="fn", list_name="args1")
  702. async def list_fn(self, args1, arg2):
  703. # we want this to behave like an asynchronous function
  704. await run_on_reactor()
  705. return self.mock(args1, arg2)
  706. obj = Cls()
  707. invalidate0 = mock.Mock()
  708. invalidate1 = mock.Mock()
  709. # cache miss
  710. obj.mock.return_value = {10: "fish", 20: "chips"}
  711. r1 = yield obj.list_fn([10, 20], 2, on_invalidate=invalidate0)
  712. obj.mock.assert_called_once_with({10, 20}, 2)
  713. self.assertEqual(r1, {10: "fish", 20: "chips"})
  714. obj.mock.reset_mock()
  715. # cache hit
  716. r2 = yield obj.list_fn([10, 20], 2, on_invalidate=invalidate1)
  717. obj.mock.assert_not_called()
  718. self.assertEqual(r2, {10: "fish", 20: "chips"})
  719. invalidate0.assert_not_called()
  720. invalidate1.assert_not_called()
  721. # now if we invalidate the keys, both invalidations should get called
  722. obj.fn.invalidate((10, 2))
  723. invalidate0.assert_called_once()
  724. invalidate1.assert_called_once()
  725. def test_cancel(self):
  726. """Test that cancelling a lookup does not cancel other lookups"""
  727. complete_lookup: "Deferred[None]" = Deferred()
  728. class Cls:
  729. @cached()
  730. def fn(self, arg1):
  731. pass
  732. @cachedList(cached_method_name="fn", list_name="args")
  733. async def list_fn(self, args):
  734. await complete_lookup
  735. return {arg: str(arg) for arg in args}
  736. obj = Cls()
  737. d1 = obj.list_fn([123, 456])
  738. d2 = obj.list_fn([123, 456, 789])
  739. self.assertFalse(d1.called)
  740. self.assertFalse(d2.called)
  741. d1.cancel()
  742. # `d2` should complete normally.
  743. complete_lookup.callback(None)
  744. self.failureResultOf(d1, CancelledError)
  745. self.assertEqual(d2.result, {123: "123", 456: "456", 789: "789"})
  746. def test_cancel_logcontexts(self):
  747. """Test that cancellation does not break logcontexts.
  748. * The `CancelledError` must be raised with the correct logcontext.
  749. * The inner lookup must not resume with a finished logcontext.
  750. * The inner lookup must not restore a finished logcontext when done.
  751. """
  752. complete_lookup: "Deferred[None]" = Deferred()
  753. class Cls:
  754. inner_context_was_finished = False
  755. @cached()
  756. def fn(self, arg1):
  757. pass
  758. @cachedList(cached_method_name="fn", list_name="args")
  759. async def list_fn(self, args):
  760. await make_deferred_yieldable(complete_lookup)
  761. self.inner_context_was_finished = current_context().finished
  762. return {arg: str(arg) for arg in args}
  763. obj = Cls()
  764. async def do_lookup():
  765. with LoggingContext("c1") as c1:
  766. try:
  767. await obj.list_fn([123])
  768. self.fail("No CancelledError thrown")
  769. except CancelledError:
  770. self.assertEqual(
  771. current_context(),
  772. c1,
  773. "CancelledError was not raised with the correct logcontext",
  774. )
  775. # suppress the error and succeed
  776. d = defer.ensureDeferred(do_lookup())
  777. d.cancel()
  778. complete_lookup.callback(None)
  779. self.successResultOf(d)
  780. self.assertFalse(
  781. obj.inner_context_was_finished, "Tried to restart a finished logcontext"
  782. )
  783. self.assertEqual(current_context(), SENTINEL_CONTEXT)