test_distributor.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from mock import Mock, patch
  17. from synapse.util.distributor import Distributor
  18. from . import unittest
  19. class DistributorTestCase(unittest.TestCase):
  20. def setUp(self):
  21. self.dist = Distributor()
  22. def test_signal_dispatch(self):
  23. self.dist.declare("alert")
  24. observer = Mock()
  25. self.dist.observe("alert", observer)
  26. self.dist.fire("alert", 1, 2, 3)
  27. observer.assert_called_with(1, 2, 3)
  28. def test_signal_catch(self):
  29. self.dist.declare("alarm")
  30. observers = [Mock() for i in (1, 2)]
  31. for o in observers:
  32. self.dist.observe("alarm", o)
  33. observers[0].side_effect = Exception("Awoogah!")
  34. with patch("synapse.util.distributor.logger", spec=["warning"]) as mock_logger:
  35. self.dist.fire("alarm", "Go")
  36. observers[0].assert_called_once_with("Go")
  37. observers[1].assert_called_once_with("Go")
  38. self.assertEquals(mock_logger.warning.call_count, 1)
  39. self.assertIsInstance(mock_logger.warning.call_args[0][0], str)
  40. def test_signal_prereg(self):
  41. observer = Mock()
  42. self.dist.observe("flare", observer)
  43. self.dist.declare("flare")
  44. self.dist.fire("flare", 4, 5)
  45. observer.assert_called_with(4, 5)
  46. def test_signal_undeclared(self):
  47. def code():
  48. self.dist.fire("notification")
  49. self.assertRaises(KeyError, code)