Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Coordinator to not use period in hostname as namespace #69

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyleco/coordinators/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(
**kwargs,
) -> None:
if namespace is None:
self.namespace = gethostname().encode()
self.namespace = gethostname().split(".")[0].encode()
elif isinstance(namespace, str):
self.namespace = namespace.encode()
elif isinstance(namespace, bytes):
Expand Down
32 changes: 24 additions & 8 deletions tests/coordinators/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
from pyleco.utils.events import SimpleEvent

from pyleco.coordinators.coordinator import Coordinator
from pyleco.coordinators import coordinator as coordinator_module # type: ignore


@pytest.fixture
def coordinator():
def coordinator() -> Coordinator:
coordinator = Coordinator(namespace="N1", host="N1host", cleaning_interval=1e5,
context=FakeContext(), # type: ignore
multi_socket=FakeMultiSocket()
Expand All @@ -61,24 +62,24 @@ def coordinator():
return coordinator


def fake_perf_counter():
def fake_perf_counter() -> float:
return 0.


@pytest.fixture()
def fake_counting(monkeypatch):
def fake_counting(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("pyleco.utils.coordinator_utils.perf_counter", fake_perf_counter)


cid = b"conversation_id;"


def fake_generate_cid():
def fake_generate_cid() -> bytes:
return cid


@pytest.fixture(autouse=True)
def fake_cid_generation(monkeypatch):
def fake_cid_generation(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("pyleco.core.serialization.generate_conversation_id", fake_generate_cid)


Expand Down Expand Up @@ -109,9 +110,24 @@ def test_method_is_available(self, component_methods, method):
raise AssertionError(f"Method {method} is not available.")


def test_coordinator_set_namespace_from_hostname():
coordinator = Coordinator(context=FakeContext()) # type: ignore
assert isinstance(coordinator.namespace, bytes)
class Test_coordinator_set_namespace_from_hostname:
@pytest.fixture
def namespace(self) -> bytes:
coordinator = Coordinator(context=FakeContext()) # type: ignore
return coordinator.namespace

def test_namespace_is_bytes(self, namespace):
assert isinstance(namespace, bytes)

def test_namespace_without_periods(self, namespace):
assert b"." not in namespace

def test_namespace_from_hostname_with_periods(self, monkeypatch: pytest.MonkeyPatch):
def fake_gethostname() -> str:
return "hostname.domain.tld"
monkeypatch.setattr(coordinator_module, "gethostname", fake_gethostname)
coordinator = Coordinator(context=FakeContext()) # type: ignore
assert coordinator.namespace == b"hostname"


def test_coordinator_set_namespace_bytes():
Expand Down
Loading