Skip to content

Commit d025e5f

Browse files
committed
Fixed test_stdout_prepend_timestamp and added test_stderr_prepend_timestamp. The tests are leaking open file descriptors, not sure why calling dispatcher.close() doesn't close them.
1 parent 7341d45 commit d025e5f

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

supervisor/tests/test_dispatchers.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,24 +571,77 @@ def test_close(self):
571571
self.assertEqual(dispatcher.closed, True)
572572

573573
def test_stdout_prepend_timestamp(self):
574+
import time
574575
from supervisor import loggers
575576
from supervisor.loggers import getLogger
576577

577578
options = DummyOptions()
578-
options.getLogger = getLogger # actually use real logger
579+
options.getLogger = getLogger # actually use real logger
579580
options.loglevel = loggers.LevelsByName.TRAC
580581

581582
logfile = '/tmp/foo'
583+
message = "testing prepand"
582584
config = DummyPConfig(options, 'process1', '/bin/process1',
583585
stdout_logfile=logfile, stdout_prepend_timestamp=True)
584586
process = DummyProcess(config)
587+
585588
dispatcher = self._makeOne(process)
586-
dispatcher.output_buffer = 'a'
589+
dispatcher.removelogs()
590+
dispatcher.output_buffer = message
587591
dispatcher.record_output()
588592

593+
# flush out the log into log files
589594
[x.flush() for x in dispatcher.childlog.handlers]
595+
596+
# logger will prefix the stdout log with the timestamp down to milliseconds
597+
# but not feasible to test to that resolution
598+
timestamp_prefix = time.strftime("%Y-%m-%d %H:%M:%S")
599+
590600
with open(logfile, 'rb') as f:
591-
self.assertEqual(b'testing stdout prepend timestamp', f.read())
601+
content = f.read()
602+
# check if the timestamp is prepended to the log
603+
self.assertEqual(timestamp_prefix.encode(), content[0:len(timestamp_prefix)])
604+
# check if the message is at the end of the log line
605+
self.assertEqual(message.encode(), content[-len(message):])
606+
607+
dispatcher.close()
608+
609+
def test_stderr_prepend_timestamp(self):
610+
import time
611+
from supervisor import loggers
612+
from supervisor.loggers import getLogger
613+
614+
options = DummyOptions()
615+
options.getLogger = getLogger # actually use real logger
616+
options.loglevel = loggers.LevelsByName.TRAC
617+
618+
logfile = '/tmp/foo'
619+
message = "testing prepand"
620+
config = DummyPConfig(options, 'process1', '/bin/process1',
621+
stderr_logfile=logfile, stderr_prepend_timestamp=True)
622+
process = DummyProcess(config)
623+
624+
dispatcher = self._makeOne(process, channel='stderr')
625+
dispatcher.removelogs()
626+
dispatcher.output_buffer = message
627+
dispatcher.record_output()
628+
629+
# flush out the log into log files
630+
[x.flush() for x in dispatcher.childlog.handlers]
631+
632+
# logger will prefix the stdout log with the timestamp down to milliseconds
633+
# but not feasible to test to that resolution
634+
timestamp_prefix = time.strftime("%Y-%m-%d %H:%M:%S")
635+
636+
with open(logfile, 'rb') as f:
637+
content = f.read()
638+
# check if the timestamp is prepended to the log
639+
self.assertEqual(timestamp_prefix.encode(), content[0:len(timestamp_prefix)])
640+
# check if the message is at the end of the log line
641+
self.assertEqual(message.encode(), content[-len(message):])
642+
643+
dispatcher.close()
644+
dispatcher.removelogs()
592645

593646
class PInputDispatcherTests(unittest.TestCase):
594647
def _getTargetClass(self):

0 commit comments

Comments
 (0)