_base.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, 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, method: str, args: Iterable[Any], expected_result: Optional[Any] = None
  40. ) -> None:
  41. master_result = self.get_success(getattr(self.master_store, method)(*args))
  42. worker_result = self.get_success(getattr(self.worker_store, method)(*args))
  43. if expected_result is not None:
  44. self.assertEqual(
  45. master_result,
  46. expected_result,
  47. "Expected master result to be %r but was %r"
  48. % (expected_result, master_result),
  49. )
  50. self.assertEqual(
  51. worker_result,
  52. expected_result,
  53. "Expected worker result to be %r but was %r"
  54. % (expected_result, worker_result),
  55. )
  56. self.assertEqual(
  57. master_result,
  58. worker_result,
  59. "Worker result %r does not match master result %r"
  60. % (worker_result, master_result),
  61. )