Skip to content

Commit

Permalink
start adding end2end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed May 11, 2024
1 parent 89e10d7 commit 39d7015
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ovos_audio/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ovos_utils.log import LOG
from ovos_utils.process_utils import MonotonicEvent
from ovos_audio.utils import validate_message_context

from ovos_utils.ocp import MediaState
try:
from ovos_plugin_common_play import OCPAudioBackend
except ImportError:
Expand Down Expand Up @@ -92,9 +92,10 @@ def load_services(self):
local = []
remote = []
for plugin_name, plugin_module in found_plugins.items():
LOG.info(f'Loading audio service plugin: {plugin_name}')
LOG.info(f'Found audio service plugin: {plugin_name}')
s = setup_audio_service(plugin_module, config=self.config, bus=self.bus)
if not s:
LOG.info(f"{plugin_name} not loaded! config: {self.config}")
continue
if isinstance(s, RemoteAudioBackend):
remote += s
Expand Down Expand Up @@ -346,6 +347,8 @@ def play(self, tracks, prefered_service, repeat=False):
break
else:
LOG.info('No service found for uri_type: ' + uri_type)
self.bus.emit(Message("ovos.common_play.media.state",
{"state": MediaState.INVALID_MEDIA}))
return
if not selected_service.supports_mime_hints:
tracks = [t[0] if isinstance(t, list) else t for t in tracks]
Expand Down
75 changes: 75 additions & 0 deletions test/unittests/test_end2end.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import unittest
import unittest.mock as mock
import time
from shutil import rmtree
from threading import Thread
from time import sleep

from os.path import exists
from ovos_utils.fakebus import FakeBus
from ovos_audio.service import AudioService
from ovos_bus_client.message import Message
from ovos_utils.ocp import MediaState


class TestLegacy(unittest.TestCase):
def setUp(self):
self.core = AudioService(FakeBus(), disable_ocp=True, autoload=False)
self.core.config['default-backend'] = "simple"
self.core.load_services()

def test_uri_error(self):

messages = []

def new_msg(msg):
nonlocal messages
m = Message.deserialize(msg)
messages.append(m)
print(len(messages), msg)

def wait_for_n_messages(n):
nonlocal messages
t = time.time()
while len(messages) < n:
sleep(0.1)
if time.time() - t > 10:
raise RuntimeError("did not get the number of expected messages under 10 seconds")

self.core.bus.on("message", new_msg)

utt = Message('mycroft.audio.service.play',
{"tracks": ["bad_uri://fake.mp3"]},
{})
self.core.bus.emit(utt)

# confirm all expected messages are sent
expected_messages = [
'mycroft.audio.service.play',
"ovos.common_play.media.state" # invalid media
]
wait_for_n_messages(len(expected_messages))

self.assertEqual(len(expected_messages), len(messages))

for idx, m in enumerate(messages):
self.assertEqual(m.msg_type, expected_messages[idx])

state = messages[-1]
self.assertEqual(state.data["state"], MediaState.INVALID_MEDIA)


if __name__ == "__main__":
unittest.main()

0 comments on commit 39d7015

Please sign in to comment.