test_remote_handler.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Copyright 2019 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.protocol import Protocol
  16. from twisted.test.proto_helpers import AccumulatingProtocol, MemoryReactorClock
  17. from synapse.logging import RemoteHandler
  18. from tests.logging import LoggerCleanupMixin
  19. from tests.server import FakeTransport, get_clock
  20. from tests.unittest import TestCase
  21. def connect_logging_client(
  22. reactor: MemoryReactorClock, client_id: int
  23. ) -> Tuple[Protocol, AccumulatingProtocol]:
  24. # This is essentially tests.server.connect_client, but disabling autoflush on
  25. # the client transport. This is necessary to avoid an infinite loop due to
  26. # sending of data via the logging transport causing additional logs to be
  27. # written.
  28. factory = reactor.tcpClients.pop(client_id)[2]
  29. client = factory.buildProtocol(None)
  30. server = AccumulatingProtocol()
  31. server.makeConnection(FakeTransport(client, reactor))
  32. client.makeConnection(FakeTransport(server, reactor, autoflush=False))
  33. return client, server
  34. class RemoteHandlerTestCase(LoggerCleanupMixin, TestCase):
  35. def setUp(self) -> None:
  36. self.reactor, _ = get_clock()
  37. def test_log_output(self) -> None:
  38. """
  39. The remote handler delivers logs over TCP.
  40. """
  41. handler = RemoteHandler("127.0.0.1", 9000, _reactor=self.reactor)
  42. logger = self.get_logger(handler)
  43. logger.info("Hello there, %s!", "wally")
  44. # Trigger the connection
  45. client, server = connect_logging_client(self.reactor, 0)
  46. # Trigger data being sent
  47. client_transport = client.transport
  48. assert isinstance(client_transport, FakeTransport)
  49. client_transport.flush()
  50. # One log message, with a single trailing newline
  51. logs = server.data.decode("utf8").splitlines()
  52. self.assertEqual(len(logs), 1)
  53. self.assertEqual(server.data.count(b"\n"), 1)
  54. # Ensure the data passed through properly.
  55. self.assertEqual(logs[0], "Hello there, wally!")
  56. def test_log_backpressure_debug(self) -> None:
  57. """
  58. When backpressure is hit, DEBUG logs will be shed.
  59. """
  60. handler = RemoteHandler(
  61. "127.0.0.1", 9000, maximum_buffer=10, _reactor=self.reactor
  62. )
  63. logger = self.get_logger(handler)
  64. # Send some debug messages
  65. for i in range(0, 3):
  66. logger.debug("debug %s" % (i,))
  67. # Send a bunch of useful messages
  68. for i in range(0, 7):
  69. logger.info("info %s" % (i,))
  70. # The last debug message pushes it past the maximum buffer
  71. logger.debug("too much debug")
  72. # Allow the reconnection
  73. client, server = connect_logging_client(self.reactor, 0)
  74. client_transport = client.transport
  75. assert isinstance(client_transport, FakeTransport)
  76. client_transport.flush()
  77. # Only the 7 infos made it through, the debugs were elided
  78. logs = server.data.splitlines()
  79. self.assertEqual(len(logs), 7)
  80. self.assertNotIn(b"debug", server.data)
  81. def test_log_backpressure_info(self) -> None:
  82. """
  83. When backpressure is hit, DEBUG and INFO logs will be shed.
  84. """
  85. handler = RemoteHandler(
  86. "127.0.0.1", 9000, maximum_buffer=10, _reactor=self.reactor
  87. )
  88. logger = self.get_logger(handler)
  89. # Send some debug messages
  90. for i in range(0, 3):
  91. logger.debug("debug %s" % (i,))
  92. # Send a bunch of useful messages
  93. for i in range(0, 10):
  94. logger.warning("warn %s" % (i,))
  95. # Send a bunch of info messages
  96. for i in range(0, 3):
  97. logger.info("info %s" % (i,))
  98. # The last debug message pushes it past the maximum buffer
  99. logger.debug("too much debug")
  100. # Allow the reconnection
  101. client, server = connect_logging_client(self.reactor, 0)
  102. client_transport = client.transport
  103. assert isinstance(client_transport, FakeTransport)
  104. client_transport.flush()
  105. # The 10 warnings made it through, the debugs and infos were elided
  106. logs = server.data.splitlines()
  107. self.assertEqual(len(logs), 10)
  108. self.assertNotIn(b"debug", server.data)
  109. self.assertNotIn(b"info", server.data)
  110. def test_log_backpressure_cut_middle(self) -> None:
  111. """
  112. When backpressure is hit, and no more DEBUG and INFOs cannot be culled,
  113. it will cut the middle messages out.
  114. """
  115. handler = RemoteHandler(
  116. "127.0.0.1", 9000, maximum_buffer=10, _reactor=self.reactor
  117. )
  118. logger = self.get_logger(handler)
  119. # Send a bunch of useful messages
  120. for i in range(0, 20):
  121. logger.warning("warn %s" % (i,))
  122. # Allow the reconnection
  123. client, server = connect_logging_client(self.reactor, 0)
  124. client_transport = client.transport
  125. assert isinstance(client_transport, FakeTransport)
  126. client_transport.flush()
  127. # The first five and last five warnings made it through, the debugs and
  128. # infos were elided
  129. logs = server.data.decode("utf8").splitlines()
  130. self.assertEqual(
  131. ["warn %s" % (i,) for i in range(5)]
  132. + ["warn %s" % (i,) for i in range(15, 20)],
  133. logs,
  134. )
  135. def test_cancel_connection(self) -> None:
  136. """
  137. Gracefully handle the connection being cancelled.
  138. """
  139. handler = RemoteHandler(
  140. "127.0.0.1", 9000, maximum_buffer=10, _reactor=self.reactor
  141. )
  142. logger = self.get_logger(handler)
  143. # Send a message.
  144. logger.info("Hello there, %s!", "wally")
  145. # Do not accept the connection and shutdown. This causes the pending
  146. # connection to be cancelled (and should not raise any exceptions).
  147. handler.close()