103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package playlist_test
|
|
|
|
import (
|
|
"io"
|
|
"segmento/pkg/media"
|
|
"segmento/pkg/playlist"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type FakeReader struct {
|
|
}
|
|
|
|
func (r *FakeReader) Read([]byte) (int, error) {
|
|
return 0, nil
|
|
}
|
|
|
|
type FakeSegmenter struct {
|
|
count int
|
|
}
|
|
|
|
func (s *FakeSegmenter) Segment(r io.Reader) (chan *media.Segment, error) {
|
|
c := make(chan *media.Segment)
|
|
|
|
go func() {
|
|
dur := 2500 * time.Millisecond
|
|
for i := 0; i < s.count; i++ {
|
|
segment := media.NewSegment(dur, 0)
|
|
segment.IncrementDuration(dur)
|
|
c <- segment
|
|
}
|
|
close(c)
|
|
}()
|
|
|
|
return c, nil
|
|
}
|
|
|
|
type FakeSegmentPublisher struct {
|
|
count int
|
|
}
|
|
|
|
func (p *FakeSegmentPublisher) Publish(s *media.Segment) (string, error) {
|
|
p.count++
|
|
return "", nil
|
|
}
|
|
|
|
type consumer struct {
|
|
pCount, sCount int
|
|
}
|
|
|
|
func (c *consumer) PlaylistUpdated(p playlist.Playlist) {
|
|
c.pCount++
|
|
}
|
|
|
|
func (c *consumer) PlaylistSegmentAdded(p playlist.Playlist, s *playlist.PlaylistSegment) {
|
|
c.sCount++
|
|
}
|
|
|
|
func TestMediaPlaylistImplements(t *testing.T) {
|
|
require.Implements(t, (*playlist.Playlist)(nil), new(playlist.MediaPlaylist))
|
|
}
|
|
|
|
func TestMediaPlaylist(t *testing.T) {
|
|
publisher := &FakeSegmentPublisher{}
|
|
playlist := playlist.NewMediaPlaylist(&FakeReader{}, &FakeSegmenter{3}, publisher)
|
|
|
|
err := playlist.Run()
|
|
require.NoError(t, err)
|
|
require.Equal(t, 3, playlist.Len())
|
|
require.Equal(t, 3, publisher.count)
|
|
}
|
|
|
|
func TestMediaPlaylistRender(t *testing.T) {
|
|
playlist := playlist.NewMediaPlaylist(&FakeReader{}, &FakeSegmenter{2}, &FakeSegmentPublisher{})
|
|
err := playlist.Run()
|
|
require.NoError(t, err)
|
|
|
|
lines := strings.Split(playlist.Render(), "\n")
|
|
|
|
require.Equal(t, "#EXTM3U", lines[0])
|
|
require.Equal(t, "#EXT-X-VERSION:3", lines[1])
|
|
require.Equal(t, "#EXT-X-TARGETDURATION:3", lines[2])
|
|
require.Equal(t, "#EXTINF:2.50000", lines[3])
|
|
require.Equal(t, "http://www.example.com/x.mp3", lines[4])
|
|
require.Equal(t, "#EXTINF:2.50000", lines[5])
|
|
require.Equal(t, "http://www.example.com/x.mp3", lines[6])
|
|
}
|
|
|
|
func TestMediaPlaylistConsumer(t *testing.T) {
|
|
consumer := &consumer{}
|
|
require.Implements(t, (*playlist.Consumer)(nil), consumer)
|
|
|
|
playlist := playlist.NewMediaPlaylist(&FakeReader{}, &FakeSegmenter{4}, &FakeSegmentPublisher{})
|
|
playlist.AddConsumer(consumer)
|
|
err := playlist.Run()
|
|
require.NoError(t, err)
|
|
require.Equal(t, 4, consumer.sCount)
|
|
require.Equal(t, 0, consumer.pCount) // TODO
|
|
}
|