test_distributor.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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(
  35. "synapse.util.distributor.logger", spec=["warning"]
  36. ) as mock_logger:
  37. self.dist.fire("alarm", "Go")
  38. observers[0].assert_called_once_with("Go")
  39. observers[1].assert_called_once_with("Go")
  40. self.assertEquals(mock_logger.warning.call_count, 1)
  41. self.assertIsInstance(
  42. mock_logger.warning.call_args[0][0], str
  43. )
  44. def test_signal_prereg(self):
  45. observer = Mock()
  46. self.dist.observe("flare", observer)
  47. self.dist.declare("flare")
  48. self.dist.fire("flare", 4, 5)
  49. observer.assert_called_with(4, 5)
  50. def test_signal_undeclared(self):
  51. def code():
  52. self.dist.fire("notification")
  53. self.assertRaises(KeyError, code)