Browse Source

move test utilities into 'test' (#3016)

* move test utilities into 'test'

* rename newEmptyTimer into emptyTimer
pull/3017/head
Alessandro Ros 1 year ago committed by GitHub
parent
commit
b6eaa6bc18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      internal/core/api_test.go
  2. 2
      internal/core/metrics_test.go
  3. 22
      internal/core/path.go
  4. 2
      internal/core/path_test.go
  5. 2
      internal/core/static_source_handler.go
  6. 3
      internal/core/webrtc_server_test.go
  7. 11
      internal/playback/server_test.go
  8. 9
      internal/protocols/webrtc/peer_connection.go
  9. 15
      internal/record/agent_test.go
  10. 3
      internal/record/cleaner_test.go
  11. 4
      internal/servers/hls/muxer.go
  12. 4
      internal/staticsources/hls/source_test.go
  13. 8
      internal/staticsources/rtmp/source_test.go
  14. 12
      internal/staticsources/rtsp/source_test.go
  15. 4
      internal/staticsources/srt/source_test.go
  16. 4
      internal/staticsources/udp/source_test.go
  17. 5
      internal/staticsources/webrtc/source_test.go
  18. 10
      internal/test/nil_logger.go
  19. 22
      internal/test/source_tester.go

4
internal/core/api_test.go

@ -27,6 +27,7 @@ import (
"github.com/bluenviron/mediamtx/internal/protocols/rtmp" "github.com/bluenviron/mediamtx/internal/protocols/rtmp"
"github.com/bluenviron/mediamtx/internal/protocols/webrtc" "github.com/bluenviron/mediamtx/internal/protocols/webrtc"
"github.com/bluenviron/mediamtx/internal/test"
) )
var testFormatH264 = &format.H264{ var testFormatH264 = &format.H264{
@ -541,6 +542,7 @@ func TestAPIProtocolList(t *testing.T) {
c := &webrtc.WHIPClient{ c := &webrtc.WHIPClient{
HTTPClient: hc, HTTPClient: hc,
URL: u, URL: u,
Log: test.NilLogger{},
} }
_, err = c.Read(context.Background()) _, err = c.Read(context.Background())
@ -840,6 +842,7 @@ func TestAPIProtocolGet(t *testing.T) {
c := &webrtc.WHIPClient{ c := &webrtc.WHIPClient{
HTTPClient: hc, HTTPClient: hc,
URL: u, URL: u,
Log: test.NilLogger{},
} }
_, err = c.Read(context.Background()) _, err = c.Read(context.Background())
@ -1134,6 +1137,7 @@ func TestAPIProtocolKick(t *testing.T) {
c := &webrtc.WHIPClient{ c := &webrtc.WHIPClient{
HTTPClient: hc, HTTPClient: hc,
URL: u, URL: u,
Log: test.NilLogger{},
} }
_, err = c.Publish(context.Background(), medi.Formats[0], nil) _, err = c.Publish(context.Background(), medi.Formats[0], nil)

2
internal/core/metrics_test.go

@ -22,6 +22,7 @@ import (
"github.com/bluenviron/mediamtx/internal/protocols/rtmp" "github.com/bluenviron/mediamtx/internal/protocols/rtmp"
"github.com/bluenviron/mediamtx/internal/protocols/webrtc" "github.com/bluenviron/mediamtx/internal/protocols/webrtc"
"github.com/bluenviron/mediamtx/internal/test"
) )
func TestMetrics(t *testing.T) { func TestMetrics(t *testing.T) {
@ -158,6 +159,7 @@ webrtc_sessions_bytes_sent 0
s := &webrtc.WHIPClient{ s := &webrtc.WHIPClient{
HTTPClient: &http.Client{Transport: &http.Transport{}}, HTTPClient: &http.Client{Transport: &http.Transport{}},
URL: su, URL: su,
Log: test.NilLogger{},
} }
tracks, err := s.Publish(context.Background(), testMediaH264.Formats[0], nil) tracks, err := s.Publish(context.Background(), testMediaH264.Formats[0], nil)

22
internal/core/path.go

@ -21,7 +21,7 @@ import (
"github.com/bluenviron/mediamtx/internal/stream" "github.com/bluenviron/mediamtx/internal/stream"
) )
func newEmptyTimer() *time.Timer { func emptyTimer() *time.Timer {
t := time.NewTimer(0) t := time.NewTimer(0)
<-t.C <-t.C
return t return t
@ -122,10 +122,10 @@ func (pa *path) initialize() {
pa.ctx = ctx pa.ctx = ctx
pa.ctxCancel = ctxCancel pa.ctxCancel = ctxCancel
pa.readers = make(map[defs.Reader]struct{}) pa.readers = make(map[defs.Reader]struct{})
pa.onDemandStaticSourceReadyTimer = newEmptyTimer() pa.onDemandStaticSourceReadyTimer = emptyTimer()
pa.onDemandStaticSourceCloseTimer = newEmptyTimer() pa.onDemandStaticSourceCloseTimer = emptyTimer()
pa.onDemandPublisherReadyTimer = newEmptyTimer() pa.onDemandPublisherReadyTimer = emptyTimer()
pa.onDemandPublisherCloseTimer = newEmptyTimer() pa.onDemandPublisherCloseTimer = emptyTimer()
pa.chReloadConf = make(chan *conf.Path) pa.chReloadConf = make(chan *conf.Path)
pa.chStaticSourceSetReady = make(chan defs.PathSourceStaticSetReadyReq) pa.chStaticSourceSetReady = make(chan defs.PathSourceStaticSetReadyReq)
pa.chStaticSourceSetNotReady = make(chan defs.PathSourceStaticSetNotReadyReq) pa.chStaticSourceSetNotReady = make(chan defs.PathSourceStaticSetNotReadyReq)
@ -393,7 +393,7 @@ func (pa *path) doSourceStaticSetReady(req defs.PathSourceStaticSetReadyReq) {
if pa.conf.HasOnDemandStaticSource() { if pa.conf.HasOnDemandStaticSource() {
pa.onDemandStaticSourceReadyTimer.Stop() pa.onDemandStaticSourceReadyTimer.Stop()
pa.onDemandStaticSourceReadyTimer = newEmptyTimer() pa.onDemandStaticSourceReadyTimer = emptyTimer()
pa.onDemandStaticSourceScheduleClose() pa.onDemandStaticSourceScheduleClose()
} }
@ -515,7 +515,7 @@ func (pa *path) doStartPublisher(req defs.PathStartPublisherReq) {
if pa.conf.HasOnDemandPublisher() && pa.onDemandPublisherState != pathOnDemandStateInitial { if pa.conf.HasOnDemandPublisher() && pa.onDemandPublisherState != pathOnDemandStateInitial {
pa.onDemandPublisherReadyTimer.Stop() pa.onDemandPublisherReadyTimer.Stop()
pa.onDemandPublisherReadyTimer = newEmptyTimer() pa.onDemandPublisherReadyTimer = emptyTimer()
pa.onDemandPublisherScheduleClose() pa.onDemandPublisherScheduleClose()
} }
@ -674,7 +674,7 @@ func (pa *path) onDemandStaticSourceScheduleClose() {
func (pa *path) onDemandStaticSourceStop(reason string) { func (pa *path) onDemandStaticSourceStop(reason string) {
if pa.onDemandStaticSourceState == pathOnDemandStateClosing { if pa.onDemandStaticSourceState == pathOnDemandStateClosing {
pa.onDemandStaticSourceCloseTimer.Stop() pa.onDemandStaticSourceCloseTimer.Stop()
pa.onDemandStaticSourceCloseTimer = newEmptyTimer() pa.onDemandStaticSourceCloseTimer = emptyTimer()
} }
pa.onDemandStaticSourceState = pathOnDemandStateInitial pa.onDemandStaticSourceState = pathOnDemandStateInitial
@ -707,7 +707,7 @@ func (pa *path) onDemandPublisherScheduleClose() {
func (pa *path) onDemandPublisherStop(reason string) { func (pa *path) onDemandPublisherStop(reason string) {
if pa.onDemandPublisherState == pathOnDemandStateClosing { if pa.onDemandPublisherState == pathOnDemandStateClosing {
pa.onDemandPublisherCloseTimer.Stop() pa.onDemandPublisherCloseTimer.Stop()
pa.onDemandPublisherCloseTimer = newEmptyTimer() pa.onDemandPublisherCloseTimer = emptyTimer()
} }
pa.onUnDemandHook(reason) pa.onUnDemandHook(reason)
@ -857,13 +857,13 @@ func (pa *path) addReaderPost(req defs.PathAddReaderReq) {
if pa.onDemandStaticSourceState == pathOnDemandStateClosing { if pa.onDemandStaticSourceState == pathOnDemandStateClosing {
pa.onDemandStaticSourceState = pathOnDemandStateReady pa.onDemandStaticSourceState = pathOnDemandStateReady
pa.onDemandStaticSourceCloseTimer.Stop() pa.onDemandStaticSourceCloseTimer.Stop()
pa.onDemandStaticSourceCloseTimer = newEmptyTimer() pa.onDemandStaticSourceCloseTimer = emptyTimer()
} }
} else if pa.conf.HasOnDemandPublisher() { } else if pa.conf.HasOnDemandPublisher() {
if pa.onDemandPublisherState == pathOnDemandStateClosing { if pa.onDemandPublisherState == pathOnDemandStateClosing {
pa.onDemandPublisherState = pathOnDemandStateReady pa.onDemandPublisherState = pathOnDemandStateReady
pa.onDemandPublisherCloseTimer.Stop() pa.onDemandPublisherCloseTimer.Stop()
pa.onDemandPublisherCloseTimer = newEmptyTimer() pa.onDemandPublisherCloseTimer = emptyTimer()
} }
} }

2
internal/core/path_test.go

@ -26,6 +26,7 @@ import (
"github.com/bluenviron/mediamtx/internal/defs" "github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/protocols/rtmp" "github.com/bluenviron/mediamtx/internal/protocols/rtmp"
"github.com/bluenviron/mediamtx/internal/protocols/webrtc" "github.com/bluenviron/mediamtx/internal/protocols/webrtc"
"github.com/bluenviron/mediamtx/internal/test"
) )
var runOnDemandSampleScript = ` var runOnDemandSampleScript = `
@ -400,6 +401,7 @@ func TestPathRunOnRead(t *testing.T) {
c := &webrtc.WHIPClient{ c := &webrtc.WHIPClient{
HTTPClient: hc, HTTPClient: hc,
URL: u, URL: u,
Log: test.NilLogger{},
} }
_, err = c.Read(context.Background()) _, err = c.Read(context.Background())

2
internal/core/static_source_handler.go

@ -180,7 +180,7 @@ func (s *staticSourceHandler) run() {
recreate() recreate()
recreating := false recreating := false
recreateTimer := newEmptyTimer() recreateTimer := emptyTimer()
for { for {
select { select {

3
internal/core/webrtc_server_test.go

@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/bluenviron/mediamtx/internal/protocols/webrtc" "github.com/bluenviron/mediamtx/internal/protocols/webrtc"
"github.com/bluenviron/mediamtx/internal/test"
) )
func TestWebRTCPages(t *testing.T) { func TestWebRTCPages(t *testing.T) {
@ -144,6 +145,7 @@ func TestWebRTCRead(t *testing.T) {
c := &webrtc.WHIPClient{ c := &webrtc.WHIPClient{
HTTPClient: hc, HTTPClient: hc,
URL: u, URL: u,
Log: test.NilLogger{},
} }
tracks, err := c.Read(context.Background()) tracks, err := c.Read(context.Background())
@ -284,6 +286,7 @@ func TestWebRTCPublish(t *testing.T) {
s := &webrtc.WHIPClient{ s := &webrtc.WHIPClient{
HTTPClient: hc, HTTPClient: hc,
URL: su, URL: su,
Log: test.NilLogger{},
} }
tracks, err := s.Publish(context.Background(), testMediaH264.Formats[0], nil) tracks, err := s.Publish(context.Background(), testMediaH264.Formats[0], nil)

11
internal/playback/server_test.go

@ -13,15 +13,10 @@ import (
"github.com/bluenviron/mediacommon/pkg/formats/fmp4" "github.com/bluenviron/mediacommon/pkg/formats/fmp4"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4/seekablebuffer" "github.com/bluenviron/mediacommon/pkg/formats/fmp4/seekablebuffer"
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type nilLogger struct{}
func (nilLogger) Log(_ logger.Level, _ string, _ ...interface{}) {
}
func writeSegment1(t *testing.T, fpath string) { func writeSegment1(t *testing.T, fpath string) {
init := fmp4.Init{ init := fmp4.Init{
Tracks: []*fmp4.InitTrack{{ Tracks: []*fmp4.InitTrack{{
@ -155,7 +150,7 @@ func TestServerGet(t *testing.T) {
RecordPath: filepath.Join(dir, "%path/%Y-%m-%d_%H-%M-%S-%f"), RecordPath: filepath.Join(dir, "%path/%Y-%m-%d_%H-%M-%S-%f"),
}, },
}, },
Parent: &nilLogger{}, Parent: &test.NilLogger{},
} }
err = s.Initialize() err = s.Initialize()
require.NoError(t, err) require.NoError(t, err)
@ -249,7 +244,7 @@ func TestServerList(t *testing.T) {
RecordPath: filepath.Join(dir, "%path/%Y-%m-%d_%H-%M-%S-%f"), RecordPath: filepath.Join(dir, "%path/%Y-%m-%d_%H-%M-%S-%f"),
}, },
}, },
Parent: &nilLogger{}, Parent: &test.NilLogger{},
} }
err = s.Initialize() err = s.Initialize()
require.NoError(t, err) require.NoError(t, err)

9
internal/protocols/webrtc/peer_connection.go

@ -19,11 +19,6 @@ const (
webrtcStreamID = "mediamtx" webrtcStreamID = "mediamtx"
) )
type nilLogger struct{}
func (nilLogger) Log(_ logger.Level, _ string, _ ...interface{}) {
}
type trackRecvPair struct { type trackRecvPair struct {
track *webrtc.TrackRemote track *webrtc.TrackRemote
receiver *webrtc.RTPReceiver receiver *webrtc.RTPReceiver
@ -48,10 +43,6 @@ type PeerConnection struct {
// Start starts the peer connection. // Start starts the peer connection.
func (co *PeerConnection) Start() error { func (co *PeerConnection) Start() error {
if co.Log == nil {
co.Log = &nilLogger{}
}
configuration := webrtc.Configuration{ configuration := webrtc.Configuration{
ICEServers: co.ICEServers, ICEServers: co.ICEServers,
} }

15
internal/record/agent_test.go

@ -11,19 +11,14 @@ import (
"github.com/bluenviron/mediacommon/pkg/codecs/h265" "github.com/bluenviron/mediacommon/pkg/codecs/h265"
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio" "github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4" "github.com/bluenviron/mediacommon/pkg/formats/fmp4"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/stream" "github.com/bluenviron/mediamtx/internal/stream"
"github.com/bluenviron/mediamtx/internal/test"
"github.com/bluenviron/mediamtx/internal/unit" "github.com/bluenviron/mediamtx/internal/unit"
) )
type nilLogger struct{}
func (nilLogger) Log(_ logger.Level, _ string, _ ...interface{}) {
}
func TestAgent(t *testing.T) { func TestAgent(t *testing.T) {
desc := &description.Session{Medias: []*description.Media{ desc := &description.Session{Medias: []*description.Media{
{ {
@ -149,7 +144,7 @@ func TestAgent(t *testing.T) {
1460, 1460,
desc, desc,
true, true,
&nilLogger{}, &test.NilLogger{},
) )
require.NoError(t, err) require.NoError(t, err)
defer stream.Close() defer stream.Close()
@ -184,7 +179,7 @@ func TestAgent(t *testing.T) {
OnSegmentComplete: func(fpath string) { OnSegmentComplete: func(fpath string) {
segDone <- struct{}{} segDone <- struct{}{}
}, },
Parent: &nilLogger{}, Parent: &test.NilLogger{},
restartPause: 1 * time.Millisecond, restartPause: 1 * time.Millisecond,
} }
w.Initialize() w.Initialize()
@ -265,7 +260,7 @@ func TestAgentFMP4NegativeDTS(t *testing.T) {
1460, 1460,
desc, desc,
true, true,
&nilLogger{}, &test.NilLogger{},
) )
require.NoError(t, err) require.NoError(t, err)
defer stream.Close() defer stream.Close()
@ -284,7 +279,7 @@ func TestAgentFMP4NegativeDTS(t *testing.T) {
SegmentDuration: 1 * time.Second, SegmentDuration: 1 * time.Second,
PathName: "mypath", PathName: "mypath",
Stream: stream, Stream: stream,
Parent: &nilLogger{}, Parent: &test.NilLogger{},
} }
w.Initialize() w.Initialize()

3
internal/record/cleaner_test.go

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -36,7 +37,7 @@ func TestCleaner(t *testing.T) {
Format: conf.RecordFormatFMP4, Format: conf.RecordFormatFMP4,
DeleteAfter: 10 * time.Second, DeleteAfter: 10 * time.Second,
}}, }},
Parent: nilLogger{}, Parent: test.NilLogger{},
} }
c.Initialize() c.Initialize()
defer c.Close() defer c.Close()

4
internal/servers/hls/muxer.go

@ -34,7 +34,7 @@ func int64Ptr(v int64) *int64 {
return &v return &v
} }
func newEmptyTimer() *time.Timer { func emptyTimer() *time.Timer {
t := time.NewTimer(0) t := time.NewTimer(0)
<-t.C <-t.C
return t return t
@ -142,7 +142,7 @@ func (m *muxer) run() {
isReady := false isReady := false
isRecreating := false isRecreating := false
recreateTimer := newEmptyTimer() recreateTimer := emptyTimer()
for { for {
select { select {

4
internal/staticsources/hls/source_test.go

@ -13,7 +13,7 @@ import (
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs" "github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/staticsources/tester" "github.com/bluenviron/mediamtx/internal/test"
) )
func TestSource(t *testing.T) { func TestSource(t *testing.T) {
@ -86,7 +86,7 @@ func TestSource(t *testing.T) {
go s.Serve(ln) go s.Serve(ln)
defer s.Shutdown(context.Background()) defer s.Shutdown(context.Background())
te := tester.New( te := test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "http://localhost:5780/stream.m3u8", ResolvedSource: "http://localhost:5780/stream.m3u8",

8
internal/staticsources/rtmp/source_test.go

@ -14,7 +14,7 @@ import (
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs" "github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/protocols/rtmp" "github.com/bluenviron/mediamtx/internal/protocols/rtmp"
"github.com/bluenviron/mediamtx/internal/staticsources/tester" "github.com/bluenviron/mediamtx/internal/test"
) )
var serverCert = []byte(`-----BEGIN CERTIFICATE----- var serverCert = []byte(`-----BEGIN CERTIFICATE-----
@ -150,10 +150,10 @@ func TestSource(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
}() }()
var te *tester.Tester var te *test.SourceTester
if ca == "plain" { if ca == "plain" {
te = tester.New( te = test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "rtmp://localhost/teststream", ResolvedSource: "rtmp://localhost/teststream",
@ -165,7 +165,7 @@ func TestSource(t *testing.T) {
&conf.Path{}, &conf.Path{},
) )
} else { } else {
te = tester.New( te = test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "rtmps://localhost/teststream", ResolvedSource: "rtmps://localhost/teststream",

12
internal/staticsources/rtsp/source_test.go

@ -16,7 +16,7 @@ import (
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs" "github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/staticsources/tester" "github.com/bluenviron/mediamtx/internal/test"
) )
var serverCert = []byte(`-----BEGIN CERTIFICATE----- var serverCert = []byte(`-----BEGIN CERTIFICATE-----
@ -206,13 +206,13 @@ func TestSource(t *testing.T) {
stream = gortsplib.NewServerStream(&s, &description.Session{Medias: []*description.Media{testMediaH264}}) stream = gortsplib.NewServerStream(&s, &description.Session{Medias: []*description.Media{testMediaH264}})
defer stream.Close() defer stream.Close()
var te *tester.Tester var te *test.SourceTester
if source != "tls" { if source != "tls" {
var sp conf.RTSPTransport var sp conf.RTSPTransport
sp.UnmarshalJSON([]byte(`"` + source + `"`)) //nolint:errcheck sp.UnmarshalJSON([]byte(`"` + source + `"`)) //nolint:errcheck
te = tester.New( te = test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "rtsp://testuser:testpass@localhost:8555/teststream", ResolvedSource: "rtsp://testuser:testpass@localhost:8555/teststream",
@ -227,7 +227,7 @@ func TestSource(t *testing.T) {
}, },
) )
} else { } else {
te = tester.New( te = test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "rtsps://testuser:testpass@localhost:8555/teststream", ResolvedSource: "rtsps://testuser:testpass@localhost:8555/teststream",
@ -313,7 +313,7 @@ func TestRTSPSourceNoPassword(t *testing.T) {
var sp conf.RTSPTransport var sp conf.RTSPTransport
sp.UnmarshalJSON([]byte(`"tcp"`)) //nolint:errcheck sp.UnmarshalJSON([]byte(`"tcp"`)) //nolint:errcheck
te := tester.New( te := test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "rtsp://testuser:@127.0.0.1:8555/teststream", ResolvedSource: "rtsp://testuser:@127.0.0.1:8555/teststream",
@ -408,7 +408,7 @@ func TestRTSPSourceRange(t *testing.T) {
cnf.RTSPRangeStart = "130s" cnf.RTSPRangeStart = "130s"
} }
te := tester.New( te := test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "rtsp://127.0.0.1:8555/teststream", ResolvedSource: "rtsp://127.0.0.1:8555/teststream",

4
internal/staticsources/srt/source_test.go

@ -11,7 +11,7 @@ import (
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs" "github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/staticsources/tester" "github.com/bluenviron/mediamtx/internal/test"
) )
func TestSource(t *testing.T) { func TestSource(t *testing.T) {
@ -56,7 +56,7 @@ func TestSource(t *testing.T) {
time.Sleep(1000 * time.Millisecond) time.Sleep(1000 * time.Millisecond)
}() }()
te := tester.New( te := test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "srt://localhost:9002?streamid=sidname&passphrase=ttest1234567", ResolvedSource: "srt://localhost:9002?streamid=sidname&passphrase=ttest1234567",

4
internal/staticsources/udp/source_test.go

@ -11,11 +11,11 @@ import (
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs" "github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/staticsources/tester" "github.com/bluenviron/mediamtx/internal/test"
) )
func TestSource(t *testing.T) { func TestSource(t *testing.T) {
te := tester.New( te := test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "udp://localhost:9001", ResolvedSource: "udp://localhost:9001",

5
internal/staticsources/webrtc/source_test.go

@ -16,7 +16,7 @@ import (
"github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs" "github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/protocols/webrtc" "github.com/bluenviron/mediamtx/internal/protocols/webrtc"
"github.com/bluenviron/mediamtx/internal/staticsources/tester" "github.com/bluenviron/mediamtx/internal/test"
) )
func whipOffer(body []byte) *pwebrtc.SessionDescription { func whipOffer(body []byte) *pwebrtc.SessionDescription {
@ -36,6 +36,7 @@ func TestSource(t *testing.T) {
pc := &webrtc.PeerConnection{ pc := &webrtc.PeerConnection{
API: api, API: api,
Publish: true, Publish: true,
Log: test.NilLogger{},
} }
err = pc.Start() err = pc.Start()
require.NoError(t, err) require.NoError(t, err)
@ -124,7 +125,7 @@ func TestSource(t *testing.T) {
go httpServ.Serve(ln) go httpServ.Serve(ln)
defer httpServ.Shutdown(context.Background()) defer httpServ.Shutdown(context.Background())
te := tester.New( te := test.NewSourceTester(
func(p defs.StaticSourceParent) defs.StaticSource { func(p defs.StaticSourceParent) defs.StaticSource {
return &Source{ return &Source{
ResolvedSource: "whep://localhost:9003/my/resource", ResolvedSource: "whep://localhost:9003/my/resource",

10
internal/test/nil_logger.go

@ -0,0 +1,10 @@
package test
import "github.com/bluenviron/mediamtx/internal/logger"
// NilLogger is a logger to /dev/null
type NilLogger struct{}
// Log implements logger.Writer.
func (NilLogger) Log(_ logger.Level, _ string, _ ...interface{}) {
}

22
internal/staticsources/tester/tester.go → internal/test/source_tester.go

@ -1,5 +1,5 @@
// Package tester contains a static source tester. // Package test contains test utilities.
package tester package test
import ( import (
"context" "context"
@ -12,8 +12,8 @@ import (
"github.com/bluenviron/mediamtx/internal/unit" "github.com/bluenviron/mediamtx/internal/unit"
) )
// Tester is a static source tester. // SourceTester is a static source tester.
type Tester struct { type SourceTester struct {
ctx context.Context ctx context.Context
ctxCancel func() ctxCancel func()
stream *stream.Stream stream *stream.Stream
@ -23,11 +23,11 @@ type Tester struct {
done chan struct{} done chan struct{}
} }
// New allocates a tester. // NewSourceTester allocates a SourceTester.
func New(createFunc func(defs.StaticSourceParent) defs.StaticSource, conf *conf.Path) *Tester { func NewSourceTester(createFunc func(defs.StaticSourceParent) defs.StaticSource, conf *conf.Path) *SourceTester {
ctx, ctxCancel := context.WithCancel(context.Background()) ctx, ctxCancel := context.WithCancel(context.Background())
t := &Tester{ t := &SourceTester{
ctx: ctx, ctx: ctx,
ctxCancel: ctxCancel, ctxCancel: ctxCancel,
Unit: make(chan unit.Unit), Unit: make(chan unit.Unit),
@ -48,7 +48,7 @@ func New(createFunc func(defs.StaticSourceParent) defs.StaticSource, conf *conf.
} }
// Close closes the tester. // Close closes the tester.
func (t *Tester) Close() { func (t *SourceTester) Close() {
t.ctxCancel() t.ctxCancel()
t.writer.Stop() t.writer.Stop()
t.stream.Close() t.stream.Close()
@ -56,11 +56,11 @@ func (t *Tester) Close() {
} }
// Log implements StaticSourceParent. // Log implements StaticSourceParent.
func (t *Tester) Log(_ logger.Level, _ string, _ ...interface{}) { func (t *SourceTester) Log(_ logger.Level, _ string, _ ...interface{}) {
} }
// SetReady implements StaticSourceParent. // SetReady implements StaticSourceParent.
func (t *Tester) SetReady(req defs.PathSourceStaticSetReadyReq) defs.PathSourceStaticSetReadyRes { func (t *SourceTester) SetReady(req defs.PathSourceStaticSetReadyReq) defs.PathSourceStaticSetReadyRes {
t.stream, _ = stream.New( t.stream, _ = stream.New(
1460, 1460,
req.Desc, req.Desc,
@ -82,5 +82,5 @@ func (t *Tester) SetReady(req defs.PathSourceStaticSetReadyReq) defs.PathSourceS
} }
// SetNotReady implements StaticSourceParent. // SetNotReady implements StaticSourceParent.
func (t *Tester) SetNotReady(_ defs.PathSourceStaticSetNotReadyReq) { func (t *SourceTester) SetNotReady(_ defs.PathSourceStaticSetNotReadyReq) {
} }
Loading…
Cancel
Save