1
0

test_descriptors.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 synapse.api.errors import SynapseError
  20. from synapse.logging.context import (
  21. SENTINEL_CONTEXT,
  22. LoggingContext,
  23. PreserveLoggingContext,
  24. current_context,
  25. make_deferred_yieldable,
  26. )
  27. from synapse.util.caches import descriptors
  28. from synapse.util.caches.descriptors import cached, lru_cache
  29. from tests import unittest
  30. from tests.test_utils import get_awaitable_result
  31. logger = logging.getLogger(__name__)
  32. class LruCacheDecoratorTestCase(unittest.TestCase):
  33. def test_base(self):
  34. class Cls:
  35. def __init__(self):
  36. self.mock = mock.Mock()
  37. @lru_cache()
  38. def fn(self, arg1, arg2):
  39. return self.mock(arg1, arg2)
  40. obj = Cls()
  41. obj.mock.return_value = "fish"
  42. r = obj.fn(1, 2)
  43. self.assertEqual(r, "fish")
  44. obj.mock.assert_called_once_with(1, 2)
  45. obj.mock.reset_mock()
  46. # a call with different params should call the mock again
  47. obj.mock.return_value = "chips"
  48. r = obj.fn(1, 3)
  49. self.assertEqual(r, "chips")
  50. obj.mock.assert_called_once_with(1, 3)
  51. obj.mock.reset_mock()
  52. # the two values should now be cached
  53. r = obj.fn(1, 2)
  54. self.assertEqual(r, "fish")
  55. r = obj.fn(1, 3)
  56. self.assertEqual(r, "chips")
  57. obj.mock.assert_not_called()
  58. def run_on_reactor():
  59. d = defer.Deferred()
  60. reactor.callLater(0, d.callback, 0)
  61. return make_deferred_yieldable(d)
  62. class DescriptorTestCase(unittest.TestCase):
  63. @defer.inlineCallbacks
  64. def test_cache(self):
  65. class Cls:
  66. def __init__(self):
  67. self.mock = mock.Mock()
  68. @descriptors.cached()
  69. def fn(self, arg1, arg2):
  70. return self.mock(arg1, arg2)
  71. obj = Cls()
  72. obj.mock.return_value = "fish"
  73. r = yield obj.fn(1, 2)
  74. self.assertEqual(r, "fish")
  75. obj.mock.assert_called_once_with(1, 2)
  76. obj.mock.reset_mock()
  77. # a call with different params should call the mock again
  78. obj.mock.return_value = "chips"
  79. r = yield obj.fn(1, 3)
  80. self.assertEqual(r, "chips")
  81. obj.mock.assert_called_once_with(1, 3)
  82. obj.mock.reset_mock()
  83. # the two values should now be cached
  84. r = yield obj.fn(1, 2)
  85. self.assertEqual(r, "fish")
  86. r = yield obj.fn(1, 3)
  87. self.assertEqual(r, "chips")
  88. obj.mock.assert_not_called()
  89. @defer.inlineCallbacks
  90. def test_cache_num_args(self):
  91. """Only the first num_args arguments should matter to the cache"""
  92. class Cls:
  93. def __init__(self):
  94. self.mock = mock.Mock()
  95. @descriptors.cached(num_args=1)
  96. def fn(self, arg1, arg2):
  97. return self.mock(arg1, arg2)
  98. obj = Cls()
  99. obj.mock.return_value = "fish"
  100. r = yield obj.fn(1, 2)
  101. self.assertEqual(r, "fish")
  102. obj.mock.assert_called_once_with(1, 2)
  103. obj.mock.reset_mock()
  104. # a call with different params should call the mock again
  105. obj.mock.return_value = "chips"
  106. r = yield obj.fn(2, 3)
  107. self.assertEqual(r, "chips")
  108. obj.mock.assert_called_once_with(2, 3)
  109. obj.mock.reset_mock()
  110. # the two values should now be cached; we should be able to vary
  111. # the second argument and still get the cached result.
  112. r = yield obj.fn(1, 4)
  113. self.assertEqual(r, "fish")
  114. r = yield obj.fn(2, 5)
  115. self.assertEqual(r, "chips")
  116. obj.mock.assert_not_called()
  117. def test_cache_with_sync_exception(self):
  118. """If the wrapped function throws synchronously, things should continue to work"""
  119. class Cls:
  120. @cached()
  121. def fn(self, arg1):
  122. raise SynapseError(100, "mai spoon iz too big!!1")
  123. obj = Cls()
  124. # this should fail immediately
  125. d = obj.fn(1)
  126. self.failureResultOf(d, SynapseError)
  127. # ... leaving the cache empty
  128. self.assertEqual(len(obj.fn.cache.cache), 0)
  129. # and a second call should result in a second exception
  130. d = obj.fn(1)
  131. self.failureResultOf(d, SynapseError)
  132. def test_cache_with_async_exception(self):
  133. """The wrapped function returns a failure"""
  134. class Cls:
  135. result = None
  136. call_count = 0
  137. @cached()
  138. def fn(self, arg1):
  139. self.call_count += 1
  140. return self.result
  141. obj = Cls()
  142. callbacks: Set[str] = set()
  143. # set off an asynchronous request
  144. obj.result = origin_d = defer.Deferred()
  145. d1 = obj.fn(1, on_invalidate=lambda: callbacks.add("d1"))
  146. self.assertFalse(d1.called)
  147. # a second request should also return a deferred, but should not call the
  148. # function itself.
  149. d2 = obj.fn(1, on_invalidate=lambda: callbacks.add("d2"))
  150. self.assertFalse(d2.called)
  151. self.assertEqual(obj.call_count, 1)
  152. # no callbacks yet
  153. self.assertEqual(callbacks, set())
  154. # the original request fails
  155. e = Exception("bzz")
  156. origin_d.errback(e)
  157. # ... which should cause the lookups to fail similarly
  158. self.assertIs(self.failureResultOf(d1, Exception).value, e)
  159. self.assertIs(self.failureResultOf(d2, Exception).value, e)
  160. # ... and the callbacks to have been, uh, called.
  161. self.assertEqual(callbacks, {"d1", "d2"})
  162. # ... leaving the cache empty
  163. self.assertEqual(len(obj.fn.cache.cache), 0)
  164. # and a second call should work as normal
  165. obj.result = defer.succeed(100)
  166. d3 = obj.fn(1)
  167. self.assertEqual(self.successResultOf(d3), 100)
  168. self.assertEqual(obj.call_count, 2)
  169. def test_cache_logcontexts(self):
  170. """Check that logcontexts are set and restored correctly when
  171. using the cache."""
  172. complete_lookup = defer.Deferred()
  173. class Cls:
  174. @descriptors.cached()
  175. def fn(self, arg1):
  176. @defer.inlineCallbacks
  177. def inner_fn():
  178. with PreserveLoggingContext():
  179. yield complete_lookup
  180. return 1
  181. return inner_fn()
  182. @defer.inlineCallbacks
  183. def do_lookup():
  184. with LoggingContext("c1") as c1:
  185. r = yield obj.fn(1)
  186. self.assertEqual(current_context(), c1)
  187. return r
  188. def check_result(r):
  189. self.assertEqual(r, 1)
  190. obj = Cls()
  191. # set off a deferred which will do a cache lookup
  192. d1 = do_lookup()
  193. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  194. d1.addCallback(check_result)
  195. # and another
  196. d2 = do_lookup()
  197. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  198. d2.addCallback(check_result)
  199. # let the lookup complete
  200. complete_lookup.callback(None)
  201. return defer.gatherResults([d1, d2])
  202. def test_cache_logcontexts_with_exception(self):
  203. """Check that the cache sets and restores logcontexts correctly when
  204. the lookup function throws an exception"""
  205. class Cls:
  206. @descriptors.cached()
  207. def fn(self, arg1):
  208. @defer.inlineCallbacks
  209. def inner_fn():
  210. # we want this to behave like an asynchronous function
  211. yield run_on_reactor()
  212. raise SynapseError(400, "blah")
  213. return inner_fn()
  214. @defer.inlineCallbacks
  215. def do_lookup():
  216. with LoggingContext("c1") as c1:
  217. try:
  218. d = obj.fn(1)
  219. self.assertEqual(
  220. current_context(),
  221. SENTINEL_CONTEXT,
  222. )
  223. yield d
  224. self.fail("No exception thrown")
  225. except SynapseError:
  226. pass
  227. self.assertEqual(current_context(), c1)
  228. # the cache should now be empty
  229. self.assertEqual(len(obj.fn.cache.cache), 0)
  230. obj = Cls()
  231. # set off a deferred which will do a cache lookup
  232. d1 = do_lookup()
  233. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  234. return d1
  235. @defer.inlineCallbacks
  236. def test_cache_default_args(self):
  237. class Cls:
  238. def __init__(self):
  239. self.mock = mock.Mock()
  240. @descriptors.cached()
  241. def fn(self, arg1, arg2=2, arg3=3):
  242. return self.mock(arg1, arg2, arg3)
  243. obj = Cls()
  244. obj.mock.return_value = "fish"
  245. r = yield obj.fn(1, 2, 3)
  246. self.assertEqual(r, "fish")
  247. obj.mock.assert_called_once_with(1, 2, 3)
  248. obj.mock.reset_mock()
  249. # a call with same params shouldn't call the mock again
  250. r = yield obj.fn(1, 2)
  251. self.assertEqual(r, "fish")
  252. obj.mock.assert_not_called()
  253. obj.mock.reset_mock()
  254. # a call with different params should call the mock again
  255. obj.mock.return_value = "chips"
  256. r = yield obj.fn(2, 3)
  257. self.assertEqual(r, "chips")
  258. obj.mock.assert_called_once_with(2, 3, 3)
  259. obj.mock.reset_mock()
  260. # the two values should now be cached
  261. r = yield obj.fn(1, 2)
  262. self.assertEqual(r, "fish")
  263. r = yield obj.fn(2, 3)
  264. self.assertEqual(r, "chips")
  265. obj.mock.assert_not_called()
  266. def test_cache_iterable(self):
  267. class Cls:
  268. def __init__(self):
  269. self.mock = mock.Mock()
  270. @descriptors.cached(iterable=True)
  271. def fn(self, arg1, arg2):
  272. return self.mock(arg1, arg2)
  273. obj = Cls()
  274. obj.mock.return_value = ["spam", "eggs"]
  275. r = obj.fn(1, 2)
  276. self.assertEqual(r.result, ["spam", "eggs"])
  277. obj.mock.assert_called_once_with(1, 2)
  278. obj.mock.reset_mock()
  279. # a call with different params should call the mock again
  280. obj.mock.return_value = ["chips"]
  281. r = obj.fn(1, 3)
  282. self.assertEqual(r.result, ["chips"])
  283. obj.mock.assert_called_once_with(1, 3)
  284. obj.mock.reset_mock()
  285. # the two values should now be cached
  286. self.assertEqual(len(obj.fn.cache.cache), 3)
  287. r = obj.fn(1, 2)
  288. self.assertEqual(r.result, ["spam", "eggs"])
  289. r = obj.fn(1, 3)
  290. self.assertEqual(r.result, ["chips"])
  291. obj.mock.assert_not_called()
  292. def test_cache_iterable_with_sync_exception(self):
  293. """If the wrapped function throws synchronously, things should continue to work"""
  294. class Cls:
  295. @descriptors.cached(iterable=True)
  296. def fn(self, arg1):
  297. raise SynapseError(100, "mai spoon iz too big!!1")
  298. obj = Cls()
  299. # this should fail immediately
  300. d = obj.fn(1)
  301. self.failureResultOf(d, SynapseError)
  302. # ... leaving the cache empty
  303. self.assertEqual(len(obj.fn.cache.cache), 0)
  304. # and a second call should result in a second exception
  305. d = obj.fn(1)
  306. self.failureResultOf(d, SynapseError)
  307. def test_invalidate_cascade(self):
  308. """Invalidations should cascade up through cache contexts"""
  309. class Cls:
  310. @cached(cache_context=True)
  311. async def func1(self, key, cache_context):
  312. return await self.func2(key, on_invalidate=cache_context.invalidate)
  313. @cached(cache_context=True)
  314. async def func2(self, key, cache_context):
  315. return self.func3(key, on_invalidate=cache_context.invalidate)
  316. @lru_cache(cache_context=True)
  317. def func3(self, key, cache_context):
  318. self.invalidate = cache_context.invalidate
  319. return 42
  320. obj = Cls()
  321. top_invalidate = mock.Mock()
  322. r = get_awaitable_result(obj.func1("k1", on_invalidate=top_invalidate))
  323. self.assertEqual(r, 42)
  324. obj.invalidate()
  325. top_invalidate.assert_called_once()
  326. class CacheDecoratorTestCase(unittest.HomeserverTestCase):
  327. """More tests for @cached
  328. The following is a set of tests that got lost in a different file for a while.
  329. There are probably duplicates of the tests in DescriptorTestCase. Ideally the
  330. duplicates would be removed and the two sets of classes combined.
  331. """
  332. @defer.inlineCallbacks
  333. def test_passthrough(self):
  334. class A:
  335. @cached()
  336. def func(self, key):
  337. return key
  338. a = A()
  339. self.assertEquals((yield a.func("foo")), "foo")
  340. self.assertEquals((yield a.func("bar")), "bar")
  341. @defer.inlineCallbacks
  342. def test_hit(self):
  343. callcount = [0]
  344. class A:
  345. @cached()
  346. def func(self, key):
  347. callcount[0] += 1
  348. return key
  349. a = A()
  350. yield a.func("foo")
  351. self.assertEquals(callcount[0], 1)
  352. self.assertEquals((yield a.func("foo")), "foo")
  353. self.assertEquals(callcount[0], 1)
  354. @defer.inlineCallbacks
  355. def test_invalidate(self):
  356. callcount = [0]
  357. class A:
  358. @cached()
  359. def func(self, key):
  360. callcount[0] += 1
  361. return key
  362. a = A()
  363. yield a.func("foo")
  364. self.assertEquals(callcount[0], 1)
  365. a.func.invalidate(("foo",))
  366. yield a.func("foo")
  367. self.assertEquals(callcount[0], 2)
  368. def test_invalidate_missing(self):
  369. class A:
  370. @cached()
  371. def func(self, key):
  372. return key
  373. A().func.invalidate(("what",))
  374. @defer.inlineCallbacks
  375. def test_max_entries(self):
  376. callcount = [0]
  377. class A:
  378. @cached(max_entries=10)
  379. def func(self, key):
  380. callcount[0] += 1
  381. return key
  382. a = A()
  383. for k in range(0, 12):
  384. yield a.func(k)
  385. self.assertEquals(callcount[0], 12)
  386. # There must have been at least 2 evictions, meaning if we calculate
  387. # all 12 values again, we must get called at least 2 more times
  388. for k in range(0, 12):
  389. yield a.func(k)
  390. self.assertTrue(
  391. callcount[0] >= 14, msg="Expected callcount >= 14, got %d" % (callcount[0])
  392. )
  393. def test_prefill(self):
  394. callcount = [0]
  395. d = defer.succeed(123)
  396. class A:
  397. @cached()
  398. def func(self, key):
  399. callcount[0] += 1
  400. return d
  401. a = A()
  402. a.func.prefill(("foo",), 456)
  403. self.assertEquals(a.func("foo").result, 456)
  404. self.assertEquals(callcount[0], 0)
  405. @defer.inlineCallbacks
  406. def test_invalidate_context(self):
  407. callcount = [0]
  408. callcount2 = [0]
  409. class A:
  410. @cached()
  411. def func(self, key):
  412. callcount[0] += 1
  413. return key
  414. @cached(cache_context=True)
  415. def func2(self, key, cache_context):
  416. callcount2[0] += 1
  417. return self.func(key, on_invalidate=cache_context.invalidate)
  418. a = A()
  419. yield a.func2("foo")
  420. self.assertEquals(callcount[0], 1)
  421. self.assertEquals(callcount2[0], 1)
  422. a.func.invalidate(("foo",))
  423. yield a.func("foo")
  424. self.assertEquals(callcount[0], 2)
  425. self.assertEquals(callcount2[0], 1)
  426. yield a.func2("foo")
  427. self.assertEquals(callcount[0], 2)
  428. self.assertEquals(callcount2[0], 2)
  429. @defer.inlineCallbacks
  430. def test_eviction_context(self):
  431. callcount = [0]
  432. callcount2 = [0]
  433. class A:
  434. @cached(max_entries=2)
  435. def func(self, key):
  436. callcount[0] += 1
  437. return key
  438. @cached(cache_context=True)
  439. def func2(self, key, cache_context):
  440. callcount2[0] += 1
  441. return self.func(key, on_invalidate=cache_context.invalidate)
  442. a = A()
  443. yield a.func2("foo")
  444. yield a.func2("foo2")
  445. self.assertEquals(callcount[0], 2)
  446. self.assertEquals(callcount2[0], 2)
  447. yield a.func2("foo")
  448. self.assertEquals(callcount[0], 2)
  449. self.assertEquals(callcount2[0], 2)
  450. yield a.func("foo3")
  451. self.assertEquals(callcount[0], 3)
  452. self.assertEquals(callcount2[0], 2)
  453. yield a.func2("foo")
  454. self.assertEquals(callcount[0], 4)
  455. self.assertEquals(callcount2[0], 3)
  456. @defer.inlineCallbacks
  457. def test_double_get(self):
  458. callcount = [0]
  459. callcount2 = [0]
  460. class A:
  461. @cached()
  462. def func(self, key):
  463. callcount[0] += 1
  464. return key
  465. @cached(cache_context=True)
  466. def func2(self, key, cache_context):
  467. callcount2[0] += 1
  468. return self.func(key, on_invalidate=cache_context.invalidate)
  469. a = A()
  470. a.func2.cache.cache = mock.Mock(wraps=a.func2.cache.cache)
  471. yield a.func2("foo")
  472. self.assertEquals(callcount[0], 1)
  473. self.assertEquals(callcount2[0], 1)
  474. a.func2.invalidate(("foo",))
  475. self.assertEquals(a.func2.cache.cache.del_multi.call_count, 1)
  476. yield a.func2("foo")
  477. a.func2.invalidate(("foo",))
  478. self.assertEquals(a.func2.cache.cache.del_multi.call_count, 2)
  479. self.assertEquals(callcount[0], 1)
  480. self.assertEquals(callcount2[0], 2)
  481. a.func.invalidate(("foo",))
  482. self.assertEquals(a.func2.cache.cache.del_multi.call_count, 3)
  483. yield a.func("foo")
  484. self.assertEquals(callcount[0], 2)
  485. self.assertEquals(callcount2[0], 2)
  486. yield a.func2("foo")
  487. self.assertEquals(callcount[0], 2)
  488. self.assertEquals(callcount2[0], 3)
  489. class CachedListDescriptorTestCase(unittest.TestCase):
  490. @defer.inlineCallbacks
  491. def test_cache(self):
  492. class Cls:
  493. def __init__(self):
  494. self.mock = mock.Mock()
  495. @descriptors.cached()
  496. def fn(self, arg1, arg2):
  497. pass
  498. @descriptors.cachedList("fn", "args1")
  499. async def list_fn(self, args1, arg2):
  500. assert current_context().name == "c1"
  501. # we want this to behave like an asynchronous function
  502. await run_on_reactor()
  503. assert current_context().name == "c1"
  504. return self.mock(args1, arg2)
  505. with LoggingContext("c1") as c1:
  506. obj = Cls()
  507. obj.mock.return_value = {10: "fish", 20: "chips"}
  508. # start the lookup off
  509. d1 = obj.list_fn([10, 20], 2)
  510. self.assertEqual(current_context(), SENTINEL_CONTEXT)
  511. r = yield d1
  512. self.assertEqual(current_context(), c1)
  513. obj.mock.assert_called_once_with((10, 20), 2)
  514. self.assertEqual(r, {10: "fish", 20: "chips"})
  515. obj.mock.reset_mock()
  516. # a call with different params should call the mock again
  517. obj.mock.return_value = {30: "peas"}
  518. r = yield obj.list_fn([20, 30], 2)
  519. obj.mock.assert_called_once_with((30,), 2)
  520. self.assertEqual(r, {20: "chips", 30: "peas"})
  521. obj.mock.reset_mock()
  522. # all the values should now be cached
  523. r = yield obj.fn(10, 2)
  524. self.assertEqual(r, "fish")
  525. r = yield obj.fn(20, 2)
  526. self.assertEqual(r, "chips")
  527. r = yield obj.fn(30, 2)
  528. self.assertEqual(r, "peas")
  529. r = yield obj.list_fn([10, 20, 30], 2)
  530. obj.mock.assert_not_called()
  531. self.assertEqual(r, {10: "fish", 20: "chips", 30: "peas"})
  532. # we should also be able to use a (single-use) iterable, and should
  533. # deduplicate the keys
  534. obj.mock.reset_mock()
  535. obj.mock.return_value = {40: "gravy"}
  536. iterable = (x for x in [10, 40, 40])
  537. r = yield obj.list_fn(iterable, 2)
  538. obj.mock.assert_called_once_with((40,), 2)
  539. self.assertEqual(r, {10: "fish", 40: "gravy"})
  540. @defer.inlineCallbacks
  541. def test_invalidate(self):
  542. """Make sure that invalidation callbacks are called."""
  543. class Cls:
  544. def __init__(self):
  545. self.mock = mock.Mock()
  546. @descriptors.cached()
  547. def fn(self, arg1, arg2):
  548. pass
  549. @descriptors.cachedList("fn", "args1")
  550. async def list_fn(self, args1, arg2):
  551. # we want this to behave like an asynchronous function
  552. await run_on_reactor()
  553. return self.mock(args1, arg2)
  554. obj = Cls()
  555. invalidate0 = mock.Mock()
  556. invalidate1 = mock.Mock()
  557. # cache miss
  558. obj.mock.return_value = {10: "fish", 20: "chips"}
  559. r1 = yield obj.list_fn([10, 20], 2, on_invalidate=invalidate0)
  560. obj.mock.assert_called_once_with((10, 20), 2)
  561. self.assertEqual(r1, {10: "fish", 20: "chips"})
  562. obj.mock.reset_mock()
  563. # cache hit
  564. r2 = yield obj.list_fn([10, 20], 2, on_invalidate=invalidate1)
  565. obj.mock.assert_not_called()
  566. self.assertEqual(r2, {10: "fish", 20: "chips"})
  567. invalidate0.assert_not_called()
  568. invalidate1.assert_not_called()
  569. # now if we invalidate the keys, both invalidations should get called
  570. obj.fn.invalidate((10, 2))
  571. invalidate0.assert_called_once()
  572. invalidate1.assert_called_once()