_base.py 3.5 KB

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