2016-04-06 10:44:22 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2016-04-06 10:44:22 -04:00
|
|
|
#
|
|
|
|
#
|
2018-06-22 04:37:10 -04:00
|
|
|
|
2022-04-05 12:19:16 -04:00
|
|
|
from typing import Hashable, Tuple
|
|
|
|
|
|
|
|
from typing_extensions import Protocol
|
2022-04-05 09:56:09 -04:00
|
|
|
|
2018-06-22 04:37:10 -04:00
|
|
|
from twisted.internet import defer, reactor
|
2022-04-05 09:56:09 -04:00
|
|
|
from twisted.internet.base import ReactorBase
|
|
|
|
from twisted.internet.defer import CancelledError, Deferred
|
2016-04-06 10:44:22 -04:00
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
from synapse.logging.context import LoggingContext, current_context
|
2018-08-10 09:50:21 -04:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2018-07-09 02:09:20 -04:00
|
|
|
|
|
|
|
from tests import unittest
|
2016-04-06 10:44:22 -04:00
|
|
|
|
|
|
|
|
2022-04-05 12:19:16 -04:00
|
|
|
class UnblockFunction(Protocol):
|
|
|
|
def __call__(self, pump_reactor: bool = True) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2016-04-06 10:44:22 -04:00
|
|
|
class LinearizerTestCase(unittest.TestCase):
|
2022-04-05 09:56:09 -04:00
|
|
|
def _start_task(
|
|
|
|
self, linearizer: Linearizer, key: Hashable
|
2022-04-05 12:19:16 -04:00
|
|
|
) -> Tuple["Deferred[None]", "Deferred[None]", UnblockFunction]:
|
2022-04-05 09:56:09 -04:00
|
|
|
"""Starts a task which acquires the linearizer lock, blocks, then completes.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
linearizer: The `Linearizer`.
|
|
|
|
key: The `Linearizer` key.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A tuple containing:
|
|
|
|
* A cancellable `Deferred` for the entire task.
|
|
|
|
* A `Deferred` that resolves once the task acquires the lock.
|
|
|
|
* A function that unblocks the task. Must be called by the caller
|
|
|
|
to allow the task to release the lock and complete.
|
|
|
|
"""
|
|
|
|
acquired_d: "Deferred[None]" = Deferred()
|
|
|
|
unblock_d: "Deferred[None]" = Deferred()
|
|
|
|
|
|
|
|
async def task() -> None:
|
2022-04-05 10:43:52 -04:00
|
|
|
async with linearizer.queue(key):
|
2022-04-05 09:56:09 -04:00
|
|
|
acquired_d.callback(None)
|
|
|
|
await unblock_d
|
|
|
|
|
|
|
|
d = defer.ensureDeferred(task())
|
|
|
|
|
2022-04-05 12:19:16 -04:00
|
|
|
def unblock(pump_reactor: bool = True) -> None:
|
2022-04-05 09:56:09 -04:00
|
|
|
unblock_d.callback(None)
|
|
|
|
# The next task, if it exists, will acquire the lock and require a kick of
|
|
|
|
# the reactor to advance.
|
2022-04-05 12:19:16 -04:00
|
|
|
if pump_reactor:
|
|
|
|
self._pump()
|
2022-04-05 09:56:09 -04:00
|
|
|
|
|
|
|
return d, acquired_d, unblock
|
|
|
|
|
|
|
|
def _pump(self) -> None:
|
|
|
|
"""Pump the reactor to advance `Linearizer`s."""
|
|
|
|
assert isinstance(reactor, ReactorBase)
|
|
|
|
while reactor.getDelayedCalls():
|
|
|
|
reactor.runUntilCurrent()
|
|
|
|
|
|
|
|
def test_linearizer(self) -> None:
|
|
|
|
"""Tests that a task is queued up behind an earlier task."""
|
2016-04-06 10:44:22 -04:00
|
|
|
linearizer = Linearizer()
|
|
|
|
|
|
|
|
key = object()
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
_, acquired_d1, unblock1 = self._start_task(linearizer, key)
|
|
|
|
self.assertTrue(acquired_d1.called)
|
|
|
|
|
|
|
|
_, acquired_d2, unblock2 = self._start_task(linearizer, key)
|
|
|
|
self.assertFalse(acquired_d2.called)
|
2016-04-06 10:44:22 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Once the first task is done, the second task can continue.
|
|
|
|
unblock1()
|
|
|
|
self.assertTrue(acquired_d2.called)
|
2016-04-06 10:44:22 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
unblock2()
|
2016-04-06 10:44:22 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
def test_linearizer_is_queued(self) -> None:
|
|
|
|
"""Tests `Linearizer.is_queued`.
|
2017-10-11 10:05:05 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
Runs through the same scenario as `test_linearizer`.
|
|
|
|
"""
|
2020-05-27 14:41:06 -04:00
|
|
|
linearizer = Linearizer()
|
|
|
|
|
|
|
|
key = object()
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
_, acquired_d1, unblock1 = self._start_task(linearizer, key)
|
|
|
|
self.assertTrue(acquired_d1.called)
|
2020-05-27 14:41:06 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Since the first task acquires the lock immediately, "is_queued" should return
|
|
|
|
# false.
|
2020-05-27 14:41:06 -04:00
|
|
|
self.assertFalse(linearizer.is_queued(key))
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
_, acquired_d2, unblock2 = self._start_task(linearizer, key)
|
|
|
|
self.assertFalse(acquired_d2.called)
|
2020-05-27 14:41:06 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Now the second task is queued up behind the first.
|
2020-05-27 14:41:06 -04:00
|
|
|
self.assertTrue(linearizer.is_queued(key))
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
unblock1()
|
2020-05-27 14:41:06 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# And now the second task acquires the lock and nothing is in the queue again.
|
|
|
|
self.assertTrue(acquired_d2.called)
|
2020-05-27 14:41:06 -04:00
|
|
|
self.assertFalse(linearizer.is_queued(key))
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
unblock2()
|
2020-05-27 14:41:06 -04:00
|
|
|
self.assertFalse(linearizer.is_queued(key))
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
def test_lots_of_queued_things(self) -> None:
|
|
|
|
"""Tests lots of fast things queued up behind a slow thing.
|
|
|
|
|
|
|
|
The stack should *not* explode when the slow thing completes.
|
|
|
|
"""
|
2017-10-11 10:05:05 -04:00
|
|
|
linearizer = Linearizer()
|
2022-04-05 09:56:09 -04:00
|
|
|
key = ""
|
2017-10-11 10:05:05 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
async def func(i: int) -> None:
|
2019-07-03 10:07:04 -04:00
|
|
|
with LoggingContext("func(%s)" % i) as lc:
|
2022-04-05 10:43:52 -04:00
|
|
|
async with linearizer.queue(key):
|
2020-03-24 10:45:33 -04:00
|
|
|
self.assertEqual(current_context(), lc)
|
2017-10-11 10:05:05 -04:00
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
self.assertEqual(current_context(), lc)
|
2017-10-11 10:05:05 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
_, _, unblock = self._start_task(linearizer, key)
|
2018-04-15 15:46:23 -04:00
|
|
|
for i in range(1, 100):
|
2022-04-05 09:56:09 -04:00
|
|
|
defer.ensureDeferred(func(i))
|
2017-10-11 10:05:05 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
d = defer.ensureDeferred(func(1000))
|
|
|
|
unblock()
|
|
|
|
self.successResultOf(d)
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
def test_multiple_entries(self) -> None:
|
|
|
|
"""Tests a `Linearizer` with a concurrency above 1."""
|
2018-07-20 08:11:43 -04:00
|
|
|
limiter = Linearizer(max_count=3)
|
|
|
|
|
|
|
|
key = object()
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
_, acquired_d1, unblock1 = self._start_task(limiter, key)
|
|
|
|
self.assertTrue(acquired_d1.called)
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
_, acquired_d2, unblock2 = self._start_task(limiter, key)
|
|
|
|
self.assertTrue(acquired_d2.called)
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
_, acquired_d3, unblock3 = self._start_task(limiter, key)
|
|
|
|
self.assertTrue(acquired_d3.called)
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# These next two tasks have to wait.
|
|
|
|
_, acquired_d4, unblock4 = self._start_task(limiter, key)
|
|
|
|
self.assertFalse(acquired_d4.called)
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
_, acquired_d5, unblock5 = self._start_task(limiter, key)
|
|
|
|
self.assertFalse(acquired_d5.called)
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Once the first task completes, the fourth task can continue.
|
|
|
|
unblock1()
|
|
|
|
self.assertTrue(acquired_d4.called)
|
|
|
|
self.assertFalse(acquired_d5.called)
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Once the third task completes, the fifth task can continue.
|
|
|
|
unblock3()
|
|
|
|
self.assertTrue(acquired_d5.called)
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Make all tasks finish.
|
|
|
|
unblock2()
|
|
|
|
unblock4()
|
|
|
|
unblock5()
|
2018-07-20 08:11:43 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# The next task shouldn't have to wait.
|
|
|
|
_, acquired_d6, unblock6 = self._start_task(limiter, key)
|
|
|
|
self.assertTrue(acquired_d6)
|
|
|
|
unblock6()
|
2018-07-20 08:59:55 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
def test_cancellation(self) -> None:
|
|
|
|
"""Tests cancellation while waiting for a `Linearizer`."""
|
2018-07-20 08:59:55 -04:00
|
|
|
linearizer = Linearizer()
|
|
|
|
|
|
|
|
key = object()
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
d1, acquired_d1, unblock1 = self._start_task(linearizer, key)
|
|
|
|
self.assertTrue(acquired_d1.called)
|
2018-07-20 08:59:55 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Create a second task, waiting for the first task.
|
|
|
|
d2, acquired_d2, _ = self._start_task(linearizer, key)
|
|
|
|
self.assertFalse(acquired_d2.called)
|
2018-07-20 08:59:55 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Create a third task, waiting for the second task.
|
|
|
|
d3, acquired_d3, unblock3 = self._start_task(linearizer, key)
|
|
|
|
self.assertFalse(acquired_d3.called)
|
2018-07-20 08:59:55 -04:00
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
# Cancel the waiting second task.
|
2018-07-20 08:59:55 -04:00
|
|
|
d2.cancel()
|
|
|
|
|
2022-04-05 09:56:09 -04:00
|
|
|
unblock1()
|
|
|
|
self.successResultOf(d1)
|
2018-07-20 08:59:55 -04:00
|
|
|
|
|
|
|
self.assertTrue(d2.called)
|
2022-04-05 09:56:09 -04:00
|
|
|
self.failureResultOf(d2, CancelledError)
|
|
|
|
|
|
|
|
# The third task should continue running.
|
|
|
|
self.assertTrue(
|
|
|
|
acquired_d3.called,
|
|
|
|
"Third task did not get the lock after the second task was cancelled",
|
|
|
|
)
|
|
|
|
unblock3()
|
|
|
|
self.successResultOf(d3)
|
2022-04-05 12:19:16 -04:00
|
|
|
|
|
|
|
def test_cancellation_during_sleep(self) -> None:
|
|
|
|
"""Tests cancellation during the sleep just after waiting for a `Linearizer`."""
|
|
|
|
linearizer = Linearizer()
|
|
|
|
|
|
|
|
key = object()
|
|
|
|
|
|
|
|
d1, acquired_d1, unblock1 = self._start_task(linearizer, key)
|
|
|
|
self.assertTrue(acquired_d1.called)
|
|
|
|
|
|
|
|
# Create a second task, waiting for the first task.
|
|
|
|
d2, acquired_d2, _ = self._start_task(linearizer, key)
|
|
|
|
self.assertFalse(acquired_d2.called)
|
|
|
|
|
|
|
|
# Create a third task, waiting for the second task.
|
|
|
|
d3, acquired_d3, unblock3 = self._start_task(linearizer, key)
|
|
|
|
self.assertFalse(acquired_d3.called)
|
|
|
|
|
|
|
|
# Once the first task completes, cancel the waiting second task while it is
|
|
|
|
# sleeping just after acquiring the lock.
|
|
|
|
unblock1(pump_reactor=False)
|
|
|
|
self.successResultOf(d1)
|
|
|
|
d2.cancel()
|
|
|
|
self._pump()
|
|
|
|
|
|
|
|
self.assertTrue(d2.called)
|
|
|
|
self.failureResultOf(d2, CancelledError)
|
|
|
|
|
|
|
|
# The third task should continue running.
|
|
|
|
self.assertTrue(
|
|
|
|
acquired_d3.called,
|
|
|
|
"Third task did not get the lock after the second task was cancelled",
|
|
|
|
)
|
|
|
|
unblock3()
|
|
|
|
self.successResultOf(d3)
|