Attempt to fix bug where speech could not be interrupted during some race conditions.

This commit is contained in:
Storm Dragon
2026-07-06 12:15:45 -04:00
parent dfef847fb5
commit 3926ed0044
10 changed files with 534 additions and 146 deletions
+79
View File
@@ -0,0 +1,79 @@
import sys
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cthulhu import debug
class DebugStartupTimingTests(unittest.TestCase):
def setUp(self) -> None:
self._debugLevel = debug.debugLevel
self._startupTimingEnabled = debug.startupTimingEnabled
self._startupTimingStartTime = debug._startupTimingStartTime
self._startupTimingLastTime = debug._startupTimingLastTime
debug.debugLevel = debug.LEVEL_SEVERE
debug.startupTimingEnabled = False
debug._startupTimingStartTime = None
debug._startupTimingLastTime = None
def tearDown(self) -> None:
debug.debugLevel = self._debugLevel
debug.startupTimingEnabled = self._startupTimingEnabled
debug._startupTimingStartTime = self._startupTimingStartTime
debug._startupTimingLastTime = self._startupTimingLastTime
def test_startup_timing_is_silent_when_disabled_and_debug_is_not_enabled(self) -> None:
with mock.patch.object(debug, "println") as println:
debug.print_startup_timing("hidden phase")
println.assert_not_called()
def test_startup_timing_logs_when_explicitly_enabled(self) -> None:
with mock.patch.object(debug.time, "monotonic", side_effect=[10.0, 10.25]):
debug.set_startup_timing_enabled(True)
with mock.patch.object(debug, "println") as println:
debug.print_startup_timing("explicit phase")
println.assert_called_once_with(
debug.LEVEL_SEVERE,
"STARTUP TIMING: explicit phase: +0.250s, total 0.250s",
True,
False,
)
def test_startup_timing_logs_when_debug_output_is_enabled(self) -> None:
debug.debugLevel = debug.LEVEL_INFO
with mock.patch.object(debug.time, "monotonic", return_value=20.0):
with mock.patch.object(debug, "println") as println:
debug.print_startup_timing("debug phase")
println.assert_called_once_with(
debug.LEVEL_INFO,
"STARTUP TIMING: debug phase: +0.000s, total 0.000s",
True,
False,
)
def test_startup_timing_context_logs_start_and_end(self) -> None:
with mock.patch.object(debug.time, "monotonic", side_effect=[30.0, 30.1, 30.4]):
debug.set_startup_timing_enabled(True)
with mock.patch.object(debug, "println") as println:
with debug.startup_timing("wrapped phase"):
pass
self.assertEqual(2, println.call_count)
self.assertEqual(
"STARTUP TIMING: wrapped phase start: +0.100s, total 0.100s",
println.call_args_list[0].args[1],
)
self.assertEqual(
"STARTUP TIMING: wrapped phase end: +0.300s, total 0.400s",
println.call_args_list[1].args[1],
)
if __name__ == "__main__":
unittest.main()
+48
View File
@@ -1,10 +1,13 @@
import sys
import threading
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cthulhu import piperfactory
from cthulhu import speechserver
class PiperFactoryRateMappingTests(unittest.TestCase):
@@ -25,6 +28,51 @@ class PiperFactoryRateMappingTests(unittest.TestCase):
self.assertEqual(2.0, self.server._mapRate(-1))
self.assertEqual(0.25, self.server._mapRate(101))
def _make_say_all_server(self):
server = piperfactory.SpeechServer.__new__(piperfactory.SpeechServer)
server._lock = threading.Lock()
server._stopEvent = threading.Event()
server._speakGeneration = 1
server._currentFuture = None
server._executor = mock.Mock()
server._audioPlayer = None
return server
def _make_say_all_context(self, utterance):
return speechserver.SayAllContext(
mock.Mock(),
utterance,
0,
len(utterance),
)
def test_stop_invalidates_pending_say_all_worker(self):
server = self._make_say_all_server()
iterator = iter([[self._make_say_all_context("first"), None]])
callback = mock.Mock()
server.stop()
result = server._sayAllWorker(iterator, callback, 1)
self.assertFalse(result)
callback.assert_not_called()
server._executor.submit.assert_not_called()
def test_stale_say_all_completion_does_not_advance(self):
server = self._make_say_all_server()
first = self._make_say_all_context("first")
second = self._make_say_all_context("second")
iterator = iter([[first, None], [second, None]])
callback = mock.Mock()
server._sayAllWorker(iterator, callback, 1)
onComplete = server._executor.submit.call_args.args[3]
server.stop()
result = onComplete()
self.assertFalse(result)
self.assertEqual(1, server._executor.submit.call_count)
if __name__ == "__main__":
unittest.main()
@@ -7,6 +7,7 @@ from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cthulhu import speechdispatcherfactory
from cthulhu import speechserver
import speechd
@@ -20,8 +21,24 @@ class SpeechDispatcherInterruptRegressionTests(unittest.TestCase):
server._speak = mock.Mock()
server._client = mock.Mock()
server._client.char = mock.Mock()
server._say_all_generation = 0
server._cancelled_say_all_generation = None
server._CALLBACK_TYPE_MAP = {
speechd.CallbackType.BEGIN: speechserver.SayAllContext.PROGRESS,
speechd.CallbackType.CANCEL: speechserver.SayAllContext.INTERRUPTED,
speechd.CallbackType.END: speechserver.SayAllContext.COMPLETED,
speechd.CallbackType.INDEX_MARK: speechserver.SayAllContext.PROGRESS,
}
return server
def _make_say_all_context(self, utterance):
return speechserver.SayAllContext(
mock.Mock(),
utterance,
0,
len(utterance),
)
def test_interrupting_string_speech_cancels_active_output_first(self):
server = self._make_server()
@@ -47,6 +64,41 @@ class SpeechDispatcherInterruptRegressionTests(unittest.TestCase):
server._cancel.assert_not_called()
server._speak.assert_called_once_with("next", None)
def test_stop_invalidates_pending_say_all_continuation(self):
server = self._make_server()
first = self._make_say_all_context("first")
second = self._make_say_all_context("second")
callback = mock.Mock()
iterator = iter([[first, None], [second, None]])
server.sayAll(iterator, callback)
server._say_all(iterator, callback, 1)
speechd_callback = server._speak.call_args.kwargs["callback"]
server.stop()
speechd_callback(speechd.CallbackType.END)
self.assertEqual(1, server._speak.call_count)
callback.assert_not_called()
def test_interrupting_speech_invalidates_pending_say_all_continuation(self):
server = self._make_server()
first = self._make_say_all_context("first")
second = self._make_say_all_context("second")
callback = mock.Mock()
iterator = iter([[first, None], [second, None]])
server.sayAll(iterator, callback)
server._say_all(iterator, callback, 1)
speechd_callback = server._speak.call_args.kwargs["callback"]
server.speak("new speech", interrupt=True)
speechd_callback(speechd.CallbackType.END)
self.assertEqual(2, server._speak.call_count)
self.assertEqual("new speech", server._speak.call_args.args[0])
callback.assert_not_called()
def test_send_command_logs_and_recovers_from_command_errors(self):
server = self._make_server()
server.reset = mock.Mock()