mirror of
https://github.com/ipnet-mesh/meshcore-hub.git
synced 2026-07-27 12:03:19 +02:00
fix: inject now param into run_evaluation for deterministic tests
Test data used a hardcoded _NOW (2026-07-12 12:00 UTC) while run_evaluation used real datetime.now(), causing a time-window flake once wall-clock time exceeded _NOW + window_hours (24h). Tests now pass now=_NOW explicitly; production callers are unaffected (defaults to datetime.now(timezone.utc) when not provided).
This commit is contained in:
@@ -16,12 +16,12 @@ from sqlalchemy import select
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def run_evaluation(db: DatabaseManager) -> int:
|
||||
def run_evaluation(db: DatabaseManager, now: datetime | None = None) -> int:
|
||||
"""Evaluate all enabled routes and upsert results.
|
||||
|
||||
Returns the number of routes evaluated.
|
||||
"""
|
||||
now = datetime.now(timezone.utc)
|
||||
now = now or datetime.now(timezone.utc)
|
||||
with db.session_scope() as session:
|
||||
routes = (
|
||||
session.execute(select(Route).where(Route.enabled.is_(True)))
|
||||
|
||||
@@ -73,12 +73,12 @@ class TestRunEvaluation:
|
||||
_make_reception(db_session, f"pkt{i}", ["AA", "BB"])
|
||||
db_session.commit()
|
||||
|
||||
count1 = run_evaluation(db_manager)
|
||||
count1 = run_evaluation(db_manager, now=_NOW)
|
||||
assert count1 == 1
|
||||
results = db_session.execute(select(RouteResult)).scalars().all()
|
||||
assert len(results) == 1
|
||||
|
||||
count2 = run_evaluation(db_manager)
|
||||
count2 = run_evaluation(db_manager, now=_NOW)
|
||||
assert count2 == 1
|
||||
results = db_session.execute(select(RouteResult)).scalars().all()
|
||||
assert len(results) == 1 # still one row (overwritten)
|
||||
@@ -90,7 +90,7 @@ class TestRunEvaluation:
|
||||
_make_route(db_session, "disabled", [node_a, node_b], enabled=False)
|
||||
db_session.commit()
|
||||
|
||||
count = run_evaluation(db_manager)
|
||||
count = run_evaluation(db_manager, now=_NOW)
|
||||
assert count == 1 # only the enabled route
|
||||
|
||||
def test_writes_correct_result(self, db_manager, db_session):
|
||||
@@ -103,7 +103,7 @@ class TestRunEvaluation:
|
||||
_make_reception(db_session, f"pkt{i}", ["AA", "BB"])
|
||||
db_session.commit()
|
||||
|
||||
run_evaluation(db_manager)
|
||||
run_evaluation(db_manager, now=_NOW)
|
||||
db_session.expire_all()
|
||||
|
||||
result = db_session.execute(
|
||||
|
||||
Reference in New Issue
Block a user