test_remote_server_up.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Tuple
  15. from twisted.internet.interfaces import IProtocol
  16. from twisted.test.proto_helpers import StringTransport
  17. from synapse.replication.tcp.resource import ReplicationStreamProtocolFactory
  18. from tests.unittest import HomeserverTestCase
  19. class RemoteServerUpTestCase(HomeserverTestCase):
  20. def prepare(self, reactor, clock, hs):
  21. self.factory = ReplicationStreamProtocolFactory(hs)
  22. def _make_client(self) -> Tuple[IProtocol, StringTransport]:
  23. """Create a new direct TCP replication connection"""
  24. proto = self.factory.buildProtocol(("127.0.0.1", 0))
  25. transport = StringTransport()
  26. proto.makeConnection(transport)
  27. # We can safely ignore the commands received during connection.
  28. self.pump()
  29. transport.clear()
  30. return proto, transport
  31. def test_relay(self):
  32. """Test that Synapse will relay REMOTE_SERVER_UP commands to all
  33. other connections, but not the one that sent it.
  34. """
  35. proto1, transport1 = self._make_client()
  36. # We shouldn't receive an echo.
  37. proto1.dataReceived(b"REMOTE_SERVER_UP example.com\n")
  38. self.pump()
  39. self.assertEqual(transport1.value(), b"")
  40. # But we should see an echo if we connect another client
  41. proto2, transport2 = self._make_client()
  42. proto1.dataReceived(b"REMOTE_SERVER_UP example.com\n")
  43. self.pump()
  44. self.assertEqual(transport1.value(), b"")
  45. self.assertEqual(transport2.value(), b"REMOTE_SERVER_UP example.com\n")