2024-03-17 15:15:41 -07:00
|
|
|
import io
|
|
|
|
import queue
|
|
|
|
import threading
|
2024-03-26 00:51:16 -07:00
|
|
|
from pathlib import Path
|
|
|
|
from unittest.mock import MagicMock
|
2024-03-17 15:15:41 -07:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import shout
|
|
|
|
|
2024-03-26 00:51:16 -07:00
|
|
|
from croaker import playlist, streamer
|
2024-03-17 15:15:41 -07:00
|
|
|
|
|
|
|
|
|
|
|
def get_stream_output(stream):
|
|
|
|
return stream.read()
|
|
|
|
|
|
|
|
|
2024-03-26 00:51:16 -07:00
|
|
|
@pytest.fixture(scope="session")
|
2024-03-17 15:15:41 -07:00
|
|
|
def silence_bytes():
|
2024-03-26 00:51:16 -07:00
|
|
|
return (Path(streamer.__file__).parent / "silence.mp3").read_bytes()
|
2024-03-17 15:15:41 -07:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
2024-03-17 15:15:41 -07:00
|
|
|
return mm
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def input_queue():
|
|
|
|
return queue.Queue()
|
|
|
|
|
2024-03-26 00:51:16 -07:00
|
|
|
|
2024-03-17 15:15:41 -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()
|
2024-03-17 15:15:41 -07:00
|
|
|
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()
|
2024-03-17 15:15:41 -07:00
|
|
|
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()
|
2024-03-17 15:15:41 -07:00
|
|
|
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")
|
2024-03-17 15:15:41 -07:00
|
|
|
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
|
2024-03-17 15:15:41 -07:00
|
|
|
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]
|
2024-03-17 15:15:41 -07:00
|
|
|
input_queue.put(bytes(track))
|
2024-04-27 11:47:25 -07:00
|
|
|
audio_streamer.stream_queued_audio()
|
|
|
|
audio_streamer.stream_queued_audio()
|
2024-03-17 15:15:41 -07:00
|
|
|
assert get_stream_output(output_stream) == silence_bytes + track.read_bytes() + silence_bytes
|
|
|
|
|
|
|
|
|
2024-06-02 22:37:14 -07:00
|
|
|
@pytest.mark.skip
|
2024-03-17 15:15:41 -07:00
|
|
|
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]
|
2024-03-17 15:15:41 -07:00
|
|
|
input_queue.put(bytes(track))
|
2024-04-27 11:47:25 -07:00
|
|
|
audio_streamer.stream_queued_audio()
|
2024-03-17 15:15:41 -07:00
|
|
|
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
|
2024-03-17 15:15:41 -07:00
|
|
|
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""
|
2024-03-17 15:15:41 -07:00
|
|
|
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()
|
2024-03-17 15:15:41 -07:00
|
|
|
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""
|
2024-03-17 15:15:41 -07:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
2024-03-17 15:15:41 -07:00
|
|
|
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""
|
2024-03-17 15:15:41 -07:00
|
|
|
assert input_queue.empty
|