test_transactions.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from unittest.mock import Mock, call
  2. from twisted.internet import defer, reactor
  3. from synapse.logging.context import SENTINEL_CONTEXT, LoggingContext, current_context
  4. from synapse.rest.client.transactions import CLEANUP_PERIOD_MS, HttpTransactionCache
  5. from synapse.util import Clock
  6. from tests import unittest
  7. from tests.utils import MockClock
  8. class HttpTransactionCacheTestCase(unittest.TestCase):
  9. def setUp(self):
  10. self.clock = MockClock()
  11. self.hs = Mock()
  12. self.hs.get_clock = Mock(return_value=self.clock)
  13. self.hs.get_auth = Mock()
  14. self.cache = HttpTransactionCache(self.hs)
  15. self.mock_http_response = (200, "GOOD JOB!")
  16. self.mock_key = "foo"
  17. @defer.inlineCallbacks
  18. def test_executes_given_function(self):
  19. cb = Mock(return_value=defer.succeed(self.mock_http_response))
  20. res = yield self.cache.fetch_or_execute(
  21. self.mock_key, cb, "some_arg", keyword="arg"
  22. )
  23. cb.assert_called_once_with("some_arg", keyword="arg")
  24. self.assertEqual(res, self.mock_http_response)
  25. @defer.inlineCallbacks
  26. def test_deduplicates_based_on_key(self):
  27. cb = Mock(return_value=defer.succeed(self.mock_http_response))
  28. for i in range(3): # invoke multiple times
  29. res = yield self.cache.fetch_or_execute(
  30. self.mock_key, cb, "some_arg", keyword="arg", changing_args=i
  31. )
  32. self.assertEqual(res, self.mock_http_response)
  33. # expect only a single call to do the work
  34. cb.assert_called_once_with("some_arg", keyword="arg", changing_args=0)
  35. @defer.inlineCallbacks
  36. def test_logcontexts_with_async_result(self):
  37. @defer.inlineCallbacks
  38. def cb():
  39. yield Clock(reactor).sleep(0)
  40. return "yay"
  41. @defer.inlineCallbacks
  42. def test():
  43. with LoggingContext("c") as c1:
  44. res = yield self.cache.fetch_or_execute(self.mock_key, cb)
  45. self.assertIs(current_context(), c1)
  46. self.assertEqual(res, "yay")
  47. # run the test twice in parallel
  48. d = defer.gatherResults([test(), test()])
  49. self.assertIs(current_context(), SENTINEL_CONTEXT)
  50. yield d
  51. self.assertIs(current_context(), SENTINEL_CONTEXT)
  52. @defer.inlineCallbacks
  53. def test_does_not_cache_exceptions(self):
  54. """Checks that, if the callback throws an exception, it is called again
  55. for the next request.
  56. """
  57. called = [False]
  58. def cb():
  59. if called[0]:
  60. # return a valid result the second time
  61. return defer.succeed(self.mock_http_response)
  62. called[0] = True
  63. raise Exception("boo")
  64. with LoggingContext("test") as test_context:
  65. try:
  66. yield self.cache.fetch_or_execute(self.mock_key, cb)
  67. except Exception as e:
  68. self.assertEqual(e.args[0], "boo")
  69. self.assertIs(current_context(), test_context)
  70. res = yield self.cache.fetch_or_execute(self.mock_key, cb)
  71. self.assertEqual(res, self.mock_http_response)
  72. self.assertIs(current_context(), test_context)
  73. @defer.inlineCallbacks
  74. def test_does_not_cache_failures(self):
  75. """Checks that, if the callback returns a failure, it is called again
  76. for the next request.
  77. """
  78. called = [False]
  79. def cb():
  80. if called[0]:
  81. # return a valid result the second time
  82. return defer.succeed(self.mock_http_response)
  83. called[0] = True
  84. return defer.fail(Exception("boo"))
  85. with LoggingContext("test") as test_context:
  86. try:
  87. yield self.cache.fetch_or_execute(self.mock_key, cb)
  88. except Exception as e:
  89. self.assertEqual(e.args[0], "boo")
  90. self.assertIs(current_context(), test_context)
  91. res = yield self.cache.fetch_or_execute(self.mock_key, cb)
  92. self.assertEqual(res, self.mock_http_response)
  93. self.assertIs(current_context(), test_context)
  94. @defer.inlineCallbacks
  95. def test_cleans_up(self):
  96. cb = Mock(return_value=defer.succeed(self.mock_http_response))
  97. yield self.cache.fetch_or_execute(self.mock_key, cb, "an arg")
  98. # should NOT have cleaned up yet
  99. self.clock.advance_time_msec(CLEANUP_PERIOD_MS / 2)
  100. yield self.cache.fetch_or_execute(self.mock_key, cb, "an arg")
  101. # still using cache
  102. cb.assert_called_once_with("an arg")
  103. self.clock.advance_time_msec(CLEANUP_PERIOD_MS)
  104. yield self.cache.fetch_or_execute(self.mock_key, cb, "an arg")
  105. # no longer using cache
  106. self.assertEqual(cb.call_count, 2)
  107. self.assertEqual(cb.call_args_list, [call("an arg"), call("an arg")])