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:
1 parent
6f3ce85307
commit
497ac8fd6c
6 files changed
+127
-17
No files matched your search
@@ -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());
|
||||
}}
|
||||
Reference in new issue
Block a user