test_cached_call.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from unittest.mock import Mock
  15. from twisted.internet import defer
  16. from twisted.internet.defer import Deferred
  17. from synapse.util.caches.cached_call import CachedCall, RetryOnExceptionCachedCall
  18. from tests.test_utils import get_awaitable_result
  19. from tests.unittest import TestCase
  20. class CachedCallTestCase(TestCase):
  21. def test_get(self):
  22. """
  23. Happy-path test case: makes a couple of calls and makes sure they behave
  24. correctly
  25. """
  26. d = Deferred()
  27. async def f():
  28. return await d
  29. slow_call = Mock(side_effect=f)
  30. cached_call = CachedCall(slow_call)
  31. # the mock should not yet have been called
  32. slow_call.assert_not_called()
  33. # now fire off a couple of calls
  34. completed_results = []
  35. async def r():
  36. res = await cached_call.get()
  37. completed_results.append(res)
  38. r1 = defer.ensureDeferred(r())
  39. r2 = defer.ensureDeferred(r())
  40. # neither result should be complete yet
  41. self.assertNoResult(r1)
  42. self.assertNoResult(r2)
  43. # and the mock should have been called *once*, with no params
  44. slow_call.assert_called_once_with()
  45. # allow the deferred to complete, which should complete both the pending results
  46. d.callback(123)
  47. self.assertEqual(completed_results, [123, 123])
  48. self.successResultOf(r1)
  49. self.successResultOf(r2)
  50. # another call to the getter should complete immediately
  51. slow_call.reset_mock()
  52. r3 = get_awaitable_result(cached_call.get())
  53. self.assertEqual(r3, 123)
  54. slow_call.assert_not_called()
  55. def test_fast_call(self):
  56. """
  57. Test the behaviour when the underlying function completes immediately
  58. """
  59. async def f():
  60. return 12
  61. fast_call = Mock(side_effect=f)
  62. cached_call = CachedCall(fast_call)
  63. # the mock should not yet have been called
  64. fast_call.assert_not_called()
  65. # run the call a couple of times, which should complete immediately
  66. self.assertEqual(get_awaitable_result(cached_call.get()), 12)
  67. self.assertEqual(get_awaitable_result(cached_call.get()), 12)
  68. # the mock should have been called once
  69. fast_call.assert_called_once_with()
  70. class RetryOnExceptionCachedCallTestCase(TestCase):
  71. def test_get(self):
  72. # set up the RetryOnExceptionCachedCall around a function which will fail
  73. # (after a while)
  74. d = Deferred()
  75. async def f1():
  76. await d
  77. raise ValueError("moo")
  78. slow_call = Mock(side_effect=f1)
  79. cached_call = RetryOnExceptionCachedCall(slow_call)
  80. # the mock should not yet have been called
  81. slow_call.assert_not_called()
  82. # now fire off a couple of calls
  83. completed_results = []
  84. async def r():
  85. try:
  86. await cached_call.get()
  87. except Exception as e1:
  88. completed_results.append(e1)
  89. r1 = defer.ensureDeferred(r())
  90. r2 = defer.ensureDeferred(r())
  91. # neither result should be complete yet
  92. self.assertNoResult(r1)
  93. self.assertNoResult(r2)
  94. # and the mock should have been called *once*, with no params
  95. slow_call.assert_called_once_with()
  96. # complete the deferred, which should make the pending calls fail
  97. d.callback(0)
  98. self.assertEqual(len(completed_results), 2)
  99. for e in completed_results:
  100. self.assertIsInstance(e, ValueError)
  101. self.assertEqual(e.args, ("moo",))
  102. # reset the mock to return a successful result, and make another pair of calls
  103. # to the getter
  104. d = Deferred()
  105. async def f2():
  106. return await d
  107. slow_call.reset_mock()
  108. slow_call.side_effect = f2
  109. r3 = defer.ensureDeferred(cached_call.get())
  110. r4 = defer.ensureDeferred(cached_call.get())
  111. self.assertNoResult(r3)
  112. self.assertNoResult(r4)
  113. slow_call.assert_called_once_with()
  114. # let that call complete, and check the results
  115. d.callback(123)
  116. self.assertEqual(self.successResultOf(r3), 123)
  117. self.assertEqual(self.successResultOf(r4), 123)
  118. # and now more calls to the getter should complete immediately
  119. slow_call.reset_mock()
  120. self.assertEqual(get_awaitable_result(cached_call.get()), 123)
  121. slow_call.assert_not_called()