From 4ba4cb583aa46a514155c372713ccca353e05170 Mon Sep 17 00:00:00 2001 From: Kekoa Kaaikala Date: Tue, 13 Sep 2022 15:32:15 +0000 Subject: [PATCH] Agent: Add thread ID to SocketsPipe thread name --- .../infection_monkey/network/relay/sockets_pipe.py | 9 ++++++++- .../network/relay/test_sockets_pipe.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 monkey/tests/unit_tests/infection_monkey/network/relay/test_sockets_pipe.py diff --git a/monkey/infection_monkey/network/relay/sockets_pipe.py b/monkey/infection_monkey/network/relay/sockets_pipe.py index ca60a7c22..b4d59416a 100644 --- a/monkey/infection_monkey/network/relay/sockets_pipe.py +++ b/monkey/infection_monkey/network/relay/sockets_pipe.py @@ -15,6 +15,8 @@ logger = getLogger(__name__) class SocketsPipe(Thread): """Manages a pipe between two sockets.""" + _thread_count: int = 0 + def __init__( self, source, @@ -25,9 +27,14 @@ class SocketsPipe(Thread): self.source = source self.dest = dest self.timeout = timeout - super().__init__(name="SocketsPipeThread", daemon=True) + super().__init__(name=f"SocketsPipeThread-{self._next_thread_num()}", daemon=True) self._pipe_closed = pipe_closed + @classmethod + def _next_thread_num(cls): + cls._thread_count += 1 + return cls._thread_count + def _pipe(self): sockets = [self.source, self.dest] socket_closed = False diff --git a/monkey/tests/unit_tests/infection_monkey/network/relay/test_sockets_pipe.py b/monkey/tests/unit_tests/infection_monkey/network/relay/test_sockets_pipe.py new file mode 100644 index 000000000..0a98c0247 --- /dev/null +++ b/monkey/tests/unit_tests/infection_monkey/network/relay/test_sockets_pipe.py @@ -0,0 +1,14 @@ +from unittest.mock import MagicMock + +from monkey.infection_monkey.network.relay import SocketsPipe + + +def test_sockets_pipe__name_increments(): + sock_in = MagicMock() + sock_out = MagicMock() + + pipe1 = SocketsPipe(sock_in, sock_out, None) + assert pipe1.name.endswith("1") + + pipe2 = SocketsPipe(sock_in, sock_out, None) + assert pipe2.name.endswith("2")