test_scheduler.py 14 KB

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