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
|
2023-01-26 20:45:24 +01:00
|
|
|
from typing import Any, Generator, Tuple, cast
|
2021-04-09 19:44:38 +02:00
|
|
|
from unittest.mock import Mock, call
|
2018-05-21 17:49:59 +02:00
|
|
|
|
2023-01-26 20:45:24 +01:00
|
|
|
from twisted.internet import defer, reactor as _reactor
|
2018-07-09 08:09:20 +02:00
|
|
|
|
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
|
2023-01-26 20:45:24 +01:00
|
|
|
from synapse.types import ISynapseReactor, JsonDict
|
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
|
|
|
|
|
2023-01-26 20:45:24 +01:00
|
|
|
reactor = cast(ISynapseReactor, _reactor)
|
|
|
|
|
2016-11-14 12:19:24 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-01-26 20:45:24 +01:00
|
|
|
self.mock_http_response = (HTTPStatus.OK, {"result": "GOOD JOB!"})
|
2016-11-14 12:19:24 +01:00
|
|
|
self.mock_key = "foo"
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2023-01-26 20:45:24 +01:00
|
|
|
def test_executes_given_function(
|
|
|
|
self,
|
|
|
|
) -> Generator["defer.Deferred[Any]", object, None]:
|
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
|
2023-01-26 20:45:24 +01:00
|
|
|
def test_deduplicates_based_on_key(
|
|
|
|
self,
|
|
|
|
) -> Generator["defer.Deferred[Any]", object, None]:
|
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
|
2023-01-26 20:45:24 +01:00
|
|
|
def test_logcontexts_with_async_result(
|
|
|
|
self,
|
|
|
|
) -> Generator["defer.Deferred[Any]", object, None]:
|
2018-05-21 17:58:20 +02:00
|
|
|
@defer.inlineCallbacks
|
2023-01-26 20:45:24 +01:00
|
|
|
def cb() -> Generator["defer.Deferred[object]", object, Tuple[int, JsonDict]]:
|
2018-06-22 10:37:10 +02:00
|
|
|
yield Clock(reactor).sleep(0)
|
2023-01-26 20:45:24 +01:00
|
|
|
return 1, {}
|
2018-05-21 17:58:20 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2023-01-26 20:45:24 +01:00
|
|
|
def test() -> Generator["defer.Deferred[Any]", object, None]:
|
2018-05-21 17:58:20 +02:00
|
|
|
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)
|
2023-01-26 20:45:24 +01:00
|
|
|
self.assertEqual(res, (1, {}))
|
2018-05-21 17:58:20 +02:00
|
|
|
|
|
|
|
# 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
|
2023-01-26 20:45:24 +01:00
|
|
|
def test_does_not_cache_exceptions(
|
|
|
|
self,
|
|
|
|
) -> Generator["defer.Deferred[Any]", object, None]:
|
2018-05-21 17:49:59 +02:00
|
|
|
"""Checks that, if the callback throws an exception, it is called again
|
|
|
|
for the next request.
|
|
|
|
"""
|
|
|
|
called = [False]
|
|
|
|
|
2023-01-26 20:45:24 +01:00
|
|
|
def cb() -> "defer.Deferred[Tuple[int, JsonDict]]":
|
2018-05-21 17:49:59 +02:00
|
|
|
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
|
2023-01-26 20:45:24 +01:00
|
|
|
def test_does_not_cache_failures(
|
|
|
|
self,
|
|
|
|
) -> Generator["defer.Deferred[Any]", object, None]:
|
2018-05-21 17:49:59 +02:00
|
|
|
"""Checks that, if the callback returns a failure, it is called again
|
|
|
|
for the next request.
|
|
|
|
"""
|
|
|
|
called = [False]
|
|
|
|
|
2023-01-26 20:45:24 +01:00
|
|
|
def cb() -> "defer.Deferred[Tuple[int, JsonDict]]":
|
2018-05-21 17:49:59 +02:00
|
|
|
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
|
2023-01-26 20:45:24 +01:00
|
|
|
def test_cleans_up(self) -> Generator["defer.Deferred[Any]", object, None]:
|
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")])
|