2022-03-11 13:42:22 +01:00
|
|
|
# Copyright 2018-2021 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from http import HTTPStatus
|
2021-04-09 19:44:38 +02:00
|
|
|
from unittest.mock import Mock, call
|
2018-05-21 17:49:59 +02:00
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from twisted.internet import defer, reactor
|
|
|
|
|
2020-03-24 15:45:33 +01:00
|
|
|
from synapse.logging.context import SENTINEL_CONTEXT, LoggingContext, current_context
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.rest.client.transactions import CLEANUP_PERIOD_MS, HttpTransactionCache
|
2018-06-22 10:37:10 +02:00
|
|
|
from synapse.util import Clock
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2016-11-14 12:19:24 +01:00
|
|
|
from tests import unittest
|
2022-04-27 15:58:26 +02:00
|
|
|
from tests.test_utils import make_awaitable
|
2016-11-14 12:19:24 +01:00
|
|
|
from tests.utils import MockClock
|
|
|
|
|
|
|
|
|
|
|
|
class HttpTransactionCacheTestCase(unittest.TestCase):
|
2022-03-11 13:42:22 +01:00
|
|
|
def setUp(self) -> None:
|
2016-11-14 12:19:24 +01:00
|
|
|
self.clock = MockClock()
|
2018-07-13 23:34:49 +02:00
|
|
|
self.hs = Mock()
|
|
|
|
self.hs.get_clock = Mock(return_value=self.clock)
|
|
|
|
self.hs.get_auth = Mock()
|
|
|
|
self.cache = HttpTransactionCache(self.hs)
|
2016-11-14 12:19:24 +01:00
|
|
|
|
2022-03-11 13:42:22 +01:00
|
|
|
self.mock_http_response = (HTTPStatus.OK, "GOOD JOB!")
|
2016-11-14 12:19:24 +01:00
|
|
|
self.mock_key = "foo"
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_executes_given_function(self):
|
2022-04-27 15:58:26 +02:00
|
|
|
cb = Mock(return_value=make_awaitable(self.mock_http_response))
|
2016-11-14 12:19:24 +01:00
|
|
|
res = yield self.cache.fetch_or_execute(
|
|
|
|
self.mock_key, cb, "some_arg", keyword="arg"
|
|
|
|
)
|
|
|
|
cb.assert_called_once_with("some_arg", keyword="arg")
|
|
|
|
self.assertEqual(res, self.mock_http_response)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_deduplicates_based_on_key(self):
|
2022-04-27 15:58:26 +02:00
|
|
|
cb = Mock(return_value=make_awaitable(self.mock_http_response))
|
2016-11-14 12:19:24 +01:00
|
|
|
for i in range(3): # invoke multiple times
|
|
|
|
res = yield self.cache.fetch_or_execute(
|
|
|
|
self.mock_key, cb, "some_arg", keyword="arg", changing_args=i
|
|
|
|
)
|
|
|
|
self.assertEqual(res, self.mock_http_response)
|
|
|
|
# expect only a single call to do the work
|
|
|
|
cb.assert_called_once_with("some_arg", keyword="arg", changing_args=0)
|
|
|
|
|
2018-05-21 17:58:20 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_logcontexts_with_async_result(self):
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def cb():
|
2018-06-22 10:37:10 +02:00
|
|
|
yield Clock(reactor).sleep(0)
|
2019-07-23 15:00:55 +02:00
|
|
|
return "yay"
|
2018-05-21 17:58:20 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test():
|
|
|
|
with LoggingContext("c") as c1:
|
|
|
|
res = yield self.cache.fetch_or_execute(self.mock_key, cb)
|
2020-03-24 15:45:33 +01:00
|
|
|
self.assertIs(current_context(), c1)
|
2018-05-21 17:58:20 +02:00
|
|
|
self.assertEqual(res, "yay")
|
|
|
|
|
|
|
|
# run the test twice in parallel
|
|
|
|
d = defer.gatherResults([test(), test()])
|
2020-03-24 15:45:33 +01:00
|
|
|
self.assertIs(current_context(), SENTINEL_CONTEXT)
|
2018-05-21 17:58:20 +02:00
|
|
|
yield d
|
2020-03-24 15:45:33 +01:00
|
|
|
self.assertIs(current_context(), SENTINEL_CONTEXT)
|
2018-05-21 17:58:20 +02:00
|
|
|
|
2018-05-21 17:49:59 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_does_not_cache_exceptions(self):
|
|
|
|
"""Checks that, if the callback throws an exception, it is called again
|
|
|
|
for the next request.
|
|
|
|
"""
|
|
|
|
called = [False]
|
|
|
|
|
|
|
|
def cb():
|
|
|
|
if called[0]:
|
|
|
|
# return a valid result the second time
|
|
|
|
return defer.succeed(self.mock_http_response)
|
|
|
|
|
|
|
|
called[0] = True
|
|
|
|
raise Exception("boo")
|
|
|
|
|
|
|
|
with LoggingContext("test") as test_context:
|
|
|
|
try:
|
|
|
|
yield self.cache.fetch_or_execute(self.mock_key, cb)
|
|
|
|
except Exception as e:
|
2018-08-09 04:22:01 +02:00
|
|
|
self.assertEqual(e.args[0], "boo")
|
2020-03-24 15:45:33 +01:00
|
|
|
self.assertIs(current_context(), test_context)
|
2018-05-21 17:49:59 +02:00
|
|
|
|
|
|
|
res = yield self.cache.fetch_or_execute(self.mock_key, cb)
|
|
|
|
self.assertEqual(res, self.mock_http_response)
|
2020-03-24 15:45:33 +01:00
|
|
|
self.assertIs(current_context(), test_context)
|
2018-05-21 17:49:59 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_does_not_cache_failures(self):
|
|
|
|
"""Checks that, if the callback returns a failure, it is called again
|
|
|
|
for the next request.
|
|
|
|
"""
|
|
|
|
called = [False]
|
|
|
|
|
|
|
|
def cb():
|
|
|
|
if called[0]:
|
|
|
|
# return a valid result the second time
|
|
|
|
return defer.succeed(self.mock_http_response)
|
|
|
|
|
|
|
|
called[0] = True
|
|
|
|
return defer.fail(Exception("boo"))
|
|
|
|
|
|
|
|
with LoggingContext("test") as test_context:
|
|
|
|
try:
|
|
|
|
yield self.cache.fetch_or_execute(self.mock_key, cb)
|
|
|
|
except Exception as e:
|
2018-08-09 04:22:01 +02:00
|
|
|
self.assertEqual(e.args[0], "boo")
|
2020-03-24 15:45:33 +01:00
|
|
|
self.assertIs(current_context(), test_context)
|
2018-05-21 17:49:59 +02:00
|
|
|
|
|
|
|
res = yield self.cache.fetch_or_execute(self.mock_key, cb)
|
|
|
|
self.assertEqual(res, self.mock_http_response)
|
2020-03-24 15:45:33 +01:00
|
|
|
self.assertIs(current_context(), test_context)
|
2018-05-21 17:49:59 +02:00
|
|
|
|
2016-11-14 12:19:24 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_cleans_up(self):
|
2022-04-27 15:58:26 +02:00
|
|
|
cb = Mock(return_value=make_awaitable(self.mock_http_response))
|
2016-11-14 12:19:24 +01:00
|
|
|
yield self.cache.fetch_or_execute(self.mock_key, cb, "an arg")
|
|
|
|
# should NOT have cleaned up yet
|
|
|
|
self.clock.advance_time_msec(CLEANUP_PERIOD_MS / 2)
|
|
|
|
|
|
|
|
yield self.cache.fetch_or_execute(self.mock_key, cb, "an arg")
|
|
|
|
# still using cache
|
|
|
|
cb.assert_called_once_with("an arg")
|
|
|
|
|
|
|
|
self.clock.advance_time_msec(CLEANUP_PERIOD_MS)
|
|
|
|
|
|
|
|
yield self.cache.fetch_or_execute(self.mock_key, cb, "an arg")
|
|
|
|
# no longer using cache
|
|
|
|
self.assertEqual(cb.call_count, 2)
|
|
|
|
self.assertEqual(cb.call_args_list, [call("an arg"), call("an arg")])
|