test_distributor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 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("synapse.util.distributor.logger",
  52. 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("Go")
  58. observers[1].assert_called_once("Go")
  59. self.assertEquals(mock_logger.warning.call_count, 1)
  60. self.assertIsInstance(mock_logger.warning.call_args[0][0],
  61. str)
  62. @defer.inlineCallbacks
  63. def test_signal_catch_no_suppress(self):
  64. # Gut-wrenching
  65. self.dist.suppress_failures = False
  66. self.dist.declare("whail")
  67. class MyException(Exception):
  68. pass
  69. @defer.inlineCallbacks
  70. def observer():
  71. yield run_on_reactor()
  72. raise MyException("Oopsie")
  73. self.dist.observe("whail", observer)
  74. d = self.dist.fire("whail")
  75. yield self.assertFailure(d, MyException)
  76. self.dist.suppress_failures = True
  77. @defer.inlineCallbacks
  78. def test_signal_prereg(self):
  79. observer = Mock()
  80. self.dist.observe("flare", observer)
  81. self.dist.declare("flare")
  82. yield self.dist.fire("flare", 4, 5)
  83. observer.assert_called_with(4, 5)
  84. def test_signal_undeclared(self):
  85. def code():
  86. self.dist.fire("notification")
  87. self.assertRaises(KeyError, code)