test_distributor.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-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 . import unittest
  16. from twisted.internet import defer
  17. from mock import Mock, patch
  18. from synapse.util.distributor import Distributor
  19. from synapse.util.async import run_on_reactor
  20. class DistributorTestCase(unittest.TestCase):
  21. def setUp(self):
  22. self.dist = Distributor()
  23. @defer.inlineCallbacks
  24. def test_signal_dispatch(self):
  25. self.dist.declare("alert")
  26. observer = Mock()
  27. self.dist.observe("alert", observer)
  28. d = self.dist.fire("alert", 1, 2, 3)
  29. yield d
  30. self.assertTrue(d.called)
  31. observer.assert_called_with(1, 2, 3)
  32. @defer.inlineCallbacks
  33. def test_signal_dispatch_deferred(self):
  34. self.dist.declare("whine")
  35. d_inner = defer.Deferred()
  36. def observer():
  37. return d_inner
  38. self.dist.observe("whine", observer)
  39. d_outer = self.dist.fire("whine")
  40. self.assertFalse(d_outer.called)
  41. d_inner.callback(None)
  42. yield d_outer
  43. self.assertTrue(d_outer.called)
  44. @defer.inlineCallbacks
  45. def test_signal_catch(self):
  46. self.dist.declare("alarm")
  47. observers = [Mock() for i in 1, 2]
  48. for o in observers:
  49. self.dist.observe("alarm", o)
  50. observers[0].side_effect = Exception("Awoogah!")
  51. with patch(
  52. "synapse.util.distributor.logger", spec=["warning"]
  53. ) as mock_logger:
  54. d = self.dist.fire("alarm", "Go")
  55. yield d
  56. self.assertTrue(d.called)
  57. observers[0].assert_called_once_with("Go")
  58. observers[1].assert_called_once_with("Go")
  59. self.assertEquals(mock_logger.warning.call_count, 1)
  60. self.assertIsInstance(
  61. mock_logger.warning.call_args[0][0], str
  62. )
  63. @defer.inlineCallbacks
  64. def test_signal_catch_no_suppress(self):
  65. # Gut-wrenching
  66. self.dist.suppress_failures = False
  67. self.dist.declare("whail")
  68. class MyException(Exception):
  69. pass
  70. @defer.inlineCallbacks
  71. def observer():
  72. yield run_on_reactor()
  73. raise MyException("Oopsie")
  74. self.dist.observe("whail", observer)
  75. d = self.dist.fire("whail")
  76. yield self.assertFailure(d, MyException)
  77. self.dist.suppress_failures = True
  78. @defer.inlineCallbacks
  79. def test_signal_prereg(self):
  80. observer = Mock()
  81. self.dist.observe("flare", observer)
  82. self.dist.declare("flare")
  83. yield self.dist.fire("flare", 4, 5)
  84. observer.assert_called_with(4, 5)
  85. def test_signal_undeclared(self):
  86. def code():
  87. self.dist.fire("notification")
  88. self.assertRaises(KeyError, code)