fix: start each schedule item at its planned time

Start played the first item right away, although the plan begins at
the next full hour. The news was at :00 and :30 in the plan but not on
the real clock.

PlaybackEngine now takes a clock and waits before each item until its
planned start. An item that runs late is never cut; the next one then
starts right after it. When the broadcast starts after the plan has
begun, items that should already have started are skipped and playback
joins at the next one.

While the engine waits for the first item, the now-playing card shows
"On air at" with the planned time. Starting a plan that is already
over is refused with a message.

Refs #12

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
fraujulianandClaude Opus 5.5 committed 2026-11-23 11:20:10 +01:00
1 parent 6f3ce85307
commit 497ac8fd6c
6 files changed
+127 -17

No files matched your search

+30 -2
View File
@@ -65,7 +65,7 @@ public class PlaybackEngineTests
private static (PlaybackEngine Engine, List<int> Started, TaskCompletionSource Finished) Create(FakePlayer player, FakeClock clock)
{
var engine = new PlaybackEngine(player, clock.Delay);
var engine = new PlaybackEngine(player, clock.Delay, () => Start);
var started = new List<int>();
var finished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
engine.ItemStarted += (_, e) => { lock (started) started.Add(e.Index); };
@@ -172,7 +172,7 @@ public class PlaybackEngineTests
if (first) { first = false; return news.Task.WaitAsync(t); }
return d > TimeSpan.FromSeconds(180) ? Task.Delay(Timeout.Infinite, t) : Task.CompletedTask;
}
var engine = new PlaybackEngine(player, Delay);
var engine = new PlaybackEngine(player, Delay, () => Start);
var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var finished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
engine.ItemStarted += (_, _) => started.TrySetResult();
@@ -188,4 +188,32 @@ public class PlaybackEngineTests
await finished.Task.WaitAsync(Wait);
Assert.Equal(["request.mp3", "b.mp3"], player.Played);
}
[Fact]
public async Task Start_SkipsItemsThatShouldHaveStartedEarlierAndWaitsForTheNext()
{
var player = new FakePlayer();
var clock = new FakeClock();
IReadOnlyList<ScheduleItem> timed =
[
Plan[0],
Plan[1] with { Start = Start.AddSeconds(300) },
Plan[2] with { Start = Start.AddSeconds(500) },
Plan[3] with { Start = Start.AddSeconds(545) },
Plan[4] with { Start = Start.AddSeconds(605) },
];
var engine = new PlaybackEngine(player, clock.Delay, () => Start.AddSeconds(360));
var started = new List<int>();
var finished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
engine.ItemStarted += (_, e) => started.Add(e.Index);
engine.Finished += (_, _) => finished.TrySetResult();
engine.Start(timed);
await finished.Task.WaitAsync(Wait);
// News and song a already began at 14:06; the ad waits until its planned 14:08:20.
Assert.Equal([2, 3, 4], started);
Assert.Equal(["b.mp3"], player.Played);
Assert.Equal(TimeSpan.FromSeconds(140), clock.Delays.First());
}}
@@ -29,7 +29,7 @@ public sealed class ScheduleControllerTests : IDisposable
private ScheduleController Create(Func<TimeSpan, CancellationToken, Task>? delay = null) =>
new(_songs,
new ScheduleGenerator(new ScheduleOptions(), new Random(7)),
new PlaybackEngine(new InstantPlayer(), delay ?? ((_, _) => Task.CompletedTask)),
new PlaybackEngine(new InstantPlayer(), delay ?? ((_, _) => Task.CompletedTask), () => _now),
() => _now);
private void AddSongs()
@@ -101,6 +101,7 @@ public sealed class ScheduleControllerTests : IDisposable
var first = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
controller.ItemStarted += (_, _) => first.TrySetResult();
_now = controller.Plan[0].Start; // on air at the planned start, so the held delay is the news
controller.Start();
await first.Task.WaitAsync(TimeSpan.FromSeconds(5));
_now = _now.AddMinutes(2);
@@ -140,6 +141,7 @@ public sealed class ScheduleControllerTests : IDisposable
controller.ItemStarted += (_, _) => first.TrySetResult();
var request = _songs.GetAll()[3];
_now = controller.Plan[0].Start; // on air at the planned start, so the held delay is the news
controller.Start();
await first.Task.WaitAsync(TimeSpan.FromSeconds(5));
@@ -159,4 +161,34 @@ public sealed class ScheduleControllerTests : IDisposable
controller.CreatePlan(1);
Assert.Throws<ScheduleException>(() => controller.Swap(1, 12345));
}
[Fact]
public async Task Start_WaitsForThePlannedStart()
{
AddSongs();
var hold = new TaskCompletionSource();
var controller = Create((_, token) => hold.Task.WaitAsync(token));
controller.CreatePlan(1);
controller.Start(); // at 14:37:12, the plan starts at 15:00
var deadline = DateTime.UtcNow.AddSeconds(5);
while (controller.WaitingUntil is null && DateTime.UtcNow < deadline) await Task.Delay(10);
Assert.Equal(new DateTime(2026, 11, 23, 15, 0, 0), controller.WaitingUntil);
Assert.True(controller.IsPlaying);
Assert.Null(controller.CurrentItem);
controller.Stop();
Assert.Null(controller.WaitingUntil);
}
[Fact]
public void Start_RejectsAPlanThatIsOver()
{
AddSongs();
var controller = Create();
controller.CreatePlan(1);
_now = controller.Plan[^1].Start;
Assert.Throws<InvalidOperationException>(() => controller.Start());
}}