test_scheduler.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from unittest.mock import Mock
  15. from twisted.internet import defer
  16. from twisted.internet.testing import MemoryReactor
  17. from synapse.appservice import ApplicationServiceState
  18. from synapse.appservice.scheduler import (
  19. ApplicationServiceScheduler,
  20. _Recoverer,
  21. _TransactionController,
  22. )
  23. from synapse.logging.context import make_deferred_yieldable
  24. from synapse.server import HomeServer
  25. from synapse.util import Clock
  26. from tests import unittest
  27. from tests.test_utils import simple_async_mock
  28. from ..utils import MockClock
  29. class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
  30. def setUp(self):
  31. self.clock = MockClock()
  32. self.store = Mock()
  33. self.as_api = Mock()
  34. self.recoverer = Mock()
  35. self.recoverer_fn = Mock(return_value=self.recoverer)
  36. self.txnctrl = _TransactionController(
  37. clock=self.clock, store=self.store, as_api=self.as_api
  38. )
  39. self.txnctrl.RECOVERER_CLASS = self.recoverer_fn
  40. def test_single_service_up_txn_sent(self):
  41. # Test: The AS is up and the txn is successfully sent.
  42. service = Mock()
  43. events = [Mock(), Mock()]
  44. txn_id = "foobar"
  45. txn = Mock(id=txn_id, service=service, events=events)
  46. # mock methods
  47. self.store.get_appservice_state = simple_async_mock(ApplicationServiceState.UP)
  48. txn.send = simple_async_mock(True)
  49. txn.complete = simple_async_mock(True)
  50. self.store.create_appservice_txn = simple_async_mock(txn)
  51. # actual call
  52. self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
  53. self.store.create_appservice_txn.assert_called_once_with(
  54. service=service,
  55. events=events,
  56. ephemeral=[],
  57. to_device_messages=[], # txn made and saved
  58. )
  59. self.assertEquals(0, len(self.txnctrl.recoverers)) # no recoverer made
  60. txn.complete.assert_called_once_with(self.store) # txn completed
  61. def test_single_service_down(self):
  62. # Test: The AS is down so it shouldn't push; Recoverers will do it.
  63. # It should still make a transaction though.
  64. service = Mock()
  65. events = [Mock(), Mock()]
  66. txn = Mock(id="idhere", service=service, events=events)
  67. self.store.get_appservice_state = simple_async_mock(
  68. ApplicationServiceState.DOWN
  69. )
  70. self.store.create_appservice_txn = simple_async_mock(txn)
  71. # actual call
  72. self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
  73. self.store.create_appservice_txn.assert_called_once_with(
  74. service=service,
  75. events=events,
  76. ephemeral=[],
  77. to_device_messages=[], # txn made and saved
  78. )
  79. self.assertEquals(0, txn.send.call_count) # txn not sent though
  80. self.assertEquals(0, txn.complete.call_count) # or completed
  81. def test_single_service_up_txn_not_sent(self):
  82. # Test: The AS is up and the txn is not sent. A Recoverer is made and
  83. # started.
  84. service = Mock()
  85. events = [Mock(), Mock()]
  86. txn_id = "foobar"
  87. txn = Mock(id=txn_id, service=service, events=events)
  88. # mock methods
  89. self.store.get_appservice_state = simple_async_mock(ApplicationServiceState.UP)
  90. self.store.set_appservice_state = simple_async_mock(True)
  91. txn.send = simple_async_mock(False) # fails to send
  92. self.store.create_appservice_txn = simple_async_mock(txn)
  93. # actual call
  94. self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
  95. self.store.create_appservice_txn.assert_called_once_with(
  96. service=service, events=events, ephemeral=[], to_device_messages=[]
  97. )
  98. self.assertEquals(1, self.recoverer_fn.call_count) # recoverer made
  99. self.assertEquals(1, self.recoverer.recover.call_count) # and invoked
  100. self.assertEquals(1, len(self.txnctrl.recoverers)) # and stored
  101. self.assertEquals(0, txn.complete.call_count) # txn not completed
  102. self.store.set_appservice_state.assert_called_once_with(
  103. service, ApplicationServiceState.DOWN # service marked as down
  104. )
  105. class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
  106. def setUp(self):
  107. self.clock = MockClock()
  108. self.as_api = Mock()
  109. self.store = Mock()
  110. self.service = Mock()
  111. self.callback = simple_async_mock()
  112. self.recoverer = _Recoverer(
  113. clock=self.clock,
  114. as_api=self.as_api,
  115. store=self.store,
  116. service=self.service,
  117. callback=self.callback,
  118. )
  119. def test_recover_single_txn(self):
  120. txn = Mock()
  121. # return one txn to send, then no more old txns
  122. txns = [txn, None]
  123. def take_txn(*args, **kwargs):
  124. return defer.succeed(txns.pop(0))
  125. self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
  126. self.recoverer.recover()
  127. # shouldn't have called anything prior to waiting for exp backoff
  128. self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
  129. txn.send = simple_async_mock(True)
  130. txn.complete = simple_async_mock(None)
  131. # wait for exp backoff
  132. self.clock.advance_time(2)
  133. self.assertEquals(1, txn.send.call_count)
  134. self.assertEquals(1, txn.complete.call_count)
  135. # 2 because it needs to get None to know there are no more txns
  136. self.assertEquals(2, self.store.get_oldest_unsent_txn.call_count)
  137. self.callback.assert_called_once_with(self.recoverer)
  138. self.assertEquals(self.recoverer.service, self.service)
  139. def test_recover_retry_txn(self):
  140. txn = Mock()
  141. txns = [txn, None]
  142. pop_txn = False
  143. def take_txn(*args, **kwargs):
  144. if pop_txn:
  145. return defer.succeed(txns.pop(0))
  146. else:
  147. return defer.succeed(txn)
  148. self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
  149. self.recoverer.recover()
  150. self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
  151. txn.send = simple_async_mock(False)
  152. txn.complete = simple_async_mock(None)
  153. self.clock.advance_time(2)
  154. self.assertEquals(1, txn.send.call_count)
  155. self.assertEquals(0, txn.complete.call_count)
  156. self.assertEquals(0, self.callback.call_count)
  157. self.clock.advance_time(4)
  158. self.assertEquals(2, txn.send.call_count)
  159. self.assertEquals(0, txn.complete.call_count)
  160. self.assertEquals(0, self.callback.call_count)
  161. self.clock.advance_time(8)
  162. self.assertEquals(3, txn.send.call_count)
  163. self.assertEquals(0, txn.complete.call_count)
  164. self.assertEquals(0, self.callback.call_count)
  165. txn.send = simple_async_mock(True) # successfully send the txn
  166. pop_txn = True # returns the txn the first time, then no more.
  167. self.clock.advance_time(16)
  168. self.assertEquals(1, txn.send.call_count) # new mock reset call count
  169. self.assertEquals(1, txn.complete.call_count)
  170. self.callback.assert_called_once_with(self.recoverer)
  171. class ApplicationServiceSchedulerQueuerTestCase(unittest.HomeserverTestCase):
  172. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
  173. self.scheduler = ApplicationServiceScheduler(hs)
  174. self.txn_ctrl = Mock()
  175. self.txn_ctrl.send = simple_async_mock()
  176. # Replace instantiated _TransactionController instances with our Mock
  177. self.scheduler.txn_ctrl = self.txn_ctrl
  178. self.scheduler.queuer.txn_ctrl = self.txn_ctrl
  179. def test_send_single_event_no_queue(self):
  180. # Expect the event to be sent immediately.
  181. service = Mock(id=4)
  182. event = Mock()
  183. self.scheduler.enqueue_for_appservice(service, events=[event])
  184. self.txn_ctrl.send.assert_called_once_with(service, [event])
  185. def test_send_single_event_with_queue(self):
  186. d = defer.Deferred()
  187. self.txn_ctrl.send = Mock(return_value=make_deferred_yieldable(d))
  188. service = Mock(id=4)
  189. event = Mock(event_id="first")
  190. event2 = Mock(event_id="second")
  191. event3 = Mock(event_id="third")
  192. # Send an event and don't resolve it just yet.
  193. self.scheduler.enqueue_for_appservice(service, events=[event])
  194. # Send more events: expect send() to NOT be called multiple times.
  195. # (call enqueue_for_appservice multiple times deliberately)
  196. self.scheduler.enqueue_for_appservice(service, events=[event2])
  197. self.scheduler.enqueue_for_appservice(service, events=[event3])
  198. self.txn_ctrl.send.assert_called_with(service, [event])
  199. self.assertEquals(1, self.txn_ctrl.send.call_count)
  200. # Resolve the send event: expect the queued events to be sent
  201. d.callback(service)
  202. self.txn_ctrl.send.assert_called_with(service, [event2, event3])
  203. self.assertEquals(2, self.txn_ctrl.send.call_count)
  204. def test_multiple_service_queues(self):
  205. # Tests that each service has its own queue, and that they don't block
  206. # on each other.
  207. srv1 = Mock(id=4)
  208. srv_1_defer = defer.Deferred()
  209. srv_1_event = Mock(event_id="srv1a")
  210. srv_1_event2 = Mock(event_id="srv1b")
  211. srv2 = Mock(id=6)
  212. srv_2_defer = defer.Deferred()
  213. srv_2_event = Mock(event_id="srv2a")
  214. srv_2_event2 = Mock(event_id="srv2b")
  215. send_return_list = [srv_1_defer, srv_2_defer]
  216. def do_send(*args, **kwargs):
  217. return make_deferred_yieldable(send_return_list.pop(0))
  218. self.txn_ctrl.send = Mock(side_effect=do_send)
  219. # send events for different ASes and make sure they are sent
  220. self.scheduler.enqueue_for_appservice(srv1, events=[srv_1_event])
  221. self.scheduler.enqueue_for_appservice(srv1, events=[srv_1_event2])
  222. self.txn_ctrl.send.assert_called_with(srv1, [srv_1_event])
  223. self.scheduler.enqueue_for_appservice(srv2, events=[srv_2_event])
  224. self.scheduler.enqueue_for_appservice(srv2, events=[srv_2_event2])
  225. self.txn_ctrl.send.assert_called_with(srv2, [srv_2_event])
  226. # make sure callbacks for a service only send queued events for THAT
  227. # service
  228. srv_2_defer.callback(srv2)
  229. self.txn_ctrl.send.assert_called_with(srv2, [srv_2_event2])
  230. self.assertEquals(3, self.txn_ctrl.send.call_count)
  231. def test_send_large_txns(self):
  232. srv_1_defer = defer.Deferred()
  233. srv_2_defer = defer.Deferred()
  234. send_return_list = [srv_1_defer, srv_2_defer]
  235. def do_send(*args, **kwargs):
  236. return make_deferred_yieldable(send_return_list.pop(0))
  237. self.txn_ctrl.send = Mock(side_effect=do_send)
  238. service = Mock(id=4, name="service")
  239. event_list = [Mock(name="event%i" % (i + 1)) for i in range(200)]
  240. for event in event_list:
  241. self.scheduler.enqueue_for_appservice(service, events=[event])
  242. # Expect the first event to be sent immediately.
  243. self.txn_ctrl.send.assert_called_with(service, [event_list[0]])
  244. srv_1_defer.callback(service)
  245. # Then send the next 100 events
  246. self.txn_ctrl.send.assert_called_with(service, event_list[1:101])
  247. srv_2_defer.callback(service)
  248. # Then the final 99 events
  249. self.txn_ctrl.send.assert_called_with(service, event_list[101:])
  250. self.assertEquals(3, self.txn_ctrl.send.call_count)
  251. def test_send_single_ephemeral_no_queue(self):
  252. # Expect the event to be sent immediately.
  253. service = Mock(id=4, name="service")
  254. event_list = [Mock(name="event")]
  255. self.scheduler.enqueue_for_appservice(service, ephemeral=event_list)
  256. self.txn_ctrl.send.assert_called_once_with(service, [], ephemeral=event_list)
  257. def test_send_multiple_ephemeral_no_queue(self):
  258. # Expect the event to be sent immediately.
  259. service = Mock(id=4, name="service")
  260. event_list = [Mock(name="event1"), Mock(name="event2"), Mock(name="event3")]
  261. self.scheduler.enqueue_for_appservice(service, ephemeral=event_list)
  262. self.txn_ctrl.send.assert_called_once_with(service, [], ephemeral=event_list)
  263. def test_send_single_ephemeral_with_queue(self):
  264. d = defer.Deferred()
  265. self.txn_ctrl.send = Mock(return_value=make_deferred_yieldable(d))
  266. service = Mock(id=4)
  267. event_list_1 = [Mock(event_id="event1"), Mock(event_id="event2")]
  268. event_list_2 = [Mock(event_id="event3"), Mock(event_id="event4")]
  269. event_list_3 = [Mock(event_id="event5"), Mock(event_id="event6")]
  270. # Send an event and don't resolve it just yet.
  271. self.scheduler.enqueue_for_appservice(service, ephemeral=event_list_1)
  272. # Send more events: expect send() to NOT be called multiple times.
  273. self.scheduler.enqueue_for_appservice(service, ephemeral=event_list_2)
  274. self.scheduler.enqueue_for_appservice(service, ephemeral=event_list_3)
  275. self.txn_ctrl.send.assert_called_with(service, [], ephemeral=event_list_1)
  276. self.assertEquals(1, self.txn_ctrl.send.call_count)
  277. # Resolve txn_ctrl.send
  278. d.callback(service)
  279. # Expect the queued events to be sent
  280. self.txn_ctrl.send.assert_called_with(
  281. service, [], ephemeral=event_list_2 + event_list_3
  282. )
  283. self.assertEquals(2, self.txn_ctrl.send.call_count)
  284. def test_send_large_txns_ephemeral(self):
  285. d = defer.Deferred()
  286. self.txn_ctrl.send = Mock(return_value=make_deferred_yieldable(d))
  287. # Expect the event to be sent immediately.
  288. service = Mock(id=4, name="service")
  289. first_chunk = [Mock(name="event%i" % (i + 1)) for i in range(100)]
  290. second_chunk = [Mock(name="event%i" % (i + 101)) for i in range(50)]
  291. event_list = first_chunk + second_chunk
  292. self.scheduler.enqueue_for_appservice(service, ephemeral=event_list)
  293. self.txn_ctrl.send.assert_called_once_with(service, [], ephemeral=first_chunk)
  294. d.callback(service)
  295. self.txn_ctrl.send.assert_called_with(service, [], ephemeral=second_chunk)
  296. self.assertEquals(2, self.txn_ctrl.send.call_count)