Skip to content
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
26 changes: 24 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

"""Fixtures that apply to every test in the workspace.

There is one, and it is here rather than in a package because the problem it
solves crosses packages.
They are here rather than in a package because the problems they solve cross
packages.
"""

from __future__ import annotations
Expand All @@ -14,6 +14,15 @@

import pytest

import pb_service.methods.hub

GRACE_SECONDS = 0.05
"""How long a connect waits after the first hub it hears, under test.

Long enough for every simulated hub to be heard, since they are all heard at
once, and short enough not to add up.
"""

_QUIET_LOGGERS = ("bleak", "asyncio")
"""The loggers ``setup_logging`` holds at a higher level, and so must restore."""

Expand Down Expand Up @@ -49,3 +58,16 @@ def _leave_logging_as_it_was() -> Iterator[None]: # pyright: ignore[reportUnuse
root.setLevel(level)
for logger, was in quiet_levels:
logger.setLevel(was)


@pytest.fixture(autouse=True)
def _short_grace(monkeypatch: pytest.MonkeyPatch) -> None: # pyright: ignore[reportUnusedFunction]
"""Stop a connect waiting for hubs that have all been heard already.

A connect that finds a hub waits a moment for a better route to it, since
real hubs advertise only every so often. Simulated hubs are all heard at
once, so under test the wait is only ever waited out, by every connect that
is not by id -- which was more than a third of the time the whole suite
took. A test about the wait itself sets it back.
"""
monkeypatch.setattr(pb_service.methods.hub, "PREFERENCE_SECONDS", GRACE_SECONDS)
8 changes: 6 additions & 2 deletions tests/e2e/test_hub_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import pytest

import pb_client
import pb_service.methods.hub # pyright: ignore[reportUnusedImport]
import pb_service.methods.hub
import pb_service.methods.program # pyright: ignore[reportUnusedImport]

# Imported to register the RPC methods.
Expand Down Expand Up @@ -645,7 +645,11 @@ async def test_commands_for_an_unknown_hub_are_reported(service_at: Endpoint) ->
assert info.value.code == ErrorCode.HUB_NOT_FOUND


async def test_connecting_to_a_hub_that_is_not_there(service_at: Endpoint) -> None:
async def test_connecting_to_a_hub_that_is_not_there(
service_at: Endpoint, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(pb_service.methods.hub, "DEFAULT_SCAN_SECONDS", 0.3)

async with pb_client.connect(service_at, autostart=False) as service:
with pytest.raises(JsonRpcError) as info:
await service.connect_hub(name="No Such Hub")
Expand Down
17 changes: 16 additions & 1 deletion tests/hardware/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ async def hub(service: pb_client.Service) -> AsyncGenerator[str, None]:
await _quiesce(service, name)


_no_hub: str | None = None
"""Why the first test that looked for a hub found none, if one has.

Each looks for SCAN_SECONDS, so with every hub switched off a run spent a
minute finding the same nothing once per test. Nothing is going to switch one
on partway through a run, so the first answer is the answer.
"""


async def _connected(service: pb_client.Service) -> str:
"""Find a hub to test against, connecting to one if need be.

Expand All @@ -148,17 +157,23 @@ async def _connected(service: pb_client.Service) -> str:
Returns:
The hub's name.
"""
global _no_hub

hubs, _ = await service.list_hubs()

if hubs:
return hubs[0].name

if _no_hub is not None:
pytest.skip(_no_hub)

try:
with anyio.fail_after(SCAN_SECONDS):
info = await service.connect_hub()

except _UNAVAILABLE as exc:
pytest.skip(f"no hub switched on and in range: {exc}")
_no_hub = f"no hub switched on and in range: {exc}"
pytest.skip(_no_hub)

return info.name

Expand Down
Loading