RemiFabre commited on
Commit
091c62e
·
1 Parent(s): 1bd30d1

First iteration of pytests, targetting the headwobbler race condition

Browse files
tests/audio/test_head_wobbler.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import math
3
+ import threading
4
+ import time
5
+ from typing import Callable, List, Tuple
6
+
7
+ import numpy as np
8
+
9
+ from reachy_mini_conversation_demo.audio.head_wobbler import HeadWobbler
10
+
11
+
12
+ def _make_audio_chunk(duration_s: float = 0.3, frequency_hz: float = 220.0) -> str:
13
+ """Generate a base64-encoded mono PCM16 sine wave."""
14
+ sample_rate = 24000
15
+ sample_count = int(sample_rate * duration_s)
16
+ t = np.linspace(0, duration_s, sample_count, endpoint=False)
17
+ wave = 0.6 * np.sin(2 * math.pi * frequency_hz * t)
18
+ pcm = np.clip(wave * np.iinfo(np.int16).max, -32768, 32767).astype(np.int16)
19
+ return base64.b64encode(pcm.tobytes()).decode("ascii")
20
+
21
+
22
+ def _wait_for(predicate: Callable[[], bool], timeout: float = 0.6) -> bool:
23
+ """Poll `predicate` until true or timeout."""
24
+ end_time = time.time() + timeout
25
+ while time.time() < end_time:
26
+ if predicate():
27
+ return True
28
+ time.sleep(0.01)
29
+ return False
30
+
31
+
32
+ def _start_wobbler() -> Tuple[HeadWobbler, List[Tuple[float, Tuple[float, float, float, float, float, float]]]]:
33
+ captured: List[Tuple[float, Tuple[float, float, float, float, float, float]]] = []
34
+
35
+ def capture(offsets: Tuple[float, float, float, float, float, float]) -> None:
36
+ captured.append((time.time(), offsets))
37
+
38
+ wobbler = HeadWobbler(set_speech_offsets=capture)
39
+ wobbler.start()
40
+ return wobbler, captured
41
+
42
+
43
+ def test_reset_drops_pending_offsets() -> None:
44
+ wobbler, captured = _start_wobbler()
45
+ try:
46
+ wobbler.feed(_make_audio_chunk(duration_s=0.35))
47
+ assert _wait_for(lambda: len(captured) > 0), "wobbler did not emit initial offsets"
48
+
49
+ pre_reset_count = len(captured)
50
+ wobbler.reset()
51
+ time.sleep(0.3)
52
+ assert len(captured) == pre_reset_count, "offsets continued after reset without new audio"
53
+ finally:
54
+ wobbler.stop()
55
+
56
+
57
+ def test_reset_allows_future_offsets() -> None:
58
+ wobbler, captured = _start_wobbler()
59
+ try:
60
+ wobbler.feed(_make_audio_chunk(duration_s=0.35))
61
+ assert _wait_for(lambda: len(captured) > 0), "wobbler did not emit initial offsets"
62
+
63
+ wobbler.reset()
64
+ pre_second_count = len(captured)
65
+
66
+ wobbler.feed(_make_audio_chunk(duration_s=0.35, frequency_hz=440.0))
67
+ assert _wait_for(lambda: len(captured) > pre_second_count), "no offsets after reset"
68
+ assert wobbler._thread is not None and wobbler._thread.is_alive()
69
+ finally:
70
+ wobbler.stop()
71
+
72
+
73
+ def test_reset_during_inflight_chunk_keeps_worker(monkeypatch) -> None:
74
+ wobbler, captured = _start_wobbler()
75
+ ready = threading.Event()
76
+ release = threading.Event()
77
+
78
+ original_feed = wobbler.sway.feed
79
+
80
+ def blocking_feed(pcm, sr): # type: ignore[no-untyped-def]
81
+ ready.set()
82
+ release.wait(timeout=2.0)
83
+ return original_feed(pcm, sr)
84
+
85
+ monkeypatch.setattr(wobbler.sway, "feed", blocking_feed)
86
+
87
+ try:
88
+ wobbler.feed(_make_audio_chunk(duration_s=0.35))
89
+ assert ready.wait(timeout=1.0), "worker thread did not dequeue audio"
90
+
91
+ wobbler.reset()
92
+ release.set()
93
+
94
+ # Allow the worker to finish processing the first chunk (which should be discarded)
95
+ time.sleep(0.1)
96
+
97
+ assert wobbler._thread is not None and wobbler._thread.is_alive(), "worker thread died after reset"
98
+
99
+ pre_second = len(captured)
100
+ wobbler.feed(_make_audio_chunk(duration_s=0.35, frequency_hz=440.0))
101
+ assert _wait_for(lambda: len(captured) > pre_second), "no offsets emitted after in-flight reset"
102
+ assert wobbler._thread.is_alive()
103
+ finally:
104
+ wobbler.stop()
tests/conftest.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ """Pytest configuration for path setup."""
2
+
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ PROJECT_ROOT = Path(__file__).resolve().parents[1]
7
+ SRC_PATH = PROJECT_ROOT / "src"
8
+ if str(SRC_PATH) not in sys.path:
9
+ sys.path.insert(0, str(SRC_PATH))