Skip to content

Commit

Permalink
replace counter name with uuid
Browse files Browse the repository at this point in the history
Signed-off-by: Jericho Tolentino <[email protected]>
  • Loading branch information
jericht committed May 1, 2024
1 parent 7371685 commit 785e42a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/openjd/adaptor_runtime/_http/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import stat
import tempfile
import uuid

from .._osname import OSName
from .exceptions import (
Expand Down Expand Up @@ -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")
Expand Down
10 changes: 8 additions & 2 deletions test/openjd/adaptor_runtime/unit/http/test_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 785e42a

Please sign in to comment.