1
0

test_distributor.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2018 New Vector 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 unittest.mock import Mock, patch
  16. from synapse.util.distributor import Distributor
  17. from . import unittest
  18. class DistributorTestCase(unittest.TestCase):
  19. def setUp(self):
  20. self.dist = Distributor()
  21. def test_signal_dispatch(self):
  22. self.dist.declare("alert")
  23. observer = Mock()
  24. self.dist.observe("alert", observer)
  25. self.dist.fire("alert", 1, 2, 3)
  26. observer.assert_called_with(1, 2, 3)
  27. def test_signal_catch(self):
  28. self.dist.declare("alarm")
  29. observers = [Mock() for i in (1, 2)]
  30. for o in observers:
  31. self.dist.observe("alarm", o)
  32. observers[0].side_effect = Exception("Awoogah!")
  33. with patch("synapse.util.distributor.logger", spec=["warning"]) as mock_logger:
  34. self.dist.fire("alarm", "Go")
  35. observers[0].assert_called_once_with("Go")
  36. observers[1].assert_called_once_with("Go")
  37. self.assertEqual(mock_logger.warning.call_count, 1)
  38. self.assertIsInstance(mock_logger.warning.call_args[0][0], str)
  39. def test_signal_prereg(self):
  40. observer = Mock()
  41. self.dist.observe("flare", observer)
  42. self.dist.declare("flare")
  43. self.dist.fire("flare", 4, 5)
  44. observer.assert_called_with(4, 5)
  45. def test_signal_undeclared(self):
  46. def code():
  47. self.dist.fire("notification")
  48. self.assertRaises(KeyError, code)