test_remote_server_up.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 typing import Tuple
  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. """
  26. proto = self.factory.buildProtocol(("127.0.0.1", 0))
  27. transport = StringTransport()
  28. proto.makeConnection(transport)
  29. # We can safely ignore the commands received during connection.
  30. self.pump()
  31. transport.clear()
  32. return proto, transport
  33. def test_relay(self):
  34. """Test that Synapse will relay REMOTE_SERVER_UP commands to all
  35. other connections, but not the one that sent it.
  36. """
  37. proto1, transport1 = self._make_client()
  38. # We shouldn't receive an echo.
  39. proto1.dataReceived(b"REMOTE_SERVER_UP example.com\n")
  40. self.pump()
  41. self.assertEqual(transport1.value(), b"")
  42. # But we should see an echo if we connect another client
  43. proto2, transport2 = self._make_client()
  44. proto1.dataReceived(b"REMOTE_SERVER_UP example.com\n")
  45. self.pump()
  46. self.assertEqual(transport1.value(), b"")
  47. self.assertEqual(transport2.value(), b"REMOTE_SERVER_UP example.com\n")