Files
Alessandro Ros c8e07b55d5 api: add new stats (#5582)
- RTSPSession.outboundRTPPacketsDiscarded
- Path.inboundFramesInError
- SRTConn.outboundFramesDiscarded
- WebRTCSession.outboundFramesDiscarded
- RTMPConn.outboundFramesDiscarded
- HLSMuxer.outboundFramesDiscarded
2026-03-17 17:22:15 +01:00

121 lines
3.1 KiB
Go

package api //nolint:revive
import (
"net/http"
"testing"
"time"
"github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs"
"github.com/bluenviron/mediamtx/internal/servers/hls"
"github.com/bluenviron/mediamtx/internal/test"
"github.com/stretchr/testify/require"
)
type testHLSServer struct {
muxers map[string]*defs.APIHLSMuxer
}
func (s *testHLSServer) APIMuxersList() (*defs.APIHLSMuxerList, error) {
items := make([]defs.APIHLSMuxer, 0, len(s.muxers))
for _, muxer := range s.muxers {
items = append(items, *muxer)
}
return &defs.APIHLSMuxerList{Items: items}, nil
}
func (s *testHLSServer) APIMuxersGet(name string) (*defs.APIHLSMuxer, error) {
muxer, ok := s.muxers[name]
if !ok {
return nil, hls.ErrMuxerNotFound
}
return muxer, nil
}
func TestHLSMuxersList(t *testing.T) {
now := time.Now()
hlsServer := &testHLSServer{
muxers: map[string]*defs.APIHLSMuxer{
"test1": {
Path: "test1",
Created: now,
LastRequest: now.Add(5 * time.Second),
OutboundBytes: 1234,
OutboundFramesDiscarded: 10,
BytesSent: 1234,
},
"test2": {
Path: "test2",
Created: now.Add(time.Minute),
LastRequest: now.Add(time.Minute + 10*time.Second),
OutboundBytes: 5678,
OutboundFramesDiscarded: 20,
BytesSent: 5678,
},
},
}
api := API{
Address: "localhost:9997",
ReadTimeout: conf.Duration(10 * time.Second),
WriteTimeout: conf.Duration(10 * time.Second),
AuthManager: test.NilAuthManager,
HLSServer: hlsServer,
Parent: &testParent{},
}
err := api.Initialize()
require.NoError(t, err)
defer api.Close()
tr := &http.Transport{}
defer tr.CloseIdleConnections()
hc := &http.Client{Transport: tr}
var out defs.APIHLSMuxerList
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/hlsmuxers/list", nil, &out)
require.Equal(t, 2, out.ItemCount)
require.Equal(t, 1, out.PageCount)
require.Len(t, out.Items, 2)
}
func TestHLSMuxersGet(t *testing.T) {
now := time.Now()
hlsServer := &testHLSServer{
muxers: map[string]*defs.APIHLSMuxer{
"mypath": {
Path: "mypath",
Created: now,
LastRequest: now.Add(5 * time.Second),
OutboundBytes: 9999,
OutboundFramesDiscarded: 12,
BytesSent: 9999,
},
},
}
api := API{
Address: "localhost:9997",
ReadTimeout: conf.Duration(10 * time.Second),
WriteTimeout: conf.Duration(10 * time.Second),
AuthManager: test.NilAuthManager,
HLSServer: hlsServer,
Parent: &testParent{},
}
err := api.Initialize()
require.NoError(t, err)
defer api.Close()
tr := &http.Transport{}
defer tr.CloseIdleConnections()
hc := &http.Client{Transport: tr}
var out defs.APIHLSMuxer
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/hlsmuxers/get/mypath", nil, &out)
require.Equal(t, "mypath", out.Path)
require.Equal(t, uint64(9999), out.OutboundBytes)
require.Equal(t, uint64(12), out.OutboundFramesDiscarded)
require.Equal(t, uint64(9999), out.BytesSent)
}