You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

68 lines
2.1 KiB

  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) -> None:
  20. self.dist = Distributor()
  21. def test_signal_dispatch(self) -> None:
  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) -> None:
  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) -> None:
  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) -> None:
  46. def code() -> None:
  47. self.dist.fire("notification")
  48. self.assertRaises(KeyError, code)