2015-03-06 06:50:27 -05: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 2015, 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]
|
2015-03-06 06:50:27 -05:00
|
|
|
#
|
|
|
|
#
|
2023-02-17 13:19:38 -05:00
|
|
|
from typing import List, Optional, Sequence, Tuple, cast
|
2023-08-25 09:27:21 -04:00
|
|
|
from unittest.mock import AsyncMock, Mock
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
from typing_extensions import TypeAlias
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from twisted.internet import defer
|
2023-02-17 13:19:38 -05:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
from synapse.appservice import (
|
|
|
|
ApplicationService,
|
|
|
|
ApplicationServiceState,
|
|
|
|
TransactionOneTimeKeysCount,
|
|
|
|
TransactionUnusedFallbackKeys,
|
|
|
|
)
|
2015-03-06 06:50:27 -05:00
|
|
|
from synapse.appservice.scheduler import (
|
2022-02-01 09:13:38 -05:00
|
|
|
ApplicationServiceScheduler,
|
2018-07-09 02:09:20 -04:00
|
|
|
_Recoverer,
|
|
|
|
_TransactionController,
|
2015-03-06 06:50:27 -05:00
|
|
|
)
|
2023-02-06 07:49:06 -05:00
|
|
|
from synapse.events import EventBase
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import make_deferred_yieldable
|
2022-02-01 09:13:38 -05:00
|
|
|
from synapse.server import HomeServer
|
2023-02-06 07:49:06 -05:00
|
|
|
from synapse.types import DeviceListUpdates, JsonDict
|
2022-02-01 09:13:38 -05:00
|
|
|
from synapse.util import Clock
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2015-03-06 06:50:27 -05:00
|
|
|
from tests import unittest
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from ..utils import MockClock
|
|
|
|
|
2015-03-06 09:36:52 -05:00
|
|
|
|
2015-03-06 11:09:05 -05:00
|
|
|
class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
|
2023-02-06 07:49:06 -05:00
|
|
|
def setUp(self) -> None:
|
2015-03-06 11:09:05 -05:00
|
|
|
self.clock = MockClock()
|
|
|
|
self.store = Mock()
|
|
|
|
self.as_api = Mock()
|
|
|
|
self.recoverer = Mock()
|
|
|
|
self.recoverer_fn = Mock(return_value=self.recoverer)
|
|
|
|
self.txnctrl = _TransactionController(
|
2023-02-06 07:49:06 -05:00
|
|
|
clock=cast(Clock, self.clock), store=self.store, as_api=self.as_api
|
2015-03-06 11:09:05 -05:00
|
|
|
)
|
2019-08-20 06:20:10 -04:00
|
|
|
self.txnctrl.RECOVERER_CLASS = self.recoverer_fn
|
2015-03-06 11:09:05 -05:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_single_service_up_txn_sent(self) -> None:
|
2015-03-06 11:09:05 -05:00
|
|
|
# Test: The AS is up and the txn is successfully sent.
|
|
|
|
service = Mock()
|
|
|
|
events = [Mock(), Mock()]
|
|
|
|
txn_id = "foobar"
|
|
|
|
txn = Mock(id=txn_id, service=service, events=events)
|
|
|
|
|
|
|
|
# mock methods
|
2023-08-25 09:27:21 -04:00
|
|
|
self.store.get_appservice_state = AsyncMock(
|
|
|
|
return_value=ApplicationServiceState.UP
|
|
|
|
)
|
|
|
|
txn.send = AsyncMock(return_value=True)
|
|
|
|
txn.complete = AsyncMock(return_value=True)
|
|
|
|
self.store.create_appservice_txn = AsyncMock(return_value=txn)
|
2015-03-06 11:09:05 -05:00
|
|
|
|
|
|
|
# actual call
|
2020-07-30 07:27:39 -04:00
|
|
|
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
|
2015-03-06 11:09:05 -05:00
|
|
|
|
|
|
|
self.store.create_appservice_txn.assert_called_once_with(
|
2022-02-01 09:13:38 -05:00
|
|
|
service=service,
|
|
|
|
events=events,
|
|
|
|
ephemeral=[],
|
|
|
|
to_device_messages=[], # txn made and saved
|
2022-11-28 11:17:29 -05:00
|
|
|
one_time_keys_count={},
|
2022-02-24 12:55:45 -05:00
|
|
|
unused_fallback_keys={},
|
2022-03-30 09:39:27 -04:00
|
|
|
device_list_summary=DeviceListUpdates(),
|
2015-03-06 11:09:05 -05:00
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(0, len(self.txnctrl.recoverers)) # no recoverer made
|
2015-03-06 11:09:05 -05:00
|
|
|
txn.complete.assert_called_once_with(self.store) # txn completed
|
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_single_service_down(self) -> None:
|
2015-03-06 11:09:05 -05:00
|
|
|
# Test: The AS is down so it shouldn't push; Recoverers will do it.
|
|
|
|
# It should still make a transaction though.
|
|
|
|
service = Mock()
|
|
|
|
events = [Mock(), Mock()]
|
|
|
|
|
|
|
|
txn = Mock(id="idhere", service=service, events=events)
|
2023-08-25 09:27:21 -04:00
|
|
|
self.store.get_appservice_state = AsyncMock(
|
|
|
|
return_value=ApplicationServiceState.DOWN
|
2015-03-06 11:09:05 -05:00
|
|
|
)
|
2023-08-25 09:27:21 -04:00
|
|
|
self.store.create_appservice_txn = AsyncMock(return_value=txn)
|
2015-03-06 11:09:05 -05:00
|
|
|
|
|
|
|
# actual call
|
2020-07-30 07:27:39 -04:00
|
|
|
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
|
2015-03-06 11:09:05 -05:00
|
|
|
|
|
|
|
self.store.create_appservice_txn.assert_called_once_with(
|
2022-02-01 09:13:38 -05:00
|
|
|
service=service,
|
|
|
|
events=events,
|
|
|
|
ephemeral=[],
|
|
|
|
to_device_messages=[], # txn made and saved
|
2022-11-28 11:17:29 -05:00
|
|
|
one_time_keys_count={},
|
2022-02-24 12:55:45 -05:00
|
|
|
unused_fallback_keys={},
|
2022-03-30 09:39:27 -04:00
|
|
|
device_list_summary=DeviceListUpdates(),
|
2015-03-06 11:09:05 -05:00
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(0, txn.send.call_count) # txn not sent though
|
|
|
|
self.assertEqual(0, txn.complete.call_count) # or completed
|
2015-03-06 11:09:05 -05:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_single_service_up_txn_not_sent(self) -> None:
|
2015-03-06 11:09:05 -05:00
|
|
|
# Test: The AS is up and the txn is not sent. A Recoverer is made and
|
|
|
|
# started.
|
|
|
|
service = Mock()
|
|
|
|
events = [Mock(), Mock()]
|
|
|
|
txn_id = "foobar"
|
|
|
|
txn = Mock(id=txn_id, service=service, events=events)
|
|
|
|
|
|
|
|
# mock methods
|
2023-08-25 09:27:21 -04:00
|
|
|
self.store.get_appservice_state = AsyncMock(
|
|
|
|
return_value=ApplicationServiceState.UP
|
|
|
|
)
|
|
|
|
self.store.set_appservice_state = AsyncMock(return_value=True)
|
|
|
|
txn.send = AsyncMock(return_value=False) # fails to send
|
|
|
|
self.store.create_appservice_txn = AsyncMock(return_value=txn)
|
2015-03-06 11:09:05 -05:00
|
|
|
|
|
|
|
# actual call
|
2020-07-30 07:27:39 -04:00
|
|
|
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
|
2015-03-06 11:09:05 -05:00
|
|
|
|
|
|
|
self.store.create_appservice_txn.assert_called_once_with(
|
2022-02-24 12:55:45 -05:00
|
|
|
service=service,
|
|
|
|
events=events,
|
|
|
|
ephemeral=[],
|
|
|
|
to_device_messages=[],
|
2022-11-28 11:17:29 -05:00
|
|
|
one_time_keys_count={},
|
2022-02-24 12:55:45 -05:00
|
|
|
unused_fallback_keys={},
|
2022-03-30 09:39:27 -04:00
|
|
|
device_list_summary=DeviceListUpdates(),
|
2015-03-06 11:09:05 -05:00
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(1, self.recoverer_fn.call_count) # recoverer made
|
|
|
|
self.assertEqual(1, self.recoverer.recover.call_count) # and invoked
|
|
|
|
self.assertEqual(1, len(self.txnctrl.recoverers)) # and stored
|
|
|
|
self.assertEqual(0, txn.complete.call_count) # txn not completed
|
2015-03-06 11:09:05 -05:00
|
|
|
self.store.set_appservice_state.assert_called_once_with(
|
|
|
|
service, ApplicationServiceState.DOWN # service marked as down
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-03-06 06:50:27 -05:00
|
|
|
class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
|
2023-02-06 07:49:06 -05:00
|
|
|
def setUp(self) -> None:
|
2015-03-06 06:50:27 -05:00
|
|
|
self.clock = MockClock()
|
|
|
|
self.as_api = Mock()
|
|
|
|
self.store = Mock()
|
|
|
|
self.service = Mock()
|
2023-08-25 09:27:21 -04:00
|
|
|
self.callback = AsyncMock()
|
2015-03-06 06:50:27 -05:00
|
|
|
self.recoverer = _Recoverer(
|
2023-02-06 07:49:06 -05:00
|
|
|
clock=cast(Clock, self.clock),
|
2015-03-06 06:50:27 -05:00
|
|
|
as_api=self.as_api,
|
|
|
|
store=self.store,
|
|
|
|
service=self.service,
|
|
|
|
callback=self.callback,
|
|
|
|
)
|
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_recover_single_txn(self) -> None:
|
2015-03-06 09:36:52 -05:00
|
|
|
txn = Mock()
|
|
|
|
# return one txn to send, then no more old txns
|
|
|
|
txns = [txn, None]
|
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def take_txn(
|
|
|
|
*args: object, **kwargs: object
|
|
|
|
) -> "defer.Deferred[Optional[Mock]]":
|
2015-03-06 09:36:52 -05:00
|
|
|
return defer.succeed(txns.pop(0))
|
2018-08-10 09:54:09 -04:00
|
|
|
|
2015-03-06 11:17:01 -05:00
|
|
|
self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
|
2015-03-06 06:50:27 -05:00
|
|
|
|
|
|
|
self.recoverer.recover()
|
2015-03-06 09:36:52 -05:00
|
|
|
# shouldn't have called anything prior to waiting for exp backoff
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(0, self.store.get_oldest_unsent_txn.call_count)
|
2023-08-25 09:27:21 -04:00
|
|
|
txn.send = AsyncMock(return_value=True)
|
|
|
|
txn.complete = AsyncMock(return_value=None)
|
2015-03-06 09:36:52 -05:00
|
|
|
# wait for exp backoff
|
2015-03-09 13:01:19 -04:00
|
|
|
self.clock.advance_time(2)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(1, txn.send.call_count)
|
|
|
|
self.assertEqual(1, txn.complete.call_count)
|
2015-03-06 09:36:52 -05:00
|
|
|
# 2 because it needs to get None to know there are no more txns
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(2, self.store.get_oldest_unsent_txn.call_count)
|
2015-03-06 10:12:24 -05:00
|
|
|
self.callback.assert_called_once_with(self.recoverer)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(self.recoverer.service, self.service)
|
2015-03-06 06:50:27 -05:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_recover_retry_txn(self) -> None:
|
2015-03-06 09:36:52 -05:00
|
|
|
txn = Mock()
|
|
|
|
txns = [txn, None]
|
|
|
|
pop_txn = False
|
2015-03-06 06:50:27 -05:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def take_txn(
|
|
|
|
*args: object, **kwargs: object
|
|
|
|
) -> "defer.Deferred[Optional[Mock]]":
|
2015-03-06 09:36:52 -05:00
|
|
|
if pop_txn:
|
|
|
|
return defer.succeed(txns.pop(0))
|
|
|
|
else:
|
|
|
|
return defer.succeed(txn)
|
2018-08-10 09:54:09 -04:00
|
|
|
|
2015-03-06 11:17:01 -05:00
|
|
|
self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
|
2015-03-06 06:50:27 -05:00
|
|
|
|
2015-03-06 09:36:52 -05:00
|
|
|
self.recoverer.recover()
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(0, self.store.get_oldest_unsent_txn.call_count)
|
2023-08-25 09:27:21 -04:00
|
|
|
txn.send = AsyncMock(return_value=False)
|
|
|
|
txn.complete = AsyncMock(return_value=None)
|
2015-03-09 13:01:19 -04:00
|
|
|
self.clock.advance_time(2)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(1, txn.send.call_count)
|
|
|
|
self.assertEqual(0, txn.complete.call_count)
|
|
|
|
self.assertEqual(0, self.callback.call_count)
|
2015-03-09 13:01:19 -04:00
|
|
|
self.clock.advance_time(4)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(2, txn.send.call_count)
|
|
|
|
self.assertEqual(0, txn.complete.call_count)
|
|
|
|
self.assertEqual(0, self.callback.call_count)
|
2015-03-09 13:01:19 -04:00
|
|
|
self.clock.advance_time(8)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(3, txn.send.call_count)
|
|
|
|
self.assertEqual(0, txn.complete.call_count)
|
|
|
|
self.assertEqual(0, self.callback.call_count)
|
2023-08-25 09:27:21 -04:00
|
|
|
txn.send = AsyncMock(return_value=True) # successfully send the txn
|
2015-03-06 09:36:52 -05:00
|
|
|
pop_txn = True # returns the txn the first time, then no more.
|
2015-03-09 13:01:19 -04:00
|
|
|
self.clock.advance_time(16)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(1, txn.send.call_count) # new mock reset call count
|
|
|
|
self.assertEqual(1, txn.complete.call_count)
|
2015-03-06 10:12:24 -05:00
|
|
|
self.callback.assert_called_once_with(self.recoverer)
|
2015-03-06 06:50:27 -05:00
|
|
|
|
2015-03-06 11:09:05 -05:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
# Corresponds to synapse.appservice.scheduler._TransactionController.send
|
|
|
|
TxnCtrlArgs: TypeAlias = """
|
|
|
|
defer.Deferred[
|
|
|
|
Tuple[
|
|
|
|
ApplicationService,
|
|
|
|
Sequence[EventBase],
|
|
|
|
Optional[List[JsonDict]],
|
|
|
|
Optional[List[JsonDict]],
|
|
|
|
Optional[TransactionOneTimeKeysCount],
|
|
|
|
Optional[TransactionUnusedFallbackKeys],
|
|
|
|
Optional[DeviceListUpdates],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2022-02-01 09:13:38 -05:00
|
|
|
class ApplicationServiceSchedulerQueuerTestCase(unittest.HomeserverTestCase):
|
2023-02-06 07:49:06 -05:00
|
|
|
def prepare(self, reactor: "MemoryReactor", clock: Clock, hs: HomeServer) -> None:
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler = ApplicationServiceScheduler(hs)
|
2015-03-16 09:15:40 -04:00
|
|
|
self.txn_ctrl = Mock()
|
2023-08-25 09:27:21 -04:00
|
|
|
self.txn_ctrl.send = AsyncMock()
|
2022-02-01 09:13:38 -05:00
|
|
|
|
|
|
|
# Replace instantiated _TransactionController instances with our Mock
|
|
|
|
self.scheduler.txn_ctrl = self.txn_ctrl
|
|
|
|
self.scheduler.queuer.txn_ctrl = self.txn_ctrl
|
2015-03-16 09:15:40 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_send_single_event_no_queue(self) -> None:
|
2015-03-16 09:15:40 -04:00
|
|
|
# Expect the event to be sent immediately.
|
2015-03-16 10:03:16 -04:00
|
|
|
service = Mock(id=4)
|
|
|
|
event = Mock()
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, events=[event])
|
2022-03-30 09:39:27 -04:00
|
|
|
self.txn_ctrl.send.assert_called_once_with(
|
|
|
|
service, [event], [], [], None, None, DeviceListUpdates()
|
|
|
|
)
|
2015-03-16 09:15:40 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_send_single_event_with_queue(self) -> None:
|
|
|
|
d: TxnCtrlArgs = defer.Deferred()
|
2022-02-01 09:13:38 -05:00
|
|
|
self.txn_ctrl.send = Mock(return_value=make_deferred_yieldable(d))
|
2015-03-16 10:03:16 -04:00
|
|
|
service = Mock(id=4)
|
|
|
|
event = Mock(event_id="first")
|
|
|
|
event2 = Mock(event_id="second")
|
|
|
|
event3 = Mock(event_id="third")
|
|
|
|
# Send an event and don't resolve it just yet.
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, events=[event])
|
2015-03-16 10:03:16 -04:00
|
|
|
# Send more events: expect send() to NOT be called multiple times.
|
2022-02-01 09:13:38 -05:00
|
|
|
# (call enqueue_for_appservice multiple times deliberately)
|
|
|
|
self.scheduler.enqueue_for_appservice(service, events=[event2])
|
|
|
|
self.scheduler.enqueue_for_appservice(service, events=[event3])
|
2022-03-30 09:39:27 -04:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
|
|
|
service, [event], [], [], None, None, DeviceListUpdates()
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(1, self.txn_ctrl.send.call_count)
|
2015-03-16 10:03:16 -04:00
|
|
|
# Resolve the send event: expect the queued events to be sent
|
|
|
|
d.callback(service)
|
2022-02-24 12:55:45 -05:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
2022-03-30 09:39:27 -04:00
|
|
|
service, [event2, event3], [], [], None, None, DeviceListUpdates()
|
2022-02-24 12:55:45 -05:00
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(2, self.txn_ctrl.send.call_count)
|
2015-03-16 09:15:40 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_multiple_service_queues(self) -> None:
|
2015-03-16 09:15:40 -04:00
|
|
|
# Tests that each service has its own queue, and that they don't block
|
|
|
|
# on each other.
|
2015-03-16 10:03:16 -04:00
|
|
|
srv1 = Mock(id=4)
|
2023-02-06 07:49:06 -05:00
|
|
|
srv_1_defer: "defer.Deferred[EventBase]" = defer.Deferred()
|
2015-03-16 10:03:16 -04:00
|
|
|
srv_1_event = Mock(event_id="srv1a")
|
|
|
|
srv_1_event2 = Mock(event_id="srv1b")
|
|
|
|
|
|
|
|
srv2 = Mock(id=6)
|
2023-02-06 07:49:06 -05:00
|
|
|
srv_2_defer: "defer.Deferred[EventBase]" = defer.Deferred()
|
2015-03-16 10:03:16 -04:00
|
|
|
srv_2_event = Mock(event_id="srv2a")
|
|
|
|
srv_2_event2 = Mock(event_id="srv2b")
|
|
|
|
|
|
|
|
send_return_list = [srv_1_defer, srv_2_defer]
|
2018-05-02 10:46:22 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def do_send(*args: object, **kwargs: object) -> "defer.Deferred[EventBase]":
|
2018-05-02 10:46:22 -04:00
|
|
|
return make_deferred_yieldable(send_return_list.pop(0))
|
2018-08-10 09:54:09 -04:00
|
|
|
|
2018-05-02 10:46:22 -04:00
|
|
|
self.txn_ctrl.send = Mock(side_effect=do_send)
|
2015-03-16 10:03:16 -04:00
|
|
|
|
|
|
|
# send events for different ASes and make sure they are sent
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(srv1, events=[srv_1_event])
|
|
|
|
self.scheduler.enqueue_for_appservice(srv1, events=[srv_1_event2])
|
2022-03-30 09:39:27 -04:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
|
|
|
srv1, [srv_1_event], [], [], None, None, DeviceListUpdates()
|
|
|
|
)
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(srv2, events=[srv_2_event])
|
|
|
|
self.scheduler.enqueue_for_appservice(srv2, events=[srv_2_event2])
|
2022-03-30 09:39:27 -04:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
|
|
|
srv2, [srv_2_event], [], [], None, None, DeviceListUpdates()
|
|
|
|
)
|
2015-03-16 10:03:16 -04:00
|
|
|
|
|
|
|
# make sure callbacks for a service only send queued events for THAT
|
|
|
|
# service
|
|
|
|
srv_2_defer.callback(srv2)
|
2022-03-30 09:39:27 -04:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
|
|
|
srv2, [srv_2_event2], [], [], None, None, DeviceListUpdates()
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(3, self.txn_ctrl.send.call_count)
|
2020-10-15 12:33:28 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_send_large_txns(self) -> None:
|
|
|
|
srv_1_defer: "defer.Deferred[EventBase]" = defer.Deferred()
|
|
|
|
srv_2_defer: "defer.Deferred[EventBase]" = defer.Deferred()
|
2020-10-21 10:36:53 -04:00
|
|
|
send_return_list = [srv_1_defer, srv_2_defer]
|
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def do_send(*args: object, **kwargs: object) -> "defer.Deferred[EventBase]":
|
2020-10-21 10:36:53 -04:00
|
|
|
return make_deferred_yieldable(send_return_list.pop(0))
|
|
|
|
|
|
|
|
self.txn_ctrl.send = Mock(side_effect=do_send)
|
|
|
|
|
|
|
|
service = Mock(id=4, name="service")
|
|
|
|
event_list = [Mock(name="event%i" % (i + 1)) for i in range(200)]
|
|
|
|
for event in event_list:
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, [event], [])
|
2020-10-21 10:36:53 -04:00
|
|
|
|
|
|
|
# Expect the first event to be sent immediately.
|
2022-02-24 12:55:45 -05:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
2022-03-30 09:39:27 -04:00
|
|
|
service, [event_list[0]], [], [], None, None, DeviceListUpdates()
|
2022-02-24 12:55:45 -05:00
|
|
|
)
|
2020-10-21 10:36:53 -04:00
|
|
|
srv_1_defer.callback(service)
|
|
|
|
# Then send the next 100 events
|
2022-02-24 12:55:45 -05:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
2022-03-30 09:39:27 -04:00
|
|
|
service, event_list[1:101], [], [], None, None, DeviceListUpdates()
|
2022-02-24 12:55:45 -05:00
|
|
|
)
|
2020-10-21 10:36:53 -04:00
|
|
|
srv_2_defer.callback(service)
|
|
|
|
# Then the final 99 events
|
2022-02-24 12:55:45 -05:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
2022-03-30 09:39:27 -04:00
|
|
|
service, event_list[101:], [], [], None, None, DeviceListUpdates()
|
2022-02-24 12:55:45 -05:00
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(3, self.txn_ctrl.send.call_count)
|
2020-10-21 10:36:53 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_send_single_ephemeral_no_queue(self) -> None:
|
2020-10-15 12:33:28 -04:00
|
|
|
# Expect the event to be sent immediately.
|
|
|
|
service = Mock(id=4, name="service")
|
|
|
|
event_list = [Mock(name="event")]
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=event_list)
|
2022-02-24 12:55:45 -05:00
|
|
|
self.txn_ctrl.send.assert_called_once_with(
|
2022-03-30 09:39:27 -04:00
|
|
|
service, [], event_list, [], None, None, DeviceListUpdates()
|
2022-02-24 12:55:45 -05:00
|
|
|
)
|
2020-10-15 12:33:28 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_send_multiple_ephemeral_no_queue(self) -> None:
|
2020-10-15 12:33:28 -04:00
|
|
|
# Expect the event to be sent immediately.
|
|
|
|
service = Mock(id=4, name="service")
|
|
|
|
event_list = [Mock(name="event1"), Mock(name="event2"), Mock(name="event3")]
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=event_list)
|
2022-02-24 12:55:45 -05:00
|
|
|
self.txn_ctrl.send.assert_called_once_with(
|
2022-03-30 09:39:27 -04:00
|
|
|
service, [], event_list, [], None, None, DeviceListUpdates()
|
2022-02-24 12:55:45 -05:00
|
|
|
)
|
2020-10-15 12:33:28 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_send_single_ephemeral_with_queue(self) -> None:
|
|
|
|
d: TxnCtrlArgs = defer.Deferred()
|
2022-02-01 09:13:38 -05:00
|
|
|
self.txn_ctrl.send = Mock(return_value=make_deferred_yieldable(d))
|
2020-10-15 12:33:28 -04:00
|
|
|
service = Mock(id=4)
|
|
|
|
event_list_1 = [Mock(event_id="event1"), Mock(event_id="event2")]
|
|
|
|
event_list_2 = [Mock(event_id="event3"), Mock(event_id="event4")]
|
|
|
|
event_list_3 = [Mock(event_id="event5"), Mock(event_id="event6")]
|
|
|
|
|
|
|
|
# Send an event and don't resolve it just yet.
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=event_list_1)
|
2020-10-15 12:33:28 -04:00
|
|
|
# Send more events: expect send() to NOT be called multiple times.
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=event_list_2)
|
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=event_list_3)
|
2022-03-30 09:39:27 -04:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
|
|
|
service, [], event_list_1, [], None, None, DeviceListUpdates()
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(1, self.txn_ctrl.send.call_count)
|
2020-10-15 12:33:28 -04:00
|
|
|
# Resolve txn_ctrl.send
|
|
|
|
d.callback(service)
|
|
|
|
# Expect the queued events to be sent
|
2022-02-01 09:13:38 -05:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
2022-03-30 09:39:27 -04:00
|
|
|
service,
|
|
|
|
[],
|
|
|
|
event_list_2 + event_list_3,
|
|
|
|
[],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
DeviceListUpdates(),
|
2022-02-01 09:13:38 -05:00
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(2, self.txn_ctrl.send.call_count)
|
2020-10-21 10:36:53 -04:00
|
|
|
|
2023-02-06 07:49:06 -05:00
|
|
|
def test_send_large_txns_ephemeral(self) -> None:
|
|
|
|
d: TxnCtrlArgs = defer.Deferred()
|
2022-02-01 09:13:38 -05:00
|
|
|
self.txn_ctrl.send = Mock(return_value=make_deferred_yieldable(d))
|
2020-10-21 10:36:53 -04:00
|
|
|
# Expect the event to be sent immediately.
|
|
|
|
service = Mock(id=4, name="service")
|
|
|
|
first_chunk = [Mock(name="event%i" % (i + 1)) for i in range(100)]
|
|
|
|
second_chunk = [Mock(name="event%i" % (i + 101)) for i in range(50)]
|
|
|
|
event_list = first_chunk + second_chunk
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=event_list)
|
2022-02-24 12:55:45 -05:00
|
|
|
self.txn_ctrl.send.assert_called_once_with(
|
2022-03-30 09:39:27 -04:00
|
|
|
service, [], first_chunk, [], None, None, DeviceListUpdates()
|
2022-02-24 12:55:45 -05:00
|
|
|
)
|
2020-10-21 10:36:53 -04:00
|
|
|
d.callback(service)
|
2022-03-30 09:39:27 -04:00
|
|
|
self.txn_ctrl.send.assert_called_with(
|
|
|
|
service, [], second_chunk, [], None, None, DeviceListUpdates()
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(2, self.txn_ctrl.send.call_count)
|