205 lines
6.9 KiB
Go
205 lines
6.9 KiB
Go
package playback
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNewPlaylistTimingResetsState(t *testing.T) {
|
|
got := NewPlaylistTiming(7, 10*time.Second)
|
|
want := PlaylistTimingState{
|
|
Revision: 7,
|
|
Duration: 10 * time.Second,
|
|
Remaining: 10 * time.Second,
|
|
}
|
|
if got != want {
|
|
t.Fatalf("NewPlaylistTiming() = %#v, want %#v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestStartPlaylistTiming(t *testing.T) {
|
|
now := time.Date(2026, time.September, 1, 12, 0, 0, 0, time.UTC)
|
|
current := NewPlaylistTiming(4, 10*time.Second)
|
|
|
|
got, started := StartPlaylistTiming(current, 4, now)
|
|
if !started {
|
|
t.Fatal("StartPlaylistTiming() started = false, want true")
|
|
}
|
|
want := PlaylistTimingState{
|
|
Revision: 4,
|
|
Duration: 10 * time.Second,
|
|
Ready: true,
|
|
Started: true,
|
|
Remaining: 10 * time.Second,
|
|
Deadline: now.Add(10 * time.Second),
|
|
}
|
|
if got != want {
|
|
t.Fatalf("StartPlaylistTiming() = %#v, want %#v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestStartPlaylistTimingIgnoresInapplicableReadiness(t *testing.T) {
|
|
now := time.Date(2026, time.September, 1, 12, 0, 0, 0, time.UTC)
|
|
started, ok := StartPlaylistTiming(NewPlaylistTiming(4, time.Second), 4, now)
|
|
if !ok {
|
|
t.Fatal("initial StartPlaylistTiming() did not start")
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
current PlaylistTimingState
|
|
revision uint64
|
|
}{
|
|
{name: "zero duration", current: NewPlaylistTiming(4, 0), revision: 4},
|
|
{name: "stale revision", current: NewPlaylistTiming(4, time.Second), revision: 3},
|
|
{name: "future revision", current: NewPlaylistTiming(4, time.Second), revision: 5},
|
|
{name: "already started", current: started, revision: 4},
|
|
{name: "paused", current: PlaylistTimingState{Revision: 4, Duration: time.Second, Paused: true, Remaining: time.Second}, revision: 4},
|
|
{name: "expired", current: PlaylistTimingState{Revision: 4, Duration: time.Second, Expired: true}, revision: 4},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got, changed := StartPlaylistTiming(test.current, test.revision, now.Add(time.Second))
|
|
if changed {
|
|
t.Fatal("StartPlaylistTiming() changed = true, want false")
|
|
}
|
|
if got != test.current {
|
|
t.Fatalf("StartPlaylistTiming() = %#v, want unchanged %#v", got, test.current)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExpirePlaylistTiming(t *testing.T) {
|
|
now := time.Date(2026, time.September, 1, 12, 0, 0, 0, time.UTC)
|
|
current, _ := StartPlaylistTiming(NewPlaylistTiming(9, 5*time.Second), 9, now)
|
|
|
|
got, expired := ExpirePlaylistTiming(current, 9, now.Add(5*time.Second))
|
|
if !expired {
|
|
t.Fatal("ExpirePlaylistTiming() expired = false, want true")
|
|
}
|
|
want := PlaylistTimingState{
|
|
Revision: 9,
|
|
Duration: 5 * time.Second,
|
|
Ready: true,
|
|
Expired: true,
|
|
Remaining: 0,
|
|
}
|
|
if got != want {
|
|
t.Fatalf("ExpirePlaylistTiming() = %#v, want %#v", got, want)
|
|
}
|
|
|
|
again, expired := ExpirePlaylistTiming(got, 9, now.Add(6*time.Second))
|
|
if expired || again != got {
|
|
t.Fatalf("duplicate ExpirePlaylistTiming() = %#v, %v; want unchanged, false", again, expired)
|
|
}
|
|
}
|
|
|
|
func TestExpirePlaylistTimingIgnoresInapplicableEvents(t *testing.T) {
|
|
now := time.Date(2026, time.September, 1, 12, 0, 0, 0, time.UTC)
|
|
current, _ := StartPlaylistTiming(NewPlaylistTiming(4, 10*time.Second), 4, now)
|
|
tests := []struct {
|
|
name string
|
|
revision uint64
|
|
at time.Time
|
|
}{
|
|
{name: "early", revision: 4, at: now.Add(9 * time.Second)},
|
|
{name: "stale revision", revision: 3, at: now.Add(10 * time.Second)},
|
|
{name: "future revision", revision: 5, at: now.Add(10 * time.Second)},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got, expired := ExpirePlaylistTiming(current, test.revision, test.at)
|
|
if expired {
|
|
t.Fatal("ExpirePlaylistTiming() expired = true, want false")
|
|
}
|
|
if got != current {
|
|
t.Fatalf("ExpirePlaylistTiming() = %#v, want unchanged %#v", got, current)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewPlaylistTimingInvalidatesPreviousDeadline(t *testing.T) {
|
|
now := time.Date(2026, time.September, 1, 12, 0, 0, 0, time.UTC)
|
|
old, _ := StartPlaylistTiming(NewPlaylistTiming(1, time.Second), 1, now)
|
|
current := NewPlaylistTiming(2, 2*time.Second)
|
|
|
|
got, expired := ExpirePlaylistTiming(current, old.Revision, old.Deadline)
|
|
if expired || got != current {
|
|
t.Fatalf("old expiry changed new timing: %#v, %v", got, expired)
|
|
}
|
|
}
|
|
|
|
func TestPauseAndResumePlaylistTimingBeforeReadiness(t *testing.T) {
|
|
now := time.Date(2026, time.September, 1, 12, 0, 0, 0, time.UTC)
|
|
current := NewPlaylistTiming(3, 10*time.Second)
|
|
|
|
paused, changed := PausePlaylistTiming(current, 3, now)
|
|
if !changed || !paused.Paused || paused.Ready || paused.Started {
|
|
t.Fatalf("PausePlaylistTiming() = %#v, %v", paused, changed)
|
|
}
|
|
resumed, changed := ResumePlaylistTiming(paused, 3, now.Add(time.Second))
|
|
if !changed || resumed.Paused || resumed.Ready || resumed.Started {
|
|
t.Fatalf("ResumePlaylistTiming() = %#v, %v", resumed, changed)
|
|
}
|
|
if resumed.Remaining != 10*time.Second {
|
|
t.Fatalf("remaining = %v, want 10s", resumed.Remaining)
|
|
}
|
|
}
|
|
|
|
func TestPauseAndResumeActivePlaylistTimingUsesRemaining(t *testing.T) {
|
|
now := time.Date(2026, time.September, 1, 12, 0, 0, 0, time.UTC)
|
|
current, _ := StartPlaylistTiming(NewPlaylistTiming(5, 10*time.Second), 5, now)
|
|
|
|
paused, changed := PausePlaylistTiming(current, 5, now.Add(4*time.Second))
|
|
if !changed || !paused.Paused || paused.Started || !paused.Ready {
|
|
t.Fatalf("PausePlaylistTiming() = %#v, %v", paused, changed)
|
|
}
|
|
if paused.Remaining != 6*time.Second || !paused.Deadline.IsZero() {
|
|
t.Fatalf("paused timing = %#v, want 6s remaining and no deadline", paused)
|
|
}
|
|
|
|
resumeAt := now.Add(20 * time.Second)
|
|
resumed, changed := ResumePlaylistTiming(paused, 5, resumeAt)
|
|
if !changed || resumed.Paused || !resumed.Started || !resumed.Ready {
|
|
t.Fatalf("ResumePlaylistTiming() = %#v, %v", resumed, changed)
|
|
}
|
|
if resumed.Deadline != resumeAt.Add(6*time.Second) {
|
|
t.Fatalf("resumed deadline = %v, want %v", resumed.Deadline, resumeAt.Add(6*time.Second))
|
|
}
|
|
}
|
|
|
|
func TestPauseAndResumePlaylistTimingIgnoreInvalidTransitions(t *testing.T) {
|
|
now := time.Now()
|
|
base := NewPlaylistTiming(2, time.Second)
|
|
paused, _ := PausePlaylistTiming(base, 2, now)
|
|
tests := []struct {
|
|
name string
|
|
state PlaylistTimingState
|
|
pause bool
|
|
revision uint64
|
|
}{
|
|
{name: "pause wrong revision", state: base, pause: true, revision: 1},
|
|
{name: "duplicate pause", state: paused, pause: true, revision: 2},
|
|
{name: "resume wrong revision", state: paused, revision: 1},
|
|
{name: "duplicate resume", state: base, revision: 2},
|
|
{name: "pause expired", state: PlaylistTimingState{Revision: 2, Duration: time.Second, Expired: true}, pause: true, revision: 2},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
var got PlaylistTimingState
|
|
var changed bool
|
|
if test.pause {
|
|
got, changed = PausePlaylistTiming(test.state, test.revision, now)
|
|
} else {
|
|
got, changed = ResumePlaylistTiming(test.state, test.revision, now)
|
|
}
|
|
if changed || got != test.state {
|
|
t.Fatalf("transition = %#v, %v; want unchanged", got, changed)
|
|
}
|
|
})
|
|
}
|
|
}
|