test_scheduler.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 synapse.appservice import ApplicationServiceState
  16. from synapse.appservice.scheduler import (
  17. _ServiceQueuer, _TransactionController, _Recoverer
  18. )
  19. from twisted.internet import defer
  20. from synapse.util.logcontext import make_deferred_yieldable
  21. from ..utils import MockClock
  22. from mock import Mock
  23. from tests import unittest
  24. class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
  25. def setUp(self):
  26. self.clock = MockClock()
  27. self.store = Mock()
  28. self.as_api = Mock()
  29. self.recoverer = Mock()
  30. self.recoverer_fn = Mock(return_value=self.recoverer)
  31. self.txnctrl = _TransactionController(
  32. clock=self.clock, store=self.store, as_api=self.as_api,
  33. recoverer_fn=self.recoverer_fn
  34. )
  35. def test_single_service_up_txn_sent(self):
  36. # Test: The AS is up and the txn is successfully sent.
  37. service = Mock()
  38. events = [Mock(), Mock()]
  39. txn_id = "foobar"
  40. txn = Mock(id=txn_id, service=service, events=events)
  41. # mock methods
  42. self.store.get_appservice_state = Mock(
  43. return_value=defer.succeed(ApplicationServiceState.UP)
  44. )
  45. txn.send = Mock(return_value=defer.succeed(True))
  46. self.store.create_appservice_txn = Mock(
  47. return_value=defer.succeed(txn)
  48. )
  49. # actual call
  50. self.txnctrl.send(service, events)
  51. self.store.create_appservice_txn.assert_called_once_with(
  52. service=service, events=events # 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(
  66. return_value=defer.succeed(txn)
  67. )
  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(
  89. return_value=defer.succeed(txn)
  90. )
  91. # actual call
  92. self.txnctrl.send(service, events)
  93. self.store.create_appservice_txn.assert_called_once_with(
  94. service=service, events=events
  95. )
  96. self.assertEquals(1, self.recoverer_fn.call_count) # recoverer made
  97. self.assertEquals(1, self.recoverer.recover.call_count) # and invoked
  98. self.assertEquals(1, len(self.txnctrl.recoverers)) # and stored
  99. self.assertEquals(0, txn.complete.call_count) # txn not completed
  100. self.store.set_appservice_state.assert_called_once_with(
  101. service, ApplicationServiceState.DOWN # service marked as down
  102. )
  103. class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
  104. def setUp(self):
  105. self.clock = MockClock()
  106. self.as_api = Mock()
  107. self.store = Mock()
  108. self.service = Mock()
  109. self.callback = Mock()
  110. self.recoverer = _Recoverer(
  111. clock=self.clock,
  112. as_api=self.as_api,
  113. store=self.store,
  114. service=self.service,
  115. callback=self.callback,
  116. )
  117. def test_recover_single_txn(self):
  118. txn = Mock()
  119. # return one txn to send, then no more old txns
  120. txns = [txn, None]
  121. def take_txn(*args, **kwargs):
  122. return defer.succeed(txns.pop(0))
  123. self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
  124. self.recoverer.recover()
  125. # shouldn't have called anything prior to waiting for exp backoff
  126. self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
  127. txn.send = Mock(return_value=True)
  128. # wait for exp backoff
  129. self.clock.advance_time(2)
  130. self.assertEquals(1, txn.send.call_count)
  131. self.assertEquals(1, txn.complete.call_count)
  132. # 2 because it needs to get None to know there are no more txns
  133. self.assertEquals(2, self.store.get_oldest_unsent_txn.call_count)
  134. self.callback.assert_called_once_with(self.recoverer)
  135. self.assertEquals(self.recoverer.service, self.service)
  136. def test_recover_retry_txn(self):
  137. txn = Mock()
  138. txns = [txn, None]
  139. pop_txn = False
  140. def take_txn(*args, **kwargs):
  141. if pop_txn:
  142. return defer.succeed(txns.pop(0))
  143. else:
  144. return defer.succeed(txn)
  145. self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
  146. self.recoverer.recover()
  147. self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
  148. txn.send = Mock(return_value=False)
  149. self.clock.advance_time(2)
  150. self.assertEquals(1, txn.send.call_count)
  151. self.assertEquals(0, txn.complete.call_count)
  152. self.assertEquals(0, self.callback.call_count)
  153. self.clock.advance_time(4)
  154. self.assertEquals(2, 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(8)
  158. self.assertEquals(3, txn.send.call_count)
  159. self.assertEquals(0, txn.complete.call_count)
  160. self.assertEquals(0, self.callback.call_count)
  161. txn.send = Mock(return_value=True) # successfully send the txn
  162. pop_txn = True # returns the txn the first time, then no more.
  163. self.clock.advance_time(16)
  164. self.assertEquals(1, txn.send.call_count) # new mock reset call count
  165. self.assertEquals(1, txn.complete.call_count)
  166. self.callback.assert_called_once_with(self.recoverer)
  167. class ApplicationServiceSchedulerQueuerTestCase(unittest.TestCase):
  168. def setUp(self):
  169. self.txn_ctrl = Mock()
  170. self.queuer = _ServiceQueuer(self.txn_ctrl, MockClock())
  171. def test_send_single_event_no_queue(self):
  172. # Expect the event to be sent immediately.
  173. service = Mock(id=4)
  174. event = Mock()
  175. self.queuer.enqueue(service, event)
  176. self.txn_ctrl.send.assert_called_once_with(service, [event])
  177. def test_send_single_event_with_queue(self):
  178. d = defer.Deferred()
  179. self.txn_ctrl.send = Mock(
  180. side_effect=lambda x, y: make_deferred_yieldable(d),
  181. )
  182. service = Mock(id=4)
  183. event = Mock(event_id="first")
  184. event2 = Mock(event_id="second")
  185. event3 = Mock(event_id="third")
  186. # Send an event and don't resolve it just yet.
  187. self.queuer.enqueue(service, event)
  188. # Send more events: expect send() to NOT be called multiple times.
  189. self.queuer.enqueue(service, event2)
  190. self.queuer.enqueue(service, event3)
  191. self.txn_ctrl.send.assert_called_with(service, [event])
  192. self.assertEquals(1, self.txn_ctrl.send.call_count)
  193. # Resolve the send event: expect the queued events to be sent
  194. d.callback(service)
  195. self.txn_ctrl.send.assert_called_with(service, [event2, event3])
  196. self.assertEquals(2, self.txn_ctrl.send.call_count)
  197. def test_multiple_service_queues(self):
  198. # Tests that each service has its own queue, and that they don't block
  199. # on each other.
  200. srv1 = Mock(id=4)
  201. srv_1_defer = defer.Deferred()
  202. srv_1_event = Mock(event_id="srv1a")
  203. srv_1_event2 = Mock(event_id="srv1b")
  204. srv2 = Mock(id=6)
  205. srv_2_defer = defer.Deferred()
  206. srv_2_event = Mock(event_id="srv2a")
  207. srv_2_event2 = Mock(event_id="srv2b")
  208. send_return_list = [srv_1_defer, srv_2_defer]
  209. def do_send(x, y):
  210. return make_deferred_yieldable(send_return_list.pop(0))
  211. self.txn_ctrl.send = Mock(side_effect=do_send)
  212. # send events for different ASes and make sure they are sent
  213. self.queuer.enqueue(srv1, srv_1_event)
  214. self.queuer.enqueue(srv1, srv_1_event2)
  215. self.txn_ctrl.send.assert_called_with(srv1, [srv_1_event])
  216. self.queuer.enqueue(srv2, srv_2_event)
  217. self.queuer.enqueue(srv2, srv_2_event2)
  218. self.txn_ctrl.send.assert_called_with(srv2, [srv_2_event])
  219. # make sure callbacks for a service only send queued events for THAT
  220. # service
  221. srv_2_defer.callback(srv2)
  222. self.txn_ctrl.send.assert_called_with(srv2, [srv_2_event2])
  223. self.assertEquals(3, self.txn_ctrl.send.call_count)