_base.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. server.makeConnection(FakeTransport(client, reactor))
  46. def replicate(self):
  47. """Tell the master side of replication that something has happened, and then
  48. wait for the replication to occur.
  49. """
  50. self.streamer.on_notifier_poke()
  51. self.pump(0.1)
  52. def check(self, method, args, expected_result=None):
  53. master_result = self.get_success(getattr(self.master_store, method)(*args))
  54. slaved_result = self.get_success(getattr(self.slaved_store, method)(*args))
  55. if expected_result is not None:
  56. self.assertEqual(master_result, expected_result)
  57. self.assertEqual(slaved_result, expected_result)
  58. self.assertEqual(master_result, slaved_result)