dnd-music-console/test/test_streamer.py

133 lines
3.6 KiB
Python
Raw Permalink Normal View History

import io
import queue
import threading
2024-03-26 00:51:16 -07:00
from pathlib import Path
from unittest.mock import MagicMock
import pytest
import shout
2024-03-26 00:51:16 -07:00
from croaker import playlist, streamer
def get_stream_output(stream):
return stream.read()
2024-03-26 00:51:16 -07:00
@pytest.fixture(scope="session")
def silence_bytes():
2024-03-26 00:51:16 -07:00
return (Path(streamer.__file__).parent / "silence.mp3").read_bytes()
@pytest.fixture
def output_stream():
return io.BytesIO()
@pytest.fixture
def mock_shout(output_stream, monkeypatch):
def handle_send(buf):
output_stream.write(buf)
2024-03-26 00:51:16 -07:00
mm = MagicMock(spec=shout.Shout, **{"return_value.send.side_effect": handle_send})
monkeypatch.setattr("shout.Shout", mm)
return mm
@pytest.fixture
def input_queue():
return queue.Queue()
2024-03-26 00:51:16 -07:00
@pytest.fixture
def skip_event():
return threading.Event()
@pytest.fixture
def stop_event():
return threading.Event()
@pytest.fixture
def load_event():
return threading.Event()
@pytest.fixture
def audio_streamer(mock_shout, input_queue, skip_event, stop_event, load_event):
return streamer.AudioStreamer(input_queue, skip_event, stop_event, load_event)
def test_streamer_stop(audio_streamer, stop_event, output_stream):
stop_event.set()
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
assert not stop_event.is_set()
def test_streamer_skip(audio_streamer, skip_event, output_stream):
skip_event.set()
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
assert not skip_event.is_set()
def test_streamer_load(audio_streamer, load_event, output_stream):
load_event.set()
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
assert not load_event.is_set()
def test_clear_queue(audio_streamer, input_queue):
2024-03-26 00:51:16 -07:00
pl = playlist.Playlist(name="test_playlist")
for track in pl.tracks:
input_queue.put(bytes(track))
assert input_queue.not_empty
audio_streamer.clear_queue()
assert input_queue.empty
2024-06-02 22:37:14 -07:00
@pytest.mark.skip
def test_streamer_defaults_to_silence(audio_streamer, input_queue, output_stream, silence_bytes):
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
2024-03-26 00:51:16 -07:00
track = playlist.Playlist(name="test_playlist").tracks[0]
input_queue.put(bytes(track))
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
audio_streamer.stream_queued_audio()
assert get_stream_output(output_stream) == silence_bytes + track.read_bytes() + silence_bytes
2024-06-02 22:37:14 -07:00
@pytest.mark.skip
def test_streamer_plays_silence_on_error(monkeypatch, audio_streamer, input_queue, output_stream, silence_bytes):
2024-04-27 11:47:25 -07:00
monkeypatch.setattr(audio_streamer.queue, "get", MagicMock(side_effect=Exception))
2024-03-26 00:51:16 -07:00
track = playlist.Playlist(name="test_playlist").tracks[0]
input_queue.put(bytes(track))
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
assert get_stream_output(output_stream) == silence_bytes
2024-03-26 00:51:16 -07:00
2024-06-02 22:37:14 -07:00
@pytest.mark.skip
def test_streamer_plays_from_queue(audio_streamer, input_queue, output_stream):
2024-03-26 00:51:16 -07:00
pl = playlist.Playlist(name="test_playlist")
expected = b""
for track in pl.tracks:
input_queue.put(bytes(track))
expected += track.read_bytes()
while not input_queue.empty():
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
assert get_stream_output(output_stream) == expected
def test_streamer_handles_stop_interrupt(audio_streamer, output_stream, stop_event):
stop_event.set()
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
2024-03-26 00:51:16 -07:00
assert get_stream_output(output_stream) == b""
def test_streamer_handles_load_interrupt(audio_streamer, input_queue, output_stream, load_event):
2024-03-26 00:51:16 -07:00
pl = playlist.Playlist(name="test_playlist")
input_queue.put(bytes(pl.tracks[0]))
load_event.set()
2024-04-27 11:47:25 -07:00
audio_streamer.stream_queued_audio()
2024-03-26 00:51:16 -07:00
assert get_stream_output(output_stream) == b""
assert input_queue.empty