_base.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 mock import Mock, NonCallableMock
  16. from synapse.replication.tcp.client import (
  17. ReplicationClientFactory,
  18. ReplicationClientHandler,
  19. )
  20. from synapse.replication.tcp.resource import ReplicationStreamProtocolFactory
  21. from tests import unittest
  22. from tests.server import FakeTransport
  23. class BaseSlavedStoreTestCase(unittest.HomeserverTestCase):
  24. def make_homeserver(self, reactor, clock):
  25. hs = self.setup_test_homeserver(
  26. "blue",
  27. federation_client=Mock(),
  28. ratelimiter=NonCallableMock(spec_set=["can_do_action"]),
  29. )
  30. hs.get_ratelimiter().can_do_action.return_value = (True, 0)
  31. return hs
  32. def prepare(self, reactor, clock, hs):
  33. self.master_store = self.hs.get_datastore()
  34. self.slaved_store = self.STORE_TYPE(self.hs.get_db_conn(), self.hs)
  35. self.event_id = 0
  36. server_factory = ReplicationStreamProtocolFactory(self.hs)
  37. self.streamer = server_factory.streamer
  38. self.replication_handler = ReplicationClientHandler(self.slaved_store)
  39. client_factory = ReplicationClientFactory(
  40. self.hs, "client_name", self.replication_handler
  41. )
  42. server = server_factory.buildProtocol(None)
  43. client = client_factory.buildProtocol(None)
  44. client.makeConnection(FakeTransport(server, reactor))
  45. self.server_to_client_transport = FakeTransport(client, reactor)
  46. server.makeConnection(self.server_to_client_transport)
  47. def replicate(self):
  48. """Tell the master side of replication that something has happened, and then
  49. wait for the replication to occur.
  50. """
  51. self.streamer.on_notifier_poke()
  52. self.pump(0.1)
  53. def check(self, method, args, expected_result=None):
  54. master_result = self.get_success(getattr(self.master_store, method)(*args))
  55. slaved_result = self.get_success(getattr(self.slaved_store, method)(*args))
  56. if expected_result is not None:
  57. self.assertEqual(
  58. master_result,
  59. expected_result,
  60. "Expected master result to be %r but was %r"
  61. % (expected_result, master_result),
  62. )
  63. self.assertEqual(
  64. slaved_result,
  65. expected_result,
  66. "Expected slave result to be %r but was %r"
  67. % (expected_result, slaved_result),
  68. )
  69. self.assertEqual(
  70. master_result,
  71. slaved_result,
  72. "Slave result %r does not match master result %r"
  73. % (slaved_result, master_result),
  74. )