test: make slow and order-dependent tests fast and independent

Slow tests:
- TestPerformance inserted 10 000 records three times per backend. One
  test per backend now times insert, save and load on the same records,
  with the same assertions.
- test_measurement_realtime_stream slept one second per update, 20 s per
  backend. The updates use synthetic timestamps one second apart in the
  recent past, without a pause, and send the same duplicates,
  out-of-order and burst writes.
- test_readers_never_see_a_partial_document: the readers spun on the
  cache and kept the writer waiting for the GIL (~50 s). They now yield
  with time.sleep(0) after each lookup (~0.5 s); a non-atomic save still
  fails the test 10 of 10 times.
- test_devicessettingsbattery.py compiled the same plain profile before
  each of its 235 tests. A module-scoped fixture compiles it once; a
  compiled profile is frozen and may be shared (20.8 s -> 9.4 s).
- The --finalize case of test_geneticoptimize.py runs 250 instead of
  400 generations. For this input and seed the best fitness is final in
  generation 213, and the solutions after 250 and 400 generations are
  identical field by field. The test checks the keys and the consistency
  of the result, not the schedule, so it detects the same as before
  (127 s -> 80 s on GitHub runners). test_genetic0optimize.py keeps 400.

Tests that depended on another test of the same process, found by
running them in another order and under pytest-xdist:
- TestPredictionContainer::test_len relied on a class flag that a
  TestPredictionProvider fixture set and never reset. Both fixtures now
  set the flags with monkeypatch.
- The EnergyCharts status code test got a cached response from an
  earlier test; it now calls _request_forecast(force_update=True) and
  asserts that the request was made.
- test_ingress_safe_links did not wait for EOSdash, which EOS starts
  after its own startup.
- test_forward_stream_truncates_very_long_line found the log queue left
  behind by test_server_start_eosdash (no worker, closed event loop). It
  installs its own queue now, and test_server_start_eosdash no longer
  leaves its queue behind. Dropping lines from a full queue was only
  covered through that left-over queue and gets a test of its own.

Refs #1553

Co-authored-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>
Assisted-by: Claude Code (claude-opus-5-5)
Assisted-by: Claude Code (claude-fable-5-2)
This commit is contained in:
Christin
2026-11-20 07:15:33 +01:00
committed by Bobby Noelte
co-authored by SchaafAlexander Claude Opus 5.5
parent ce7fb96faf
commit e2726354c2
10 changed files with 99 additions and 62 deletions
+53 -15
View File
@@ -115,21 +115,19 @@ class TestServerStartStop:
monkeypatch.setenv("EOS_CONFIG_DIR", str(eos_dir))
# Import after env vars are set
from akkudoktoreos.server.rest import starteosdash
from akkudoktoreos.server.rest.starteosdash import (
EOSDASH_LOG_MAX_LINE_BYTES,
_eosdash_log_worker,
eosdash_log_queue,
forward_stream,
)
# ---- Ensure queue + worker are initialized ----
if eosdash_log_queue is None:
from akkudoktoreos.server.rest import starteosdash
starteosdash.eosdash_log_queue = asyncio.Queue(maxsize=10)
worker_task = asyncio.create_task(_eosdash_log_worker())
else:
worker_task = None
# ---- Queue + worker of this test ----
# A queue left behind by an earlier test of this process (test_server_start_eosdash)
# has no worker and belongs to a closed event loop. monkeypatch restores the value.
log_queue: asyncio.Queue = asyncio.Queue(maxsize=10)
monkeypatch.setattr(starteosdash, "eosdash_log_queue", log_queue)
worker_task = asyncio.create_task(_eosdash_log_worker())
long_message = "X" * (EOSDASH_LOG_MAX_LINE_BYTES + 10_000)
raw_line = f"INFO some.module:123 some_func - {long_message}\n"
@@ -157,12 +155,8 @@ class TestServerStartStop:
logger.remove(logger_id)
# Clean shutdown of worker (important for pytest)
if worker_task:
from akkudoktoreos.server.rest import starteosdash
if starteosdash.eosdash_log_queue:
starteosdash.eosdash_log_queue.put_nowait(None)
await worker_task
log_queue.put_nowait(None)
await worker_task
# ---- Assert ----
assert len(records) == 1, "Expected exactly one log record"
@@ -173,6 +167,46 @@ class TestServerStartStop:
assert msg.endswith("[TRUNCATED]"), "Expected truncation marker"
assert len(msg) <= EOSDASH_LOG_MAX_LINE_BYTES + 20
@pytest.mark.asyncio
async def test_forward_stream_drops_lines_when_queue_is_full(self, monkeypatch, tmp_path):
"""Lines that do not fit into the log queue are dropped with a single warning."""
eos_dir = tmp_path
monkeypatch.setenv("EOS_DIR", str(eos_dir))
monkeypatch.setenv("EOS_CONFIG_DIR", str(eos_dir))
# Import after env vars are set
from akkudoktoreos.server.rest import starteosdash
from akkudoktoreos.server.rest.starteosdash import forward_stream
# A queue for one entry and no worker: the second and the third line find it full.
log_queue: asyncio.Queue = asyncio.Queue(maxsize=1)
monkeypatch.setattr(starteosdash, "eosdash_log_queue", log_queue)
monkeypatch.setattr(starteosdash, "eosdash_last_drop_warning", float("-inf"))
reader = asyncio.StreamReader()
reader.feed_data(
b"INFO | some/module.py:123 | some_func | first\n"
b"INFO | some/module.py:124 | some_func | second\n"
b"a line without log metadata\n"
)
reader.feed_eof()
records = []
def sink(message):
records.append(message.record)
logger_id = logger.add(sink, level="WARNING")
try:
await forward_stream(reader)
finally:
logger.remove(logger_id)
assert log_queue.qsize() == 1
drop_warnings = [record for record in records if "queue full" in record["message"]]
assert len(drop_warnings) == 1, "Expected one rate-limited warning for two dropped lines"
@pytest.mark.asyncio
async def test_server_start_eosdash(self, config_eos, monkeypatch, tmp_path):
"""Test the EOSdash server startup from EOS.
@@ -192,6 +226,10 @@ class TestServerStartStop:
# Import with environment vars set to prevent creation of EOS.config.json in wrong dir.
from akkudoktoreos.server.rest import starteosdash
# The supervisor creates the log queue in this test's event loop. monkeypatch
# removes it again, so no later test of the process finds a queue of a closed loop.
monkeypatch.setattr(starteosdash, "eosdash_log_queue", None)
config_eos.server.host = get_default_host()
config_eos.server.port = 8503
config_eos.server.eosdash_host = config_eos.server.host