From 785e42a36b2c1122fff89669ae0a42b9884eb366 Mon Sep 17 00:00:00 2001 From: Jericho Tolentino <68654047+jericht@users.noreply.github.com> Date: Wed, 1 May 2024 02:36:04 +0000 Subject: [PATCH] replace counter name with uuid Signed-off-by: Jericho Tolentino <68654047+jericht@users.noreply.github.com> --- src/openjd/adaptor_runtime/_http/sockets.py | 5 ++--- test/openjd/adaptor_runtime/unit/http/test_sockets.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/openjd/adaptor_runtime/_http/sockets.py b/src/openjd/adaptor_runtime/_http/sockets.py index aed857b..a839c8d 100644 --- a/src/openjd/adaptor_runtime/_http/sockets.py +++ b/src/openjd/adaptor_runtime/_http/sockets.py @@ -6,6 +6,7 @@ import os import stat import tempfile +import uuid from .._osname import OSName from .exceptions import ( @@ -106,11 +107,9 @@ def mkdir(path: str) -> str: return path def gen_socket_path(dir: str, base_name: str): - i = 0 name = base_name while os.path.exists(os.path.join(dir, name)): - i = i + 1 - name = f"{base_name}_{i}" + name = f"{base_name}_{str(uuid.uuid4()).replace('-', '')}" return os.path.join(dir, name) rel_path = os.path.join(".openjd", "adaptors", "sockets") diff --git a/test/openjd/adaptor_runtime/unit/http/test_sockets.py b/test/openjd/adaptor_runtime/unit/http/test_sockets.py index bf497cc..9967fbe 100644 --- a/test/openjd/adaptor_runtime/unit/http/test_sockets.py +++ b/test/openjd/adaptor_runtime/unit/http/test_sockets.py @@ -209,22 +209,28 @@ def test_raises_when_no_tmpdir_sticky_bit( ) ) + @patch.object(sockets.uuid, "uuid4") @patch.object(sockets.os.path, "exists") def test_handles_socket_name_collisions( self, mock_exists: MagicMock, + mock_uuid4: MagicMock, ) -> None: # GIVEN sock_name = "sock" - existing_sock_names = [sock_name, f"{sock_name}_1", f"{sock_name}_2"] + existing_sock_names = [sock_name, f"{sock_name}_abc123", f"{sock_name}_123abc"] mock_exists.side_effect = ([True] * len(existing_sock_names)) + [False] + + expected_sock_name = f"{sock_name}_123456789abcdefg" + mock_uuid4.side_effect = existing_sock_names[1:] + [expected_sock_name] + subject = SocketPathsStub() # WHEN result = subject.get_socket_path(sock_name) # THEN - assert result.endswith(f"{sock_name}_3") + assert result.endswith(expected_sock_name) mock_exists.call_count == len(existing_sock_names) + 1