chore(tests): add first couple replay functionality tests

This commit is contained in:
Gabe Kangas 2023-08-27 20:37:43 -07:00
parent 065e779e99
commit ea376477b6
No known key found for this signature in database
GPG Key ID: 4345B2060657F330
2 changed files with 89 additions and 11 deletions

View File

@ -84,17 +84,6 @@ func NewRecording(streamID string) *HLSRecorder {
return &h return &h
} }
// SetOutputConfigurations sets the output configurations for this stream.
func (h *HLSRecorder) SetOutputConfigurations(configs []HLSOutputConfiguration) {
h.outputConfigurations = configs
}
// StreamBegan is called when a stream is started.
func (h *HLSRecorder) StreamBegan(id string) {
h.streamID = id
h.startTime = time.Now()
}
// SegmentWritten is called when a segment is written to disk. // SegmentWritten is called when a segment is written to disk.
func (h *HLSRecorder) SegmentWritten(path string) { func (h *HLSRecorder) SegmentWritten(path string) {
outputConfigurationIndexString := utils.GetIndexFromFilePath(path) outputConfigurationIndexString := utils.GetIndexFromFilePath(path)

89
replays/replay_test.go Normal file
View File

@ -0,0 +1,89 @@
package replays
import (
"context"
"database/sql"
"fmt"
"path/filepath"
"testing"
"time"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/db"
log "github.com/sirupsen/logrus"
"github.com/teris-io/shortid"
)
var (
fakeStreamId = shortid.MustGenerate()
fakeSegmentCount = 300
fakeSegmentDuration = 4 // Seconds
fakeStreamStartTime = time.Now()
fakeConfigId = ""
)
func TestMain(m *testing.M) {
if err := data.SetupPersistence(":memory:"); err != nil {
panic(err)
}
Setup()
populateFakeStream()
m.Run()
}
func populateFakeStream() {
queries := data.GetDatastore().GetQueries()
recording := NewRecording(fakeStreamId)
fakeConfigId = recording.outputConfigurations[0].ID
for i := 0; i < fakeSegmentCount; i++ {
fakeSegmentName := fmt.Sprintf("%s-%d.ts", fakeStreamId, i)
if err := queries.InsertSegment(context.Background(), db.InsertSegmentParams{
ID: shortid.MustGenerate(),
StreamID: fakeStreamId,
OutputConfigurationID: fakeConfigId,
Path: filepath.Join(fakeStreamId, fakeConfigId, "0", fakeSegmentName),
RelativeTimestamp: float32(i * fakeSegmentDuration),
Timestamp: sql.NullTime{Time: fakeStreamStartTime.Add(time.Duration(fakeSegmentDuration * i)), Valid: true},
}); err != nil {
log.Errorln(err)
}
}
if err := queries.SetStreamEnded(context.Background(), db.SetStreamEndedParams{
ID: fakeStreamId,
EndTime: sql.NullTime{Time: fakeStreamStartTime.Add(time.Duration(fakeSegmentDuration * fakeSegmentCount)), Valid: true},
}); err != nil {
log.Errorln(err)
}
}
func TestStream(t *testing.T) {
playlist := NewPlaylistGenerator()
stream, err := playlist.GetStream(fakeStreamId)
if err != nil {
t.Error(err)
}
if stream.ID != fakeStreamId {
t.Error("expected stream id", fakeStreamId, "got", stream.ID)
}
}
func TestPlaylist(t *testing.T) {
playlist := NewPlaylistGenerator()
p, err := playlist.GenerateMediaPlaylistForStreamAndConfiguration(fakeStreamId, fakeConfigId)
if p == nil {
t.Error("expected playlist")
}
if err != nil {
t.Error(err)
}
if len(p.Segments) != fakeSegmentCount {
t.Error("expected", fakeSegmentCount, "segments, got", len(p.Segments))
}
}