mirror of
https://github.com/element-hq/synapse.git
synced 2026-09-13 21:55:45 +00:00
Refactor test to use clock
This commit is contained in:
@@ -21,14 +21,14 @@
|
||||
|
||||
from typing import Hashable, Protocol, Tuple
|
||||
|
||||
from twisted.internet import defer, reactor
|
||||
from twisted.internet.base import ReactorBase
|
||||
from twisted.internet import defer
|
||||
from twisted.internet.defer import CancelledError, Deferred
|
||||
|
||||
from synapse.logging.context import LoggingContext, current_context
|
||||
from synapse.util.async_helpers import Linearizer
|
||||
|
||||
from tests import unittest
|
||||
from tests.server import get_clock
|
||||
|
||||
|
||||
class UnblockFunction(Protocol):
|
||||
@@ -36,6 +36,9 @@ class UnblockFunction(Protocol):
|
||||
|
||||
|
||||
class LinearizerTestCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.reactor, self.clock = get_clock()
|
||||
|
||||
def _start_task(
|
||||
self, linearizer: Linearizer, key: Hashable
|
||||
) -> Tuple["Deferred[None]", "Deferred[None]", UnblockFunction]:
|
||||
@@ -67,19 +70,13 @@ class LinearizerTestCase(unittest.TestCase):
|
||||
# The next task, if it exists, will acquire the lock and require a kick of
|
||||
# the reactor to advance.
|
||||
if pump_reactor:
|
||||
self._pump()
|
||||
self.reactor.pump([0.0])
|
||||
|
||||
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."""
|
||||
linearizer = Linearizer()
|
||||
linearizer = Linearizer(clock=self.clock)
|
||||
|
||||
key = object()
|
||||
|
||||
@@ -100,7 +97,7 @@ class LinearizerTestCase(unittest.TestCase):
|
||||
|
||||
Runs through the same scenario as `test_linearizer`.
|
||||
"""
|
||||
linearizer = Linearizer()
|
||||
linearizer = Linearizer(clock=self.clock)
|
||||
|
||||
key = object()
|
||||
|
||||
@@ -131,7 +128,7 @@ class LinearizerTestCase(unittest.TestCase):
|
||||
|
||||
The stack should *not* explode when the slow thing completes.
|
||||
"""
|
||||
linearizer = Linearizer()
|
||||
linearizer = Linearizer(clock=self.clock)
|
||||
key = ""
|
||||
|
||||
async def func(i: int) -> None:
|
||||
@@ -151,7 +148,7 @@ class LinearizerTestCase(unittest.TestCase):
|
||||
|
||||
def test_multiple_entries(self) -> None:
|
||||
"""Tests a `Linearizer` with a concurrency above 1."""
|
||||
limiter = Linearizer(max_count=3)
|
||||
limiter = Linearizer(clock=self.clock, max_count=3)
|
||||
|
||||
key = object()
|
||||
|
||||
@@ -192,7 +189,7 @@ class LinearizerTestCase(unittest.TestCase):
|
||||
|
||||
def test_cancellation(self) -> None:
|
||||
"""Tests cancellation while waiting for a `Linearizer`."""
|
||||
linearizer = Linearizer()
|
||||
linearizer = Linearizer(clock=self.clock)
|
||||
|
||||
key = object()
|
||||
|
||||
@@ -226,7 +223,7 @@ class LinearizerTestCase(unittest.TestCase):
|
||||
|
||||
def test_cancellation_during_sleep(self) -> None:
|
||||
"""Tests cancellation during the sleep just after waiting for a `Linearizer`."""
|
||||
linearizer = Linearizer()
|
||||
linearizer = Linearizer(clock=self.clock)
|
||||
|
||||
key = object()
|
||||
|
||||
@@ -246,7 +243,7 @@ class LinearizerTestCase(unittest.TestCase):
|
||||
unblock1(pump_reactor=False)
|
||||
self.successResultOf(d1)
|
||||
d2.cancel()
|
||||
self._pump()
|
||||
self.reactor.pump([0.0])
|
||||
|
||||
self.assertTrue(d2.called)
|
||||
self.failureResultOf(d2, CancelledError)
|
||||
|
||||
Reference in New Issue
Block a user