Files
2026-09-02 00:08:53 +03:00

211 lines
5.6 KiB
Go

package playback
import (
"context"
"errors"
"testing"
"time"
)
func TestIsSessionFailed(t *testing.T) {
const generation = 7
video := FeedConfig{Domain: "/video", UUID: "video", Active: true}
audio := FeedConfig{Domain: "/audio", UUID: "audio", Active: true}
failedFeed := func(unit Unit, feed FeedConfig) Status {
return Status{
Unit: unit, State: StateFailed, Generation: generation, Feed: feed,
}
}
independent := SessionSnapshot{
Generation: generation,
Plan: SessionPlan{
Topology: TopologyIndependent,
Video: video,
Audio: audio,
},
}
synchronized := SessionSnapshot{
Generation: generation,
Plan: SessionPlan{
Topology: TopologySynchronized,
Sync: SyncPairConfig{Video: video, Audio: audio},
},
}
tests := []struct {
name string
session SessionSnapshot
statuses PlaybackStatusSnapshot
want bool
}{
{
name: "video failure in independent pair",
session: independent,
statuses: PlaybackStatusSnapshot{
Generation: generation,
Video: failedFeed(UnitVideo, video), HasVideo: true,
},
want: true,
},
{
name: "audio failure in independent pair",
session: independent,
statuses: PlaybackStatusSnapshot{
Generation: generation,
Audio: failedFeed(UnitAudio, audio), HasAudio: true,
},
want: true,
},
{
name: "sync failure",
session: synchronized,
statuses: PlaybackStatusSnapshot{
Generation: generation,
Sync: Status{
Unit: UnitSync, State: StateFailed, Generation: generation,
Pair: SyncPairConfig{Video: video, Audio: audio},
},
HasSync: true,
},
want: true,
},
{
name: "stale snapshot generation",
session: independent,
statuses: PlaybackStatusSnapshot{
Generation: generation - 1,
Video: failedFeed(UnitVideo, video), HasVideo: true,
},
},
{
name: "wrong source",
session: independent,
statuses: PlaybackStatusSnapshot{
Generation: generation,
Video: failedFeed(UnitVideo, FeedConfig{
Domain: "/video", UUID: "other", Active: true,
}),
HasVideo: true,
},
},
{
name: "inactive failed unit is ignored",
session: SessionSnapshot{
Generation: generation,
Plan: SessionPlan{
Topology: TopologyIndependent,
Video: video,
Audio: FeedConfig{Domain: audio.Domain, UUID: audio.UUID},
},
},
statuses: PlaybackStatusSnapshot{
Generation: generation,
Audio: failedFeed(UnitAudio, audio), HasAudio: true,
},
},
{
name: "reconnecting has not exhausted retries",
session: independent,
statuses: PlaybackStatusSnapshot{
Generation: generation,
Video: Status{
Unit: UnitVideo, State: StateReconnecting,
Generation: generation, Feed: video,
},
HasVideo: true,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := IsSessionFailed(test.session, test.statuses); got != test.want {
t.Fatalf("IsSessionFailed() = %v, want %v", got, test.want)
}
})
}
}
func TestPlaylistControllerFailurePolicy(t *testing.T) {
for _, test := range []struct {
name string
policy PlaylistFailurePolicy
wantAdvance bool
}{
{name: "wait", policy: PlaylistFailureWait},
{name: "next", policy: PlaylistFailureNext, wantAdvance: true},
} {
t.Run(test.name, func(t *testing.T) {
playlist := navigationPlaylist(false)
playlist.OnFailure = test.policy
sessions := make(chan SessionCommand, 4)
controller, err := NewPlaylistController(
playlist, validPlaylistRetryPolicy(), sessions,
)
if err != nil {
t.Fatalf("NewPlaylistController() error = %v", err)
}
commands := make(chan PlaylistCommand, 2)
events := make(chan PlaylistEvent, 2)
ctx, cancel := context.WithCancel(context.Background())
result := make(chan error, 1)
go func() { result <- controller.Run(ctx, commands, events) }()
commands <- PlaylistCommand{Kind: PlaylistSelect, Index: 0}
<-sessions
failureErr := errors.New("flow unavailable")
events <- PlaylistEvent{
Revision: 1,
Kind: PlaylistEventFailed,
Failure: Status{
Unit: UnitVideo, State: StateFailed,
Attempt: 2, FailedAttempts: 2, Err: failureErr,
},
}
if test.wantAdvance {
select {
case <-sessions:
case <-time.After(time.Second):
t.Fatal("failure did not advance playlist")
}
snapshot := waitForPlaylistSnapshot(t, controller, func(snapshot PlaylistSnapshot) bool {
return snapshot.State.CurrentIndex == 1 && snapshot.Revision == 2
})
if snapshot.State.CurrentIndex != 1 || snapshot.Revision != 2 {
t.Fatalf("snapshot = %#v, want index 1 revision 2", snapshot)
}
assertPlaylistFailure(t, snapshot, failureErr)
} else {
select {
case command := <-sessions:
t.Fatalf("unexpected session command: %#v", command)
case <-time.After(20 * time.Millisecond):
}
snapshot := waitForPlaylistSnapshot(t, controller, func(snapshot PlaylistSnapshot) bool {
return snapshot.HasFailure
})
assertPlaylistFailure(t, snapshot, failureErr)
}
cancel()
if err := <-result; err != context.Canceled {
t.Fatalf("Run() error = %v, want context canceled", err)
}
})
}
}
func assertPlaylistFailure(t *testing.T, snapshot PlaylistSnapshot, wantErr error) {
t.Helper()
if !snapshot.HasFailure {
t.Fatal("snapshot has no playlist failure")
}
failure := snapshot.Failure
if failure.EntryIndex != 0 || failure.EntryName != "first" ||
failure.Status.Unit != UnitVideo || failure.Status.Attempt != 2 ||
failure.Status.FailedAttempts != 2 || !errors.Is(failure.Status.Err, wantErr) {
t.Fatalf("failure = %#v", failure)
}
}