1
0

test_scheduler.py 14 KB

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