adding unit tests of streamer

This commit is contained in:
evilchili 2024-03-17 14:44:36 -07:00
parent a5cf97870b
commit 4ee4fb4a73
6 changed files with 46 additions and 30 deletions

View File

@ -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")

View File

@ -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():

View File

@ -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')

View File

@ -0,0 +1 @@
_theme.mp3

View File

@ -0,0 +1 @@
one.mp3

View File

@ -0,0 +1 @@
two.mp3