test_remote_handler.py 5.7 KB

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