Working on tests

This commit is contained in:
Michael Hansen
2024-01-09 10:59:28 -06:00
parent f74071666a
commit 172a85846c
5 changed files with 134 additions and 0 deletions
+1
View File
@@ -3,3 +3,4 @@ flake8==6.0.0
isort==5.11.3
mypy==0.991
pylint==2.15.9
pytest==7.4.3
Executable
+13
View File
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
import subprocess
import sys
import venv
from pathlib import Path
_DIR = Path(__file__).parent
_PROGRAM_DIR = _DIR.parent
_VENV_DIR = _PROGRAM_DIR / ".venv"
_TEST_DIR = _PROGRAM_DIR / "tests"
context = venv.EnvBuilder().ensure_directories(_VENV_DIR)
subprocess.check_call([context.env_exe, "-m", "pytest", _TEST_DIR] + sys.argv[1:])
+1
View File
@@ -0,0 +1 @@
"""Wyoming satellite tests."""
+17
View File
@@ -0,0 +1,17 @@
"""Shared code for Wyoming satellite tests."""
from wyoming.info import Info, Attribution, Satellite
from wyoming.audio import AudioChunk
WYOMING_INFO = Info(
satellite=Satellite(
name="test satellite",
area="test area",
description="test description",
attribution=Attribution(name="", url=""),
installed=True,
)
)
AUDIO_CHUNK = AudioChunk(
rate=16000, width=2, channels=1, audio=bytes([255] * 960) # 30ms
)
+102
View File
@@ -0,0 +1,102 @@
import asyncio
import logging
import os
from pathlib import Path
import pytest
from wyoming.audio import AudioChunk
from wyoming.wake import Detection
from wyoming.client import AsyncClient
from wyoming.event import Event, async_write_event
from wyoming.server import AsyncServer, AsyncEventHandler
from wyoming_satellite import (
AlwaysStreamingSatellite,
EventSettings,
SatelliteSettings,
MicSettings,
WakeSettings,
)
from .shared import WYOMING_INFO, AUDIO_CHUNK
_LOGGER = logging.getLogger()
class MicEventHandler(AsyncEventHandler):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._is_connected = True
self._task = asyncio.create_task(self.stream_audio())
async def handle_event(self, event: Event) -> bool:
"""Output only."""
return True
async def stream_audio(self) -> None:
while self._is_connected:
await self.write_event(AUDIO_CHUNK.event())
await asyncio.sleep(AUDIO_CHUNK.seconds)
async def disconnect(self) -> None:
self._is_connected = False
await self._task
class WakeEventHandler(AsyncEventHandler):
async def handle_event(self, event: Event) -> bool:
_LOGGER.error(event)
if AudioChunk.is_type(event.type):
await self.write_event(Detection().event())
return True
@pytest.mark.asyncio
async def test_multiple_wakeups(tmp_path: Path) -> None:
mic_socket = tmp_path / "mic.socket"
os.mkfifo(mic_socket)
mic_uri = f"unix://{mic_socket}"
mic_server = AsyncServer.from_uri(mic_uri)
mic_task = asyncio.create_task(mic_server.run(MicEventHandler))
wake_socket = tmp_path / "wake.socket"
os.mkfifo(wake_socket)
wake_uri = f"unix://{wake_socket}"
wake_server = AsyncServer.from_uri(wake_uri)
wake_task = asyncio.create_task(wake_server.run(WakeEventHandler))
wake_event = asyncio.Event()
class EventEventHandler(AsyncEventHandler):
async def handle_event(self, event: Event) -> bool:
if Detection.is_type(event.type):
wake_event.set()
return True
event_socket = tmp_path / "event.socket"
os.mkfifo(event_socket)
event_uri = f"unix://{event_socket}"
event_server = AsyncServer.from_uri(event_uri)
event_task = asyncio.create_task(event_server.run(EventEventHandler))
satellite = AlwaysStreamingSatellite(
SatelliteSettings(
mic=MicSettings(uri=mic_uri),
wake=WakeSettings(uri=wake_uri),
event=EventSettings(uri=event_uri),
)
)
satellite_task = asyncio.create_task(satellite.run())
await asyncio.sleep(0.5)
# async with asyncio.timeout(1):
# await wake_event.wait()
# await satellite.stop()
tasks = [mic_task, wake_task, event_task]
for task in tasks:
task.cancel()
await asyncio.wait(tasks)