[core][esp32_ble] Add wake_loop_threadsafe() helper for background thread wakeups

This commit is contained in:
J. Nick Koston
2025-11-02 21:48:17 -06:00
parent 4dd3c90663
commit 12077d016d
8 changed files with 220 additions and 148 deletions
+40
View File
@@ -70,3 +70,43 @@ async def test_register_component__with_setup_priority(monkeypatch):
assert add_mock.call_count == 4
app_mock.register_component.assert_called_with(var)
assert core_mock.component_ids == []
def test_require_wake_loop_threadsafe__first_call() -> None:
"""Test that first call sets up define and consumes socket."""
ch.require_wake_loop_threadsafe()
# Verify CORE.data was updated
assert ch.CORE.data[ch.KEY_WAKE_LOOP_THREADSAFE_REQUIRED] is True
# Verify the define was added
assert any(d.name == "USE_WAKE_LOOP_THREADSAFE" for d in ch.CORE.defines)
def test_require_wake_loop_threadsafe__idempotent() -> None:
"""Test that subsequent calls are idempotent."""
# Set up initial state as if already called
ch.CORE.data[ch.KEY_WAKE_LOOP_THREADSAFE_REQUIRED] = True
# Call again - should not raise or fail
ch.require_wake_loop_threadsafe()
# Verify state is still True
assert ch.CORE.data[ch.KEY_WAKE_LOOP_THREADSAFE_REQUIRED] is True
# Define should not be added since flag was already True
assert not any(d.name == "USE_WAKE_LOOP_THREADSAFE" for d in ch.CORE.defines)
def test_require_wake_loop_threadsafe__multiple_calls() -> None:
"""Test that multiple calls only set up once."""
# Call three times
ch.require_wake_loop_threadsafe()
ch.require_wake_loop_threadsafe()
ch.require_wake_loop_threadsafe()
# Verify CORE.data was set
assert ch.CORE.data[ch.KEY_WAKE_LOOP_THREADSAFE_REQUIRED] is True
# Verify the define was added (only once, but we can just check it exists)
assert any(d.name == "USE_WAKE_LOOP_THREADSAFE" for d in ch.CORE.defines)