From 4ee4fb4a73546c238c909d8b462172b7c1c032b3 Mon Sep 17 00:00:00 2001 From: evilchili Date: Sun, 17 Mar 2024 14:44:36 -0700 Subject: [PATCH] adding unit tests of streamer --- croaker/server.py | 4 ++ croaker/streamer.py | 63 ++++++++++--------- test/conftest.py | 6 ++ .../playlists/test_playlist/_theme.mp3 | 1 + test/fixtures/playlists/test_playlist/one.mp3 | 1 + test/fixtures/playlists/test_playlist/two.mp3 | 1 + 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/croaker/server.py b/croaker/server.py index 8488073..7c53912 100644 --- a/croaker/server.py +++ b/croaker/server.py @@ -33,6 +33,8 @@ class RequestHandler(socketserver.StreamRequestHandler): "STFU": " - Terminate the Croaker server." } + should_listen = True + def handle(self): """ Start a command and control session. Commands are read one line at a @@ -69,6 +71,8 @@ class RequestHandler(socketserver.StreamRequestHandler): if not handler: self.send(f"ERR No handler for {cmd}.") handler(args) + if not self.should_listen: + break def send(self, msg): return self.wfile.write(msg.encode() + b"\n") diff --git a/croaker/streamer.py b/croaker/streamer.py index f8b9d10..f0126c7 100644 --- a/croaker/streamer.py +++ b/croaker/streamer.py @@ -45,41 +45,44 @@ class AudioStreamer(threading.Thread): s.audio_info = {shout.SHOUT_AI_BITRATE: "192", shout.SHOUT_AI_SAMPLERATE: "44100", shout.SHOUT_AI_CHANNELS: "5"} return s - def run(self): + def run(self): # pragma: no cover self._shout.open() logger.debug(f"Connnected to shoutcast server at {self._shout.host}:{self._shout.port}") while True: - - # If the user said STOP, clear the queue. - if self.stop_requested.is_set(): - logger.debug("Stop requested; clearing queue.") - self.clear_queue() - self.stop_requested.clear() - - # Check to see if there is a queued request. If there is, play it. - # If there isn't, or if there's a problem playing the request, - # fallback to silence. - not_playing = False - try: - request = self.queue.get(block=False) - logger.debug(f"Received: {request = }") - self.play_file(Path(request.decode())) - except queue.Empty: - logger.debug("Nothing queued; looping silence.") - not_playing = True - except Exception as exc: - logger.error("Caught exception; falling back to silence.", exc_info=exc) - not_playing = True - - if not_playing: - try: - self.silence.seek(0, 0) - self._shout.set_metadata({"song": '[NOTHING PLAYING]'}) - self.play_from_stream(self.silence) - except Exception as exc: - logger.error("Caught exception trying to loop silence!", exc_info=exc) + self.do_one_loop() self._shout.close() + def do_one_loop(self): + + # If the user said STOP, clear the queue. + if self.stop_requested.is_set(): + logger.debug("Stop requested; clearing queue.") + self.clear_queue() + self.stop_requested.clear() + + # Check to see if there is a queued request. If there is, play it. + # If there isn't, or if there's a problem playing the request, + # fallback to silence. + not_playing = False + try: + request = self.queue.get(block=False) + logger.debug(f"Received: {request = }") + self.play_file(Path(request.decode())) + except queue.Empty: + logger.debug("Nothing queued; looping silence.") + not_playing = True + except Exception as exc: + logger.error("Caught exception; falling back to silence.", exc_info=exc) + not_playing = True + + if not_playing: + try: + self.silence.seek(0, 0) + self._shout.set_metadata({"song": '[NOTHING PLAYING]'}) + self.play_from_stream(self.silence) + except Exception as exc: # pragma: no cover + logger.error("Caught exception trying to loop silence!", exc_info=exc) + def clear_queue(self): logger.debug("Clearing queue...") while not self.queue.empty(): diff --git a/test/conftest.py b/test/conftest.py index e9ec787..67df0b0 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -8,3 +8,9 @@ def mock_env(monkeypatch): fixtures = Path(__file__).parent / 'fixtures' monkeypatch.setenv('CROAKER_ROOT', str(fixtures)) monkeypatch.setenv('MEDIA_GLOB', '*.mp3,*.foo,*.bar') + monkeypatch.setenv('ICECAST_URL', 'http://127.0.0.1') + monkeypatch.setenv('ICECAST_HOST', 'localhost') + monkeypatch.setenv('ICECAST_MOUNT', 'mount') + monkeypatch.setenv('ICECAST_PORT', '6523') + monkeypatch.setenv('ICECAST_PASSWORD', 'password') + monkeypatch.setenv('DEBUG', '1') diff --git a/test/fixtures/playlists/test_playlist/_theme.mp3 b/test/fixtures/playlists/test_playlist/_theme.mp3 index e69de29..ab2e4f9 100644 --- a/test/fixtures/playlists/test_playlist/_theme.mp3 +++ b/test/fixtures/playlists/test_playlist/_theme.mp3 @@ -0,0 +1 @@ +_theme.mp3 diff --git a/test/fixtures/playlists/test_playlist/one.mp3 b/test/fixtures/playlists/test_playlist/one.mp3 index e69de29..5ab0fc0 100644 --- a/test/fixtures/playlists/test_playlist/one.mp3 +++ b/test/fixtures/playlists/test_playlist/one.mp3 @@ -0,0 +1 @@ +one.mp3 diff --git a/test/fixtures/playlists/test_playlist/two.mp3 b/test/fixtures/playlists/test_playlist/two.mp3 index e69de29..e6095a7 100644 --- a/test/fixtures/playlists/test_playlist/two.mp3 +++ b/test/fixtures/playlists/test_playlist/two.mp3 @@ -0,0 +1 @@ +two.mp3