_base.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright 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 typing import Any, Callable, Iterable, Optional
  16. from unittest.mock import Mock
  17. from twisted.test.proto_helpers import MemoryReactor
  18. from synapse.server import HomeServer
  19. from synapse.util import Clock
  20. from tests.replication._base import BaseStreamTestCase
  21. class BaseWorkerStoreTestCase(BaseStreamTestCase):
  22. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  23. return self.setup_test_homeserver(federation_client=Mock())
  24. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  25. super().prepare(reactor, clock, hs)
  26. self.reconnect()
  27. self.master_store = hs.get_datastores().main
  28. self.worker_store = self.worker_hs.get_datastores().main
  29. persistence = hs.get_storage_controllers().persistence
  30. assert persistence is not None
  31. self.persistance = persistence
  32. def replicate(self) -> None:
  33. """Tell the master side of replication that something has happened, and then
  34. wait for the replication to occur.
  35. """
  36. self.streamer.on_notifier_poke()
  37. self.pump(0.1)
  38. def check(
  39. self,
  40. method: str,
  41. args: Iterable[Any],
  42. expected_result: Optional[Any] = None,
  43. asserter: Optional[Callable[[Any, Any, Optional[Any]], None]] = None,
  44. ) -> None:
  45. if asserter is None:
  46. asserter = self.assertEqual
  47. master_result = self.get_success(getattr(self.master_store, method)(*args))
  48. worker_result = self.get_success(getattr(self.worker_store, method)(*args))
  49. if expected_result is not None:
  50. asserter(
  51. master_result,
  52. expected_result,
  53. "Expected master result to be %r but was %r"
  54. % (expected_result, master_result),
  55. )
  56. asserter(
  57. worker_result,
  58. expected_result,
  59. "Expected worker result to be %r but was %r"
  60. % (expected_result, worker_result),
  61. )
  62. asserter(
  63. master_result,
  64. worker_result,
  65. "Worker result %r does not match master result %r"
  66. % (worker_result, master_result),
  67. )