Skip to content

Commit

Permalink
Merge pull request #69 from pymeasure/fix-coordinator-hostname
Browse files Browse the repository at this point in the history
Fix Coordinator to not use period in hostname as namespace
  • Loading branch information
BenediktBurger authored Mar 27, 2024
2 parents 646ccfd + 6867ff6 commit 0ce18cf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
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

0 comments on commit 0ce18cf

Please sign in to comment.