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")