Skip to content
Snippets Groups Projects
Commit 75c26c4a authored by David Trattnig's avatar David Trattnig
Browse files

test: play active item, add command + engine mocks

parent a56f2e91
No related branches found
No related tags found
1 merge request!35ORM-less scheduling
......@@ -17,17 +17,164 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import inspect
import time
import unittest
from aura_engine.base.config import AuraConfig
from aura_engine.base.utils import SimpleUtil as SU
from aura_engine.control import EngineExecutor
from aura_engine.core.channels import GenericChannel
from aura_engine.engine import Engine
from aura_engine.scheduling.domain import Playlist, PlaylistItem, Timeslot
from aura_engine.scheduling.scheduler import AuraScheduler
from aura_engine.scheduling.timetable import TimetableService
class CommandRegistry:
commands: {} = {}
def register(f, cmd: EngineExecutor):
if not CommandRegistry.commands.get(f):
CommandRegistry.commands[f] = []
CommandRegistry.commands.get(f).append(cmd)
def get(f):
return CommandRegistry.commands.get(f)
def reset(f):
c: EngineExecutor
for c in CommandRegistry.commands.get(f):
c.cancel()
CommandRegistry.commands[f] = None
def prune():
for key in CommandRegistry.commands.keys():
CommandRegistry.reset(key)
#
# Mocks
#
class MockedPlayer:
"""
Mocked version of engine.
"""
def roll(channel, seconds_to_roll: int):
print(f"called pre-rolling for {channel} seconds on player channel {seconds_to_roll}")
pass
class MockedEngine:
"""
Mocked version of engine.
"""
scheduler: AuraScheduler
player: MockedPlayer
def __init__(self):
"""
Init.
"""
self.scheduler = None
self.player = MockedPlayer
class MockedTimeslotCommand:
"""
Command for triggering start and end of timeslot events.
"""
engine: Engine = None
config: AuraConfig = None
timeslot: Timeslot
def __init__(self, engine: Engine, timeslot: Timeslot):
"""
Initialize the timeslot command.
Args:
engine (Engine): The engine
timeslot (Timeslot): The timeslot which is starting at this time
"""
self.config = AuraConfig()
self.engine = engine
caller = inspect.stack()[2].function
CommandRegistry.register(caller, self)
print(f"Created instance of MockedTimeslotCommand from {caller}")
self.timeslot = timeslot
self.do_start_timeslot(timeslot)
self.do_end_timeslot(timeslot)
def do_start_timeslot(self, timeslot: Timeslot):
"""
Indicate the start of the timeslot by sending a `on_timeslot_start` event.
"""
caller = inspect.stack()[2].function
print(f"Called do_play of MockedTimeslotCommand from {caller}")
def do_end_timeslot(self, timeslot: Timeslot):
"""
Indicate the start of the timeslot by sending a `on_timeslot_end` event.
Also resetting the used channel.
"""
caller = inspect.stack()[2].function
print(f"Called do_play of MockedTimeslotCommand from {caller}")
class MockedPlayCommand:
"""
Mocked command for triggering timed preloading and playing.
"""
engine: Engine = None
config: AuraConfig = None
items: [PlaylistItem]
def __init__(self, engine: Engine, items: [PlaylistItem]):
"""
Initialize the play command.
Args:
engine (Engine): The engine
items ([PlaylistItem]): One or more playlist items to be started
"""
self.config = AuraConfig()
self.engine = engine
caller = inspect.stack()[2].function
CommandRegistry.register(caller, self)
print(f"Created instance of MockedPlayCommand from {caller}")
self.items = items
self.do_preload(items)
self.do_play(items)
def do_preload(self, items: [PlaylistItem]):
"""
Preload the items.
Args:
items ([PlaylistItem]): The set of playlist items to be pre-loaded.
"""
caller = inspect.stack()[2].function
print(f"Called do_preload of MockedPlayCommand from {caller}")
items[0].play_channel = GenericChannel(1, "main channel", None)
def do_play(self, items: [PlaylistItem]):
"""
Play the items.
Args:
items ([PlaylistItem]): The set of playlist items to be played.
"""
caller = inspect.stack()[2].function
print(f"Called do_play of MockedPlayCommand from {caller}")
class TestSchedulingScheduler(unittest.TestCase):
"""
Testing the scheduling utils.
......@@ -42,6 +189,10 @@ class TestSchedulingScheduler(unittest.TestCase):
self.config = AuraConfig()
self.engine = Engine.get_instance()
# Init mocked command classes
AuraScheduler.TimeslotCommandClass = MockedTimeslotCommand
AuraScheduler.PlayCommandClass = MockedPlayCommand
def tearDown(self):
pass
......@@ -56,25 +207,28 @@ class TestSchedulingScheduler(unittest.TestCase):
scheduler.timetable.timetable_file = f.replace("timetable.json", "timetable-test.json")
scheduler.timetable.delete_timetable_file()
# Prune command registry
CommandRegistry.prune()
# Build some timetable
now = SU.timestamp()
ts1 = Timeslot(
id=1, repetition_id=None, start=now - 500, end=now - 400, show=None, episode=None
)
ts2 = Timeslot(
id=1, repetition_id=None, start=now - 400, end=now + 400, show=None, episode=None
id=2, repetition_id=None, start=now - 400, end=now + 400, show=None, episode=None
)
ts3 = Timeslot(
id=1, repetition_id=None, start=now + 400, end=now + 540, show=None, episode=None
id=3, repetition_id=None, start=now + 400, end=now + 540, show=None, episode=None
)
ts4 = Timeslot(
id=1, repetition_id=None, start=now + 540, end=now + 720, show=None, episode=None
id=4, repetition_id=None, start=now + 540, end=now + 720, show=None, episode=None
)
pl = Playlist("9999", "Playlist XYZ", False)
alpha = PlaylistItem("alpha.flac", 100, 100, None)
beta = PlaylistItem("beta.flac", 600, 100, None)
gamma = PlaylistItem("gamma.flac", 100, 100, None)
alpha = PlaylistItem("file://alpha.flac", 100, 100, None)
beta = PlaylistItem("file://beta.flac", 600, 100, None)
gamma = PlaylistItem("file://gamma.flac", 100, 100, None)
pl.add(alpha)
pl.add(beta)
pl.add(gamma)
......@@ -86,7 +240,7 @@ class TestSchedulingScheduler(unittest.TestCase):
# Test Cases
#
def test_start_and_terminate_scheduler(self):
def xxx_test_start_and_terminate_scheduler(self):
print(self._testMethodName)
# Boot the scheduler
......@@ -105,7 +259,7 @@ class TestSchedulingScheduler(unittest.TestCase):
print(self._testMethodName)
# Construct the scheduler
scheduler = AuraScheduler(self.engine)
scheduler = AuraScheduler(MockedEngine())
scheduler.timetable.delete_timetable_file()
# Build some timetable
......@@ -113,7 +267,27 @@ class TestSchedulingScheduler(unittest.TestCase):
tt = scheduler.get_timetable()
self.assertIsNotNone(tt)
self.assertEquals(4, len(tt.timetable))
self.assertEqual(4, len(tt.timetable))
def test_play_active_item(self):
print(self._testMethodName)
# Construct the scheduler
scheduler = AuraScheduler(MockedEngine())
scheduler.timetable.delete_timetable_file()
# Build some timetable
scheduler.timetable.timetable = self.build_timetable(scheduler)
is_playing = scheduler.play_active_item()
cmds = CommandRegistry.get("test_play_active_item")
self.assertEqual(2, len(cmds))
timeslot_cmd: MockedTimeslotCommand = cmds[0]
play_cmd: MockedPlayCommand = cmds[1]
self.assertEqual(2, timeslot_cmd.timeslot.get_id())
self.assertEqual(1, len(play_cmd.items))
channel: GenericChannel = play_cmd.items[0].play_channel
self.assertEqual("main channel", channel.name)
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment