_base.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 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
  16. from synapse.replication.tcp.commands import ReplicateCommand
  17. from synapse.replication.tcp.protocol import ClientReplicationStreamProtocol
  18. from synapse.replication.tcp.resource import ReplicationStreamProtocolFactory
  19. from tests import unittest
  20. from tests.server import FakeTransport
  21. class BaseStreamTestCase(unittest.HomeserverTestCase):
  22. """Base class for tests of the replication streams"""
  23. def prepare(self, reactor, clock, hs):
  24. # build a replication server
  25. server_factory = ReplicationStreamProtocolFactory(self.hs)
  26. self.streamer = server_factory.streamer
  27. server = server_factory.buildProtocol(None)
  28. # build a replication client, with a dummy handler
  29. handler_factory = Mock()
  30. self.test_handler = TestReplicationClientHandler()
  31. self.test_handler.factory = handler_factory
  32. self.client = ClientReplicationStreamProtocol(
  33. "client", "test", clock, self.test_handler
  34. )
  35. # wire them together
  36. self.client.makeConnection(FakeTransport(server, reactor))
  37. server.makeConnection(FakeTransport(self.client, reactor))
  38. def replicate(self):
  39. """Tell the master side of replication that something has happened, and then
  40. wait for the replication to occur.
  41. """
  42. self.streamer.on_notifier_poke()
  43. self.pump(0.1)
  44. def replicate_stream(self, stream, token="NOW"):
  45. """Make the client end a REPLICATE command to set up a subscription to a stream"""
  46. self.client.send_command(ReplicateCommand(stream, token))
  47. class TestReplicationClientHandler(object):
  48. """Drop-in for ReplicationClientHandler which just collects RDATA rows"""
  49. def __init__(self):
  50. self.received_rdata_rows = []
  51. def get_streams_to_replicate(self):
  52. return {}
  53. def get_currently_syncing_users(self):
  54. return []
  55. def update_connection(self, connection):
  56. pass
  57. def finished_connecting(self):
  58. pass
  59. async def on_rdata(self, stream_name, token, rows):
  60. for r in rows:
  61. self.received_rdata_rows.append((stream_name, token, r))