fix: refactor get_sample_timestamps

This commit is contained in:
Roman 2026-05-06 13:44:15 +08:00
parent e0575f6350
commit 762a7238ef
No known key found for this signature in database
GPG Key ID: 583BDF43C238B83E
5 changed files with 25 additions and 37 deletions

View File

@ -1,3 +1,4 @@
import os
from time import time
from datetime import datetime, timedelta
@ -150,50 +151,36 @@ PUBSUB_TOPICS_WRONG_FORMAT = [
{"description": "A bool", "value": True},
]
SAMPLE_TIMESTAMPS = [
{"description": "Now", "value": int(time() * 1e9), "valid_for": ["nwaku"]},
{
"description": "Far future",
"value": int((NOW + timedelta(days=365 * 10)).timestamp() * 1e9),
"valid_for": ["nwaku"],
}, # 10 years from now
{"description": "Recent past", "value": int((NOW - timedelta(hours=1)).timestamp() * 1e9), "valid_for": ["nwaku"]}, # 1 hour ago
{"description": "Near future", "value": int((NOW + timedelta(hours=1)).timestamp() * 1e9), "valid_for": ["nwaku"]}, # 1 hour ahead
{"description": "Positive number", "value": 1, "valid_for": ["nwaku"]},
{"description": "Negative number", "value": -1, "valid_for": ["nwaku"]},
{"description": "DST change", "value": int(datetime(2020, 3, 8, 2, 0, 0).timestamp() * 1e9), "valid_for": ["nwaku"]}, # DST starts
{"description": "Timestamp as string number", "value": str(int(time() * 1e9)), "valid_for": []},
{"description": "Invalid large number", "value": 2**63, "valid_for": []},
{"description": "Float number", "value": float(time() * 1e9), "valid_for": []},
{"description": "Array instead of timestamp", "value": [int(time() * 1e9)], "valid_for": []},
{"description": "Object instead of timestamp", "value": {"time": int(time() * 1e9)}, "valid_for": []},
{"description": "ISO 8601 timestamp", "value": "2023-12-26T10:58:51", "valid_for": []},
{"description": "Missing", "value": None, "valid_for": []},
]
def get_sample_timestamps():
"""Return timestamp test-cases with values evaluated fresh at call time.
This factory function MUST be called from inside each test (never at module
import time) so that the "Now" value maps to the active RLN epoch when the
message is actually published.
import time) so that the "Now" value reflects the actual time when the
message is published.
Valid_for semantics:
``["nwaku"]`` accepted by nwaku in both standalone and fleet (RLN) mode.
``[]`` rejected by nwaku in fleet (RLN) mode; kept for completeness
and for use by the companion invalid-timestamp tests.
``["nwaku"]`` accepted by nwaku in the current run mode.
``[]`` rejected by nwaku in the current run mode (either
structurally invalid type, or out-of-epoch in fleet/RLN
mode).
"""
now_ns = int(time() * 1e9)
now_dt = datetime.now()
fleet_mode = os.getenv("FLEET_BOOTSTRAP", "false").lower() == "true"
standalone_valid = [] if fleet_mode else ["nwaku"]
return [
{"description": "Now", "value": now_ns, "valid_for": ["nwaku"]},
{"description": "Far future", "value": int((now_dt + timedelta(days=365 * 10)).timestamp() * 1e9), "valid_for": []},
{"description": "Recent past", "value": int((now_dt - timedelta(hours=1)).timestamp() * 1e9), "valid_for": []},
{"description": "Near future", "value": int((now_dt + timedelta(hours=1)).timestamp() * 1e9), "valid_for": []},
{"description": "Positive number", "value": 1, "valid_for": []},
{"description": "Negative number", "value": -1, "valid_for": []},
{"description": "DST change", "value": int(datetime(2020, 3, 8, 2, 0, 0).timestamp() * 1e9), "valid_for": []},
# 10 years from now
{"description": "Far future", "value": int((now_dt + timedelta(days=365 * 10)).timestamp() * 1e9), "valid_for": standalone_valid},
# 1 hour ago
{"description": "Recent past", "value": int((now_dt - timedelta(hours=1)).timestamp() * 1e9), "valid_for": standalone_valid},
# 1 hour ahead
{"description": "Near future", "value": int((now_dt + timedelta(hours=1)).timestamp() * 1e9), "valid_for": standalone_valid},
{"description": "Positive number", "value": 1, "valid_for": standalone_valid},
{"description": "Negative number", "value": -1, "valid_for": standalone_valid},
# DST starts
{"description": "DST change", "value": int(datetime(2020, 3, 8, 2, 0, 0).timestamp() * 1e9), "valid_for": standalone_valid},
{"description": "Timestamp as string number", "value": str(now_ns), "valid_for": []},
{"description": "Invalid large number", "value": 2**63, "valid_for": []},
{"description": "Float number", "value": float(now_ns), "valid_for": []},

View File

@ -116,6 +116,8 @@ def configure_fleet_bootstrap(request, fleet_rln_state):
yield
return
os.environ["FLEET_BOOTSTRAP"] = "true"
from src.node.fleet_waku_node import FleetBootstrapConfig
from src.node.waku_node import WakuNode

View File

@ -2,7 +2,7 @@ import pytest
from src.env_vars import NODE_1, NODE_2
from src.libs.common import delay, to_base64
from src.libs.custom_logger import get_custom_logger
from src.test_data import SAMPLE_INPUTS, SAMPLE_TIMESTAMPS, get_sample_timestamps
from src.test_data import SAMPLE_INPUTS, get_sample_timestamps
from src.steps.filter import StepsFilter
logger = get_custom_logger(__name__)

View File

@ -9,7 +9,6 @@ from src.test_data import (
INVALID_PAYLOADS,
PUBSUB_TOPICS_WRONG_FORMAT,
SAMPLE_INPUTS,
SAMPLE_TIMESTAMPS,
VALID_PUBSUB_TOPICS,
get_sample_timestamps,
)
@ -177,7 +176,7 @@ class TestLightPushPublish(StepsLightPush):
def test_light_push_with_invalid_timestamps(self):
success_timestamps = []
for timestamp in SAMPLE_TIMESTAMPS:
for timestamp in get_sample_timestamps():
if self.light_push_node1.type() not in timestamp["valid_for"]:
logger.debug(f'Running test with timestamp {timestamp["description"]}')
message = self.create_message(timestamp=timestamp["value"])

View File

@ -4,7 +4,7 @@ from src.libs.custom_logger import get_custom_logger
from time import time
from src.libs.common import delay, to_base64
from src.steps.relay import StepsRelay
from src.test_data import INVALID_CONTENT_TOPICS, INVALID_PAYLOADS, SAMPLE_INPUTS, SAMPLE_TIMESTAMPS, VALID_PUBSUB_TOPICS, get_sample_timestamps
from src.test_data import INVALID_CONTENT_TOPICS, INVALID_PAYLOADS, SAMPLE_INPUTS, VALID_PUBSUB_TOPICS, get_sample_timestamps
from src.node.waku_message import WakuMessage
logger = get_custom_logger(__name__)
@ -138,7 +138,7 @@ class TestRelayPublish(StepsRelay):
def test_publish_with_invalid_timestamps(self):
success_timestamps = []
for timestamp in SAMPLE_TIMESTAMPS:
for timestamp in get_sample_timestamps():
if self.node1.type() not in timestamp["valid_for"]:
logger.debug(f'Running test with timestamp {timestamp["description"]}')
message = self.create_message(timestamp=timestamp["value"])