You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

178 lines
6.1 KiB

  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. assert isinstance(client.transport, FakeTransport)
  48. client.transport.flush()
  49. # One log message, with a single trailing newline
  50. logs = server.data.decode("utf8").splitlines()
  51. self.assertEqual(len(logs), 1)
  52. self.assertEqual(server.data.count(b"\n"), 1)
  53. # Ensure the data passed through properly.
  54. self.assertEqual(logs[0], "Hello there, wally!")
  55. def test_log_backpressure_debug(self) -> None:
  56. """
  57. When backpressure is hit, DEBUG logs will be shed.
  58. """
  59. handler = RemoteHandler(
  60. "127.0.0.1", 9000, maximum_buffer=10, _reactor=self.reactor
  61. )
  62. logger = self.get_logger(handler)
  63. # Send some debug messages
  64. for i in range(0, 3):
  65. logger.debug("debug %s" % (i,))
  66. # Send a bunch of useful messages
  67. for i in range(0, 7):
  68. logger.info("info %s" % (i,))
  69. # The last debug message pushes it past the maximum buffer
  70. logger.debug("too much debug")
  71. # Allow the reconnection
  72. client, server = connect_logging_client(self.reactor, 0)
  73. assert isinstance(client.transport, FakeTransport)
  74. client.transport.flush()
  75. # Only the 7 infos made it through, the debugs were elided
  76. logs = server.data.splitlines()
  77. self.assertEqual(len(logs), 7)
  78. self.assertNotIn(b"debug", server.data)
  79. def test_log_backpressure_info(self) -> None:
  80. """
  81. When backpressure is hit, DEBUG and INFO logs will be shed.
  82. """
  83. handler = RemoteHandler(
  84. "127.0.0.1", 9000, maximum_buffer=10, _reactor=self.reactor
  85. )
  86. logger = self.get_logger(handler)
  87. # Send some debug messages
  88. for i in range(0, 3):
  89. logger.debug("debug %s" % (i,))
  90. # Send a bunch of useful messages
  91. for i in range(0, 10):
  92. logger.warning("warn %s" % (i,))
  93. # Send a bunch of info messages
  94. for i in range(0, 3):
  95. logger.info("info %s" % (i,))
  96. # The last debug message pushes it past the maximum buffer
  97. logger.debug("too much debug")
  98. # Allow the reconnection
  99. client, server = connect_logging_client(self.reactor, 0)
  100. assert isinstance(client.transport, FakeTransport)
  101. client.transport.flush()
  102. # The 10 warnings made it through, the debugs and infos were elided
  103. logs = server.data.splitlines()
  104. self.assertEqual(len(logs), 10)
  105. self.assertNotIn(b"debug", server.data)
  106. self.assertNotIn(b"info", server.data)
  107. def test_log_backpressure_cut_middle(self) -> None:
  108. """
  109. When backpressure is hit, and no more DEBUG and INFOs cannot be culled,
  110. it will cut the middle messages out.
  111. """
  112. handler = RemoteHandler(
  113. "127.0.0.1", 9000, maximum_buffer=10, _reactor=self.reactor
  114. )
  115. logger = self.get_logger(handler)
  116. # Send a bunch of useful messages
  117. for i in range(0, 20):
  118. logger.warning("warn %s" % (i,))
  119. # Allow the reconnection
  120. client, server = connect_logging_client(self.reactor, 0)
  121. assert isinstance(client.transport, FakeTransport)
  122. client.transport.flush()
  123. # The first five and last five warnings made it through, the debugs and
  124. # infos were elided
  125. logs = server.data.decode("utf8").splitlines()
  126. self.assertEqual(
  127. ["warn %s" % (i,) for i in range(5)]
  128. + ["warn %s" % (i,) for i in range(15, 20)],
  129. logs,
  130. )
  131. def test_cancel_connection(self) -> None:
  132. """
  133. Gracefully handle the connection being cancelled.
  134. """
  135. handler = RemoteHandler(
  136. "127.0.0.1", 9000, maximum_buffer=10, _reactor=self.reactor
  137. )
  138. logger = self.get_logger(handler)
  139. # Send a message.
  140. logger.info("Hello there, %s!", "wally")
  141. # Do not accept the connection and shutdown. This causes the pending
  142. # connection to be cancelled (and should not raise any exceptions).
  143. handler.close()