-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Convert Linearizer tests from inlineCallbacks to async
#12353
Changes from 6 commits
e0c1444
071618d
45ce571
7842add
4cca457
5bfb04d
ba5839c
632bd38
0e0e25d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Convert `Linearizer` tests from `inlineCallbacks` to async. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,160 +13,205 @@ | |||||
| # See the License for the specific language governing permissions and | ||||||
| # limitations under the License. | ||||||
|
|
||||||
| from twisted.internet import defer, reactor | ||||||
| from twisted.internet.defer import CancelledError | ||||||
| from typing import Callable, Hashable, Optional, Tuple | ||||||
|
|
||||||
| from synapse.logging.context import LoggingContext, current_context | ||||||
| from synapse.util import Clock | ||||||
| from twisted.internet import defer, reactor | ||||||
| from twisted.internet.base import ReactorBase | ||||||
| from twisted.internet.defer import CancelledError, Deferred | ||||||
|
|
||||||
| from synapse.logging.context import ( | ||||||
| LoggingContext, | ||||||
| current_context, | ||||||
| make_deferred_yieldable, | ||||||
| ) | ||||||
| from synapse.util.async_helpers import Linearizer | ||||||
|
|
||||||
| from tests import unittest | ||||||
|
|
||||||
|
|
||||||
| class LinearizerTestCase(unittest.TestCase): | ||||||
| @defer.inlineCallbacks | ||||||
| def test_linearizer(self): | ||||||
| def _start_task( | ||||||
| self, linearizer: Linearizer, key: Hashable | ||||||
| ) -> Tuple["Deferred[None]", "Deferred[None]", Callable[[], None]]: | ||||||
| """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: | ||||||
| with await linearizer.queue(key): | ||||||
| acquired_d.callback(None) | ||||||
| await unblock_d | ||||||
|
|
||||||
| d = defer.ensureDeferred(task()) | ||||||
|
|
||||||
| def unblock() -> None: | ||||||
| unblock_d.callback(None) | ||||||
| # The next task, if it exists, will acquire the lock and require a kick of | ||||||
| # the reactor to advance. | ||||||
| self._pump() | ||||||
|
|
||||||
| 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() | ||||||
|
Comment on lines
+63
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||||||
|
|
||||||
| def test_linearizer(self) -> None: | ||||||
| """Tests that a task is queued up behind an earlier task.""" | ||||||
| linearizer = Linearizer() | ||||||
|
|
||||||
| key = object() | ||||||
|
|
||||||
| d1 = linearizer.queue(key) | ||||||
| cm1 = yield d1 | ||||||
| _, 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) | ||||||
|
|
||||||
| d2 = linearizer.queue(key) | ||||||
| self.assertFalse(d2.called) | ||||||
| # Once the first task is done, the second task can continue. | ||||||
| unblock1() | ||||||
| self.assertTrue(acquired_d2.called) | ||||||
|
|
||||||
| with cm1: | ||||||
| self.assertFalse(d2.called) | ||||||
| unblock2() | ||||||
|
|
||||||
| with (yield d2): | ||||||
| pass | ||||||
| def test_linearizer_is_queued(self) -> None: | ||||||
| """Tests `Linearizer.is_queued`. | ||||||
| @defer.inlineCallbacks | ||||||
| def test_linearizer_is_queued(self): | ||||||
| Runs through the same scenario as `test_linearizer`. | ||||||
| """ | ||||||
| linearizer = Linearizer() | ||||||
|
|
||||||
| key = object() | ||||||
|
|
||||||
| d1 = linearizer.queue(key) | ||||||
| cm1 = yield d1 | ||||||
| _, acquired_d1, unblock1 = self._start_task(linearizer, key) | ||||||
| self.assertTrue(acquired_d1.called) | ||||||
|
|
||||||
| # Since d1 gets called immediately, "is_queued" should return false. | ||||||
| # Since the first task acquires the lock immediately, "is_queued" should return | ||||||
| # false. | ||||||
| self.assertFalse(linearizer.is_queued(key)) | ||||||
|
|
||||||
| d2 = linearizer.queue(key) | ||||||
| self.assertFalse(d2.called) | ||||||
| _, acquired_d2, unblock2 = self._start_task(linearizer, key) | ||||||
| self.assertFalse(acquired_d2.called) | ||||||
|
|
||||||
| # Now d2 is queued up behind successful completion of cm1 | ||||||
| # Now the second task is queued up behind the first. | ||||||
| self.assertTrue(linearizer.is_queued(key)) | ||||||
|
|
||||||
| with cm1: | ||||||
| self.assertFalse(d2.called) | ||||||
| unblock1() | ||||||
|
|
||||||
| # cm1 still not done, so d2 still queued. | ||||||
| self.assertTrue(linearizer.is_queued(key)) | ||||||
|
|
||||||
| # And now d2 is called and nothing is in the queue again | ||||||
| # And now the second task acquires the lock and nothing is in the queue again. | ||||||
| self.assertTrue(acquired_d2.called) | ||||||
| self.assertFalse(linearizer.is_queued(key)) | ||||||
|
|
||||||
| with (yield d2): | ||||||
| self.assertFalse(linearizer.is_queued(key)) | ||||||
|
|
||||||
| unblock2() | ||||||
| self.assertFalse(linearizer.is_queued(key)) | ||||||
|
|
||||||
| def test_lots_of_queued_things(self): | ||||||
| # we have one slow thing, and lots of fast things queued up behind it. | ||||||
| # it should *not* explode the stack. | ||||||
| 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 fast thing completes. | ||||||
DMRobertson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| """ | ||||||
| linearizer = Linearizer() | ||||||
| key = "" | ||||||
|
|
||||||
| @defer.inlineCallbacks | ||||||
| def func(i, sleep=False): | ||||||
| async def func(i: int, wait_for: Optional["Deferred[None]"] = None) -> None: | ||||||
|
||||||
| async def func(i: int, wait_for: Optional["Deferred[None]"] = None) -> None: | |
| async def func(i: int) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's gone now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike the previous code, we enter the context manager immediately once
linearizer.queue()resolves.So
turns into
And the asserts that were once inside the context manager often become duplicates and are removed.