Skip to content

Commit 96b31a3

Browse files
committed
tests: Fix priority and heartbeat tests
1 parent a59987d commit 96b31a3

File tree

1 file changed

+104
-100
lines changed

1 file changed

+104
-100
lines changed

tests/test_command_runner.py

Lines changed: 104 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
__author__ = "Orsiris de Jong"
1919
__copyright__ = "Copyright (C) 2015-2025 Orsiris de Jong"
2020
__licence__ = "BSD 3 Clause"
21-
__build__ = "2025041601"
21+
__build__ = "2025041801"
2222

2323

2424
import sys
@@ -50,36 +50,11 @@ def timestamp(date):
5050
return date.timestamp()
5151

5252

53-
# Log capture class, blatantly copied from https://stackoverflow.com/a/37967421/2635443
54-
class TailLogHandler(logging.Handler):
55-
56-
def __init__(self, log_queue):
57-
logging.Handler.__init__(self)
58-
self.log_queue = log_queue
59-
60-
def emit(self, record):
61-
self.log_queue.append(self.format(record))
62-
63-
64-
class TailLogger(object):
65-
66-
def __init__(self, maxlen):
67-
self._log_queue = collections.deque(maxlen=maxlen)
68-
self._log_handler = TailLogHandler(self._log_queue)
69-
70-
def contents(self):
71-
return "\n".join(self._log_queue)
72-
73-
@property
74-
def log_handler(self):
75-
return self._log_handler
76-
77-
7853
# We need a logging unit here
7954
logger = logging.getLogger()
80-
logger.setLevel(logging.ERROR)
55+
logger.setLevel(logging.WARNING)
8156
handler = logging.StreamHandler(sys.stdout)
82-
handler.setLevel(logging.ERROR)
57+
handler.setLevel(logging.WARNING)
8358
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
8459
handler.setFormatter(formatter)
8560
logger.addHandler(handler)
@@ -852,72 +827,6 @@ def on_exit():
852827
assert ON_EXIT_CALLED is True, "On exit was never called"
853828

854829

855-
def test_low_priority():
856-
def check_low_priority(process):
857-
niceness = os.nice(process.pid)
858-
io_niceness = psutil.Process(process.pid).ionice()
859-
if os.name == "nt":
860-
assert niceness == 16384, "Process niceness not properly set: {}".format(
861-
niceness
862-
)
863-
assert io_niceness == 1, "Process io niceness not set properly: {}".format(
864-
io_niceness
865-
)
866-
else:
867-
assert niceness == 15, "Process niceness not properly set: {}".format(
868-
niceness
869-
)
870-
assert io_niceness == 3, "Process io niceness not set properly: {}".format(
871-
io_niceness
872-
)
873-
print("Nice !")
874-
875-
def command_runner_thread():
876-
return command_runner_threaded(
877-
PING_CMD,
878-
priority="low",
879-
io_priority="low",
880-
process_callback=check_low_priority,
881-
)
882-
883-
thread = threading.Thread(target=command_runner_thread, args=())
884-
thread.daemon = True # thread dies with the program
885-
thread.start()
886-
887-
888-
def test_high_priority():
889-
def check_high_priority(process):
890-
niceness = os.nice(process.pid)
891-
io_niceness = psutil.Process(process.pid).ionice()
892-
if os.name == "nt":
893-
assert niceness == 128, "Process niceness not properly set: {}".format(
894-
niceness
895-
)
896-
assert io_niceness == 3, "Process io niceness not set properly: {}".format(
897-
io_niceness
898-
)
899-
else:
900-
assert niceness == -15, "Process niceness not properly set: {}".format(
901-
niceness
902-
)
903-
assert io_niceness == 1, "Process io niceness not set properly: {}".format(
904-
io_niceness
905-
)
906-
print("Nice !")
907-
908-
def command_runner_thread():
909-
return command_runner_threaded(
910-
PING_CMD,
911-
priority="low",
912-
io_priority="low",
913-
process_callback=check_high_priority,
914-
)
915-
916-
thread = threading.Thread(target=command_runner_thread, args=())
917-
thread.daemon = True # thread dies with the program
918-
thread.start()
919-
920-
921830
def test_no_close_queues():
922831
"""
923832
Test no_close_queues
@@ -976,10 +885,102 @@ def test_no_close_queues():
976885
assert exit_code == 0, "We did not succeed in running the thread"
977886

978887

888+
def test_low_priority():
889+
def check_low_priority(process):
890+
niceness = psutil.Process(process.pid).nice()
891+
io_niceness = psutil.Process(process.pid).ionice()
892+
if os.name == "nt":
893+
assert niceness == 16384, "Process low prio niceness not properly set: {}".format(
894+
niceness
895+
)
896+
assert io_niceness == 1, "Process low prio io niceness not set properly: {}".format(
897+
io_niceness
898+
)
899+
else:
900+
assert niceness == 15, "Process low prio niceness not properly set: {}".format(
901+
niceness
902+
)
903+
assert io_niceness == 3, "Process low prio io niceness not set properly: {}".format(
904+
io_niceness
905+
)
906+
print("Nice !")
907+
908+
def command_runner_thread():
909+
return command_runner_threaded(
910+
PING_CMD,
911+
priority="low",
912+
io_priority="low",
913+
process_callback=check_low_priority,
914+
)
915+
916+
thread = threading.Thread(target=command_runner_thread, args=())
917+
thread.daemon = True # thread dies with the program
918+
thread.start()
919+
920+
921+
def test_high_priority():
922+
def check_high_priority(process):
923+
niceness = psutil.Process(process.pid).nice()
924+
io_niceness = psutil.Process(process.pid).ionice()
925+
if os.name == "nt":
926+
assert niceness == 128, "Process high prio niceness not properly set: {}".format(
927+
niceness
928+
)
929+
# So se actually don't test this here, since high prio cannot be set on Windows unless
930+
# we have NtSetInformationProcess privilege
931+
# assert io_niceness == 3, "Process high prio io niceness not set properly: {}".format(
932+
# io_niceness
933+
# )
934+
else:
935+
assert niceness == -15, "Process high prio niceness not properly set: {}".format(
936+
niceness
937+
)
938+
assert io_niceness == 1, "Process high prio io niceness not set properly: {}".format(
939+
io_niceness
940+
)
941+
print("Nice !")
942+
943+
def command_runner_thread():
944+
return command_runner_threaded(
945+
PING_CMD,
946+
priority="high",
947+
# io_priority="high",
948+
process_callback=check_high_priority,
949+
)
950+
951+
thread = threading.Thread(target=command_runner_thread, args=())
952+
thread.daemon = True # thread dies with the program
953+
thread.start()
954+
955+
979956
def test_heartbeat():
980957
# Don't run this on github actions since they already capture logging output
981-
if running_on_github_actions():
982-
return
958+
#if running_on_github_actions():
959+
# return
960+
# Log capture class, blatantly copied from https://stackoverflow.com/a/37967421/2635443
961+
class TailLogHandler(logging.Handler):
962+
963+
def __init__(self, log_queue):
964+
logging.Handler.__init__(self)
965+
self.log_queue = log_queue
966+
967+
def emit(self, record):
968+
self.log_queue.append(self.format(record))
969+
970+
971+
class TailLogger(object):
972+
973+
def __init__(self, maxlen):
974+
self._log_queue = collections.deque(maxlen=maxlen)
975+
self._log_handler = TailLogHandler(self._log_queue)
976+
977+
def contents(self):
978+
return "\n".join(self._log_queue)
979+
980+
@property
981+
def log_handler(self):
982+
return self._log_handler
983+
983984
tail = TailLogger(10)
984985

985986
formatter = logging.Formatter(
@@ -989,13 +990,15 @@ def test_heartbeat():
989990
log_handler = tail.log_handler
990991
log_handler.setFormatter(formatter)
991992
logger.addHandler(log_handler) # Add the handler to the logger
992-
logger.setLevel(logging.ERROR)
993+
logger.setLevel(logging.INFO)
993994

994-
exit_code, _ = command_runner(
995+
exit_code, output = command_runner(
995996
PING_CMD_10S, heartbeat=2, shell=False
996997
)
997998
log_contents = tail.contents()
998-
print("LOGS\n", log_contents)
999+
print("LOGS:\n", log_contents)
1000+
print("END LOGS")
1001+
print("COMMAND_OUTPUT:\n", output)
9991002
assert exit_code == 0, "Exit code should be 0 for ping command with heartbeat"
10001003
# We should have a modulo 2 heeatbeat
10011004
assert (
@@ -1030,7 +1033,8 @@ def test_heartbeat():
10301033
test_null_redir()
10311034
test_split_streams()
10321035
test_on_exit()
1036+
test_no_close_queues()
10331037
test_low_priority()
10341038
test_high_priority()
1035-
test_no_close_queues()
10361039
test_heartbeat()
1040+

0 commit comments

Comments
 (0)