test_scheduler.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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, store=self.store, as_api=self.as_api,
  35. recoverer_fn=self.recoverer_fn
  36. )
  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=defer.succeed(True))
  48. self.store.create_appservice_txn = Mock(
  49. return_value=defer.succeed(txn)
  50. )
  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(
  68. return_value=defer.succeed(txn)
  69. )
  70. # actual call
  71. self.txnctrl.send(service, events)
  72. self.store.create_appservice_txn.assert_called_once_with(
  73. service=service, events=events # txn made and saved
  74. )
  75. self.assertEquals(0, txn.send.call_count) # txn not sent though
  76. self.assertEquals(0, txn.complete.call_count) # or completed
  77. def test_single_service_up_txn_not_sent(self):
  78. # Test: The AS is up and the txn is not sent. A Recoverer is made and
  79. # started.
  80. service = Mock()
  81. events = [Mock(), Mock()]
  82. txn_id = "foobar"
  83. txn = Mock(id=txn_id, service=service, events=events)
  84. # mock methods
  85. self.store.get_appservice_state = Mock(
  86. return_value=defer.succeed(ApplicationServiceState.UP)
  87. )
  88. self.store.set_appservice_state = Mock(return_value=defer.succeed(True))
  89. txn.send = Mock(return_value=defer.succeed(False)) # fails to send
  90. self.store.create_appservice_txn = Mock(
  91. return_value=defer.succeed(txn)
  92. )
  93. # actual call
  94. self.txnctrl.send(service, events)
  95. self.store.create_appservice_txn.assert_called_once_with(
  96. service=service, events=events
  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 = 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 = Mock(return_value=True)
  130. # wait for exp backoff
  131. self.clock.advance_time(2)
  132. self.assertEquals(1, txn.send.call_count)
  133. self.assertEquals(1, txn.complete.call_count)
  134. # 2 because it needs to get None to know there are no more txns
  135. self.assertEquals(2, self.store.get_oldest_unsent_txn.call_count)
  136. self.callback.assert_called_once_with(self.recoverer)
  137. self.assertEquals(self.recoverer.service, self.service)
  138. def test_recover_retry_txn(self):
  139. txn = Mock()
  140. txns = [txn, None]
  141. pop_txn = False
  142. def take_txn(*args, **kwargs):
  143. if pop_txn:
  144. return defer.succeed(txns.pop(0))
  145. else:
  146. return defer.succeed(txn)
  147. self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
  148. self.recoverer.recover()
  149. self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
  150. txn.send = Mock(return_value=False)
  151. self.clock.advance_time(2)
  152. self.assertEquals(1, 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(4)
  156. self.assertEquals(2, txn.send.call_count)
  157. self.assertEquals(0, txn.complete.call_count)
  158. self.assertEquals(0, self.callback.call_count)
  159. self.clock.advance_time(8)
  160. self.assertEquals(3, txn.send.call_count)
  161. self.assertEquals(0, txn.complete.call_count)
  162. self.assertEquals(0, self.callback.call_count)
  163. txn.send = Mock(return_value=True) # successfully send the txn
  164. pop_txn = True # returns the txn the first time, then no more.
  165. self.clock.advance_time(16)
  166. self.assertEquals(1, txn.send.call_count) # new mock reset call count
  167. self.assertEquals(1, txn.complete.call_count)
  168. self.callback.assert_called_once_with(self.recoverer)
  169. class ApplicationServiceSchedulerQueuerTestCase(unittest.TestCase):
  170. def setUp(self):
  171. self.txn_ctrl = Mock()
  172. self.queuer = _ServiceQueuer(self.txn_ctrl, MockClock())
  173. def test_send_single_event_no_queue(self):
  174. # Expect the event to be sent immediately.
  175. service = Mock(id=4)
  176. event = Mock()
  177. self.queuer.enqueue(service, event)
  178. self.txn_ctrl.send.assert_called_once_with(service, [event])
  179. def test_send_single_event_with_queue(self):
  180. d = defer.Deferred()
  181. self.txn_ctrl.send = Mock(
  182. side_effect=lambda x, y: make_deferred_yieldable(d),
  183. )
  184. service = Mock(id=4)
  185. event = Mock(event_id="first")
  186. event2 = Mock(event_id="second")
  187. event3 = Mock(event_id="third")
  188. # Send an event and don't resolve it just yet.
  189. self.queuer.enqueue(service, event)
  190. # Send more events: expect send() to NOT be called multiple times.
  191. self.queuer.enqueue(service, event2)
  192. self.queuer.enqueue(service, event3)
  193. self.txn_ctrl.send.assert_called_with(service, [event])
  194. self.assertEquals(1, self.txn_ctrl.send.call_count)
  195. # Resolve the send event: expect the queued events to be sent
  196. d.callback(service)
  197. self.txn_ctrl.send.assert_called_with(service, [event2, event3])
  198. self.assertEquals(2, self.txn_ctrl.send.call_count)
  199. def test_multiple_service_queues(self):
  200. # Tests that each service has its own queue, and that they don't block
  201. # on each other.
  202. srv1 = Mock(id=4)
  203. srv_1_defer = defer.Deferred()
  204. srv_1_event = Mock(event_id="srv1a")
  205. srv_1_event2 = Mock(event_id="srv1b")
  206. srv2 = Mock(id=6)
  207. srv_2_defer = defer.Deferred()
  208. srv_2_event = Mock(event_id="srv2a")
  209. srv_2_event2 = Mock(event_id="srv2b")
  210. send_return_list = [srv_1_defer, srv_2_defer]
  211. def do_send(x, y):
  212. return make_deferred_yieldable(send_return_list.pop(0))
  213. self.txn_ctrl.send = Mock(side_effect=do_send)
  214. # send events for different ASes and make sure they are sent
  215. self.queuer.enqueue(srv1, srv_1_event)
  216. self.queuer.enqueue(srv1, srv_1_event2)
  217. self.txn_ctrl.send.assert_called_with(srv1, [srv_1_event])
  218. self.queuer.enqueue(srv2, srv_2_event)
  219. self.queuer.enqueue(srv2, srv_2_event2)
  220. self.txn_ctrl.send.assert_called_with(srv2, [srv_2_event])
  221. # make sure callbacks for a service only send queued events for THAT
  222. # service
  223. srv_2_defer.callback(srv2)
  224. self.txn_ctrl.send.assert_called_with(srv2, [srv_2_event2])
  225. self.assertEquals(3, self.txn_ctrl.send.call_count)