Skip to content

Commit 39f435d

Browse files
fix: fixed tests for stable one.
1 parent 9e37142 commit 39f435d

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

tests/test_orchestrator_rates.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@ def test_per_sensor_rate_hz_controls_sampling_frequency():
77
devices_cfg = [{
88
"id": "A",
99
"sensors": [
10-
{"kind": "temp", "count": 1, "priority": 5, "rate_hz": 5}, # every 0.2s
11-
{"kind": "vibration", "count": 1, "priority": 5, "rate_hz": 10}, # every 0.1s
10+
{"kind": "temp", "count": 1, "priority": 5, "rate_hz": 5}, # ~every 0.2s
11+
{"kind": "vibration", "count": 1, "priority": 5, "rate_hz": 10}, # ~every 0.1s
1212
]
1313
}]
1414

1515
run_stream(
1616
spec_str="device=A: temp*1,vibration*1",
17-
rate_hz=10.0, # global tick
17+
rate_hz=10.0, # global tick
1818
duration_s=None,
19-
total_count=20, # ~2.0s
19+
total_count=20, # ~2.0s
2020
writer_for_type={"*": cap.router({"*": cap.writer_for_type("*")})},
2121
partition_by="none",
2222
devices_cfg=devices_cfg,
2323
)
2424

25-
n_temp = len([r for r in cap.per_type["*"] if r["type"] == "temp"]) \
26-
if "*" in cap.per_type else len([r for r in cap.per_type.get("temp", [])])
27-
n_vib = len([r for r in cap.per_type["*"] if r["type"] == "vibration"]) \
28-
if "*" in cap.per_type else len([r for r in cap.per_type.get("vibration", [])])
25+
# Count how many samples we collected for each type
26+
all_recs = cap.per_type.get("*")
27+
if all_recs is not None:
28+
n_temp = sum(1 for r in all_recs if r["type"] == "temp")
29+
n_vib = sum(1 for r in all_recs if r["type"] == "vibration")
30+
else:
31+
n_temp = len(cap.per_type.get("temp", []))
32+
n_vib = len(cap.per_type.get("vibration", []))
2933

30-
# vibration should have noticeably more samples than temp (timing tolerance)
31-
assert n_vib >= n_temp + 5, f"expected more vib samples than temp; got temp={n_temp}, vib={n_vib}"
34+
# Expect significantly more vibration samples (10 Hz) than temperature (5 Hz).
35+
# Leave tolerance for CI timing jitter: require at least 1.5× more vib or +3 samples.
36+
assert n_vib >= max(int(1.5 * n_temp), n_temp + 3), \
37+
f"expected clearly more vib samples than temp; got temp={n_temp}, vib={n_vib}"

tests/test_vibration.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1+
from __future__ import annotations
2+
import math
13
from dummysensors import VibrationSensor
4+
25
def test_vibration_basic():
3-
s = VibrationSensor(base_hz=10.0, amp=1.0)
4-
xs = [s.read(t_s=i*0.01) for i in range(100)]
5-
assert max(xs) <= 1.0 + 1e-9 and min(xs) >= -1.0 - 1e-9
6+
import math
7+
amp = 1.0
8+
vib = VibrationSensor(
9+
base_hz=8.0,
10+
amp=amp,
11+
noise_theta=2.0,
12+
noise_sigma=0.05,
13+
spike_prob=0.0, # ← disable spikes for a stable test
14+
spike_scale=4.0,
15+
)
16+
dt = 0.001
17+
n = 1500
18+
vals = [vib.read(i * dt) for i in range(n)]
19+
20+
assert all(math.isfinite(v) for v in vals)
21+
mean = sum(vals) / len(vals)
22+
assert abs(mean) < 0.2
23+
24+
var = sum((v - mean) ** 2 for v in vals) / len(vals)
25+
std = math.sqrt(var)
26+
assert 0.4 <= std <= 1.2
27+
28+
# allow for OU noise, but not spikes
29+
mx, mn = max(vals), min(vals)
30+
max_allowed = amp + 4 * 0.05
31+
min_allowed = -amp - 4 * 0.05
32+
assert mn >= min_allowed and mx <= max_allowed, (
33+
f"values exceeded expected bounds: "
34+
f"min={mn}, max={mx}, allowed=({min_allowed}, {max_allowed})"
35+
)

0 commit comments

Comments
 (0)