test_remote_server_up.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.address import IPv4Address
  16. from twisted.internet.interfaces import IProtocol
  17. from twisted.test.proto_helpers import StringTransport
  18. from synapse.replication.tcp.resource import ReplicationStreamProtocolFactory
  19. from tests.unittest import HomeserverTestCase
  20. class RemoteServerUpTestCase(HomeserverTestCase):
  21. def prepare(self, reactor, clock, hs):
  22. self.factory = ReplicationStreamProtocolFactory(hs)
  23. def _make_client(self) -> Tuple[IProtocol, StringTransport]:
  24. """Create a new direct TCP replication connection"""
  25. proto = self.factory.buildProtocol(IPv4Address("TCP", "127.0.0.1", 0))
  26. transport = StringTransport()
  27. proto.makeConnection(transport)
  28. # We can safely ignore the commands received during connection.
  29. self.pump()
  30. transport.clear()
  31. return proto, transport
  32. def test_relay(self):
  33. """Test that Synapse will relay REMOTE_SERVER_UP commands to all
  34. other connections, but not the one that sent it.
  35. """
  36. proto1, transport1 = self._make_client()
  37. # We shouldn't receive an echo.
  38. proto1.dataReceived(b"REMOTE_SERVER_UP example.com\n")
  39. self.pump()
  40. self.assertEqual(transport1.value(), b"")
  41. # But we should see an echo if we connect another client
  42. proto2, transport2 = self._make_client()
  43. proto1.dataReceived(b"REMOTE_SERVER_UP example.com\n")
  44. self.pump()
  45. self.assertEqual(transport1.value(), b"")
  46. self.assertEqual(transport2.value(), b"REMOTE_SERVER_UP example.com\n")