test_scheduler.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from mock import Mock
  16. from twisted.internet import defer
  17. from synapse.appservice import ApplicationServiceState
  18. from synapse.appservice.scheduler import (
  19. _Recoverer,
  20. _ServiceQueuer,
  21. _TransactionController,
  22. )
  23. from synapse.util.logcontext import make_deferred_yieldable
  24. from tests import unittest
  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,
  35. store=self.store,
  36. as_api=self.as_api,
  37. recoverer_fn=self.recoverer_fn,
  38. )
  39. def test_single_service_up_txn_sent(self):
  40. # Test: The AS is up and the txn is successfully sent.
  41. service = Mock()
  42. events = [Mock(), Mock()]
  43. txn_id = "foobar"
  44. txn = Mock(id=txn_id, service=service, events=events)
  45. # mock methods
  46. self.store.get_appservice_state = Mock(
  47. return_value=defer.succeed(ApplicationServiceState.UP)
  48. )
  49. txn.send = Mock(return_value=defer.succeed(True))
  50. self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn))
  51. # actual call
  52. self.txnctrl.send(service, events)
  53. self.store.create_appservice_txn.assert_called_once_with(
  54. service=service, events=events # txn made and saved
  55. )
  56. self.assertEquals(0, len(self.txnctrl.recoverers)) # no recoverer made
  57. txn.complete.assert_called_once_with(self.store) # txn completed
  58. def test_single_service_down(self):
  59. # Test: The AS is down so it shouldn't push; Recoverers will do it.
  60. # It should still make a transaction though.
  61. service = Mock()
  62. events = [Mock(), Mock()]
  63. txn = Mock(id="idhere", service=service, events=events)
  64. self.store.get_appservice_state = Mock(
  65. return_value=defer.succeed(ApplicationServiceState.DOWN)
  66. )
  67. self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn))
  68. # actual call
  69. self.txnctrl.send(service, events)
  70. self.store.create_appservice_txn.assert_called_once_with(
  71. service=service, events=events # txn made and saved
  72. )
  73. self.assertEquals(0, txn.send.call_count) # txn not sent though
  74. self.assertEquals(0, txn.complete.call_count) # or completed
  75. def test_single_service_up_txn_not_sent(self):
  76. # Test: The AS is up and the txn is not sent. A Recoverer is made and
  77. # started.
  78. service = Mock()
  79. events = [Mock(), Mock()]
  80. txn_id = "foobar"
  81. txn = Mock(id=txn_id, service=service, events=events)
  82. # mock methods
  83. self.store.get_appservice_state = Mock(
  84. return_value=defer.succeed(ApplicationServiceState.UP)
  85. )
  86. self.store.set_appservice_state = Mock(return_value=defer.succeed(True))
  87. txn.send = Mock(return_value=defer.succeed(False)) # fails to send
  88. self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn))
  89. # actual call
  90. self.txnctrl.send(service, events)
  91. self.store.create_appservice_txn.assert_called_once_with(
  92. service=service, events=events
  93. )
  94. self.assertEquals(1, self.recoverer_fn.call_count) # recoverer made
  95. self.assertEquals(1, self.recoverer.recover.call_count) # and invoked
  96. self.assertEquals(1, len(self.txnctrl.recoverers)) # and stored
  97. self.assertEquals(0, txn.complete.call_count) # txn not completed
  98. self.store.set_appservice_state.assert_called_once_with(
  99. service, ApplicationServiceState.DOWN # service marked as down
  100. )
  101. class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
  102. def setUp(self):
  103. self.clock = MockClock()
  104. self.as_api = Mock()
  105. self.store = Mock()
  106. self.service = Mock()
  107. self.callback = Mock()
  108. self.recoverer = _Recoverer(
  109. clock=self.clock,
  110. as_api=self.as_api,
  111. store=self.store,
  112. service=self.service,
  113. callback=self.callback,
  114. )
  115. def test_recover_single_txn(self):
  116. txn = Mock()
  117. # return one txn to send, then no more old txns
  118. txns = [txn, None]
  119. def take_txn(*args, **kwargs):
  120. return defer.succeed(txns.pop(0))
  121. self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
  122. self.recoverer.recover()
  123. # shouldn't have called anything prior to waiting for exp backoff
  124. self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
  125. txn.send = Mock(return_value=True)
  126. # wait for exp backoff
  127. self.clock.advance_time(2)
  128. self.assertEquals(1, txn.send.call_count)
  129. self.assertEquals(1, txn.complete.call_count)
  130. # 2 because it needs to get None to know there are no more txns
  131. self.assertEquals(2, self.store.get_oldest_unsent_txn.call_count)
  132. self.callback.assert_called_once_with(self.recoverer)
  133. self.assertEquals(self.recoverer.service, self.service)
  134. def test_recover_retry_txn(self):
  135. txn = Mock()
  136. txns = [txn, None]
  137. pop_txn = False
  138. def take_txn(*args, **kwargs):
  139. if pop_txn:
  140. return defer.succeed(txns.pop(0))
  141. else:
  142. return defer.succeed(txn)
  143. self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
  144. self.recoverer.recover()
  145. self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
  146. txn.send = Mock(return_value=False)
  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=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(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(side_effect=lambda x, y: make_deferred_yieldable(d))
  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(service, event)
  184. # Send more events: expect send() to NOT be called multiple times.
  185. self.queuer.enqueue(service, event2)
  186. self.queuer.enqueue(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):
  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(srv1, srv_1_event)
  210. self.queuer.enqueue(srv1, srv_1_event2)
  211. self.txn_ctrl.send.assert_called_with(srv1, [srv_1_event])
  212. self.queuer.enqueue(srv2, srv_2_event)
  213. self.queuer.enqueue(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)