Browse Source

fix race condition in tests (#1826)

pull/1829/head
Alessandro Ros 3 years ago committed by GitHub
parent
commit
330a9e027c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 166
      internal/core/api_test.go
  2. 28
      internal/core/hls_manager_test.go
  3. 9
      internal/core/metrics_test.go
  4. 34
      internal/core/webrtc_manager_test.go
  5. 4
      internal/highleveltests/hls_server_test.go

166
internal/core/api_test.go

@ -39,43 +39,35 @@ var testMediaH264 = &media.Media{ @@ -39,43 +39,35 @@ var testMediaH264 = &media.Media{
Formats: []formats.Format{testFormatH264},
}
func httpRequest(method string, ur string, in interface{}, out interface{}) error {
buf, err := func() (io.Reader, error) {
func httpRequest(t *testing.T, hc *http.Client, method string, ur string, in interface{}, out interface{}) {
buf := func() io.Reader {
if in == nil {
return nil, nil
return nil
}
byts, err := json.Marshal(in)
if err != nil {
return nil, err
}
require.NoError(t, err)
return bytes.NewBuffer(byts), nil
return bytes.NewBuffer(byts)
}()
if err != nil {
return err
}
req, err := http.NewRequest(method, ur, buf)
if err != nil {
return err
}
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("bad status code: %d", res.StatusCode)
t.Errorf("bad status code: %d", res.StatusCode)
}
if out == nil {
return nil
return
}
return json.NewDecoder(res.Body).Decode(out)
err = json.NewDecoder(res.Body).Decode(out)
require.NoError(t, err)
}
func TestPagination(t *testing.T) {
@ -111,17 +103,14 @@ func TestPagination(t *testing.T) { @@ -111,17 +103,14 @@ func TestPagination(t *testing.T) {
}
func TestAPIConfigGet(t *testing.T) {
// since the HTTP server is created and deleted multiple times,
// we can't reuse TCP connections.
http.DefaultTransport.(*http.Transport).DisableKeepAlives = true
p, ok := newInstance("api: yes\n")
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
var out map[string]interface{}
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.Equal(t, true, out["api"])
}
@ -130,18 +119,18 @@ func TestAPIConfigSet(t *testing.T) { @@ -130,18 +119,18 @@ func TestAPIConfigSet(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
err := httpRequest(http.MethodPost, "http://localhost:9997/v2/config/set", map[string]interface{}{
hc := &http.Client{Transport: &http.Transport{}}
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/set", map[string]interface{}{
"rtmpDisable": true,
"readTimeout": "7s",
"protocols": []string{"tcp"},
}, nil)
require.NoError(t, err)
time.Sleep(500 * time.Millisecond)
var out map[string]interface{}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.Equal(t, true, out["rtmpDisable"])
require.Equal(t, "7s", out["readTimeout"])
require.Equal(t, []interface{}{"tcp"}, out["protocols"])
@ -152,15 +141,15 @@ func TestAPIConfigPathsAdd(t *testing.T) { @@ -152,15 +141,15 @@ func TestAPIConfigPathsAdd(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
err := httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
hc := &http.Client{Transport: &http.Transport{}}
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
"source": "rtsp://127.0.0.1:9999/mypath",
"sourceOnDemand": true,
}, nil)
require.NoError(t, err)
var out map[string]interface{}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.Equal(t, "rtsp://127.0.0.1:9999/mypath",
out["paths"].(map[string]interface{})["my/path"].(map[string]interface{})["source"])
}
@ -170,25 +159,24 @@ func TestAPIConfigPathsEdit(t *testing.T) { @@ -170,25 +159,24 @@ func TestAPIConfigPathsEdit(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
err := httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
hc := &http.Client{Transport: &http.Transport{}}
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
"source": "rtsp://127.0.0.1:9999/mypath",
"sourceOnDemand": true,
}, nil)
require.NoError(t, err)
err = httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/edit/my/path", map[string]interface{}{
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/edit/my/path", map[string]interface{}{
"source": "rtsp://127.0.0.1:9998/mypath",
"sourceOnDemand": true,
}, nil)
require.NoError(t, err)
var out struct {
Paths map[string]struct {
Source string `json:"source"`
} `json:"paths"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.Equal(t, "rtsp://127.0.0.1:9998/mypath", out.Paths["my/path"].Source)
}
@ -197,20 +185,19 @@ func TestAPIConfigPathsRemove(t *testing.T) { @@ -197,20 +185,19 @@ func TestAPIConfigPathsRemove(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
err := httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
hc := &http.Client{Transport: &http.Transport{}}
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
"source": "rtsp://127.0.0.1:9999/mypath",
"sourceOnDemand": true,
}, nil)
require.NoError(t, err)
err = httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/remove/my/path", nil, nil)
require.NoError(t, err)
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/remove/my/path", nil, nil)
var out struct {
Paths map[string]interface{} `json:"paths"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
_, ok = out.Paths["my/path"]
require.Equal(t, false, ok)
}
@ -240,6 +227,8 @@ func TestAPIPathsList(t *testing.T) { @@ -240,6 +227,8 @@ func TestAPIPathsList(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
media0 := testMediaH264
source := gortsplib.Client{}
@ -274,8 +263,7 @@ func TestAPIPathsList(t *testing.T) { @@ -274,8 +263,7 @@ func TestAPIPathsList(t *testing.T) {
})
var out pathList
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.Equal(t, pathList{
PageCount: 1,
Items: []path{{
@ -308,6 +296,8 @@ func TestAPIPathsList(t *testing.T) { @@ -308,6 +296,8 @@ func TestAPIPathsList(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
medias := media.Medias{
{
Type: media.TypeVideo,
@ -335,8 +325,7 @@ func TestAPIPathsList(t *testing.T) { @@ -335,8 +325,7 @@ func TestAPIPathsList(t *testing.T) {
defer source.Close()
var out pathList
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.Equal(t, pathList{
PageCount: 1,
Items: []path{{
@ -359,9 +348,10 @@ func TestAPIPathsList(t *testing.T) { @@ -359,9 +348,10 @@ func TestAPIPathsList(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
var out pathList
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.Equal(t, pathList{
PageCount: 1,
Items: []path{{
@ -384,9 +374,10 @@ func TestAPIPathsList(t *testing.T) { @@ -384,9 +374,10 @@ func TestAPIPathsList(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
var out pathList
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.Equal(t, pathList{
PageCount: 1,
Items: []path{{
@ -409,9 +400,10 @@ func TestAPIPathsList(t *testing.T) { @@ -409,9 +400,10 @@ func TestAPIPathsList(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
var out pathList
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
require.Equal(t, pathList{
PageCount: 1,
Items: []path{{
@ -433,6 +425,8 @@ func TestAPIPathsGet(t *testing.T) { @@ -433,6 +425,8 @@ func TestAPIPathsGet(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
source := gortsplib.Client{}
err := source.StartRecording("rtsp://localhost:8554/mypath", media.Medias{testMediaH264})
require.NoError(t, err)
@ -459,11 +453,9 @@ func TestAPIPathsGet(t *testing.T) { @@ -459,11 +453,9 @@ func TestAPIPathsGet(t *testing.T) {
pathName = "nonexisting"
}
var out path
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/get/"+pathName, nil, &out)
if ca == "ok" {
require.NoError(t, err)
var out path
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/get/"+pathName, nil, &out)
require.Equal(t, path{
Name: "mypath",
Source: pathSource{
@ -473,7 +465,10 @@ func TestAPIPathsGet(t *testing.T) { @@ -473,7 +465,10 @@ func TestAPIPathsGet(t *testing.T) {
Tracks: []string{"H264"},
}, out)
} else {
require.EqualError(t, err, "bad status code: 404")
res, err := hc.Get("http://localhost:9997/v2/paths/get/" + pathName)
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, 404, res.StatusCode)
}
})
}
@ -521,6 +516,8 @@ func TestAPIProtocolList(t *testing.T) { @@ -521,6 +516,8 @@ func TestAPIProtocolList(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
medi := testMediaH264
switch ca { //nolint:dupl
@ -619,7 +616,7 @@ func TestAPIProtocolList(t *testing.T) { @@ -619,7 +616,7 @@ func TestAPIProtocolList(t *testing.T) {
}()
func() {
res, err := http.Get("http://localhost:8888/mypath/index.m3u8")
res, err := hc.Get("http://localhost:8888/mypath/index.m3u8")
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, 200, res.StatusCode)
@ -632,7 +629,7 @@ func TestAPIProtocolList(t *testing.T) { @@ -632,7 +629,7 @@ func TestAPIProtocolList(t *testing.T) {
require.NoError(t, err)
defer source.Close()
c := newWebRTCTestClient(t, "http://localhost:8889/mypath/whep", false)
c := newWebRTCTestClient(t, hc, "http://localhost:8889/mypath/whep", false)
defer c.close()
time.Sleep(500 * time.Millisecond)
@ -680,8 +677,7 @@ func TestAPIProtocolList(t *testing.T) { @@ -680,8 +677,7 @@ func TestAPIProtocolList(t *testing.T) {
State string `json:"state"`
} `json:"items"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out)
if ca != "rtsp conns" && ca != "rtsps conns" {
require.Equal(t, "publish", out.Items[0].State)
@ -694,8 +690,7 @@ func TestAPIProtocolList(t *testing.T) { @@ -694,8 +690,7 @@ func TestAPIProtocolList(t *testing.T) {
LastRequest string `json:"lastRequest"`
} `json:"items"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/hlsmuxers/list", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/hlsmuxers/list", nil, &out)
s := fmt.Sprintf("^%d-", time.Now().Year())
require.Regexp(t, s, out.Items[0].Created)
@ -716,8 +711,7 @@ func TestAPIProtocolList(t *testing.T) { @@ -716,8 +711,7 @@ func TestAPIProtocolList(t *testing.T) {
PageCount int `json:"pageCount"`
Items []item `json:"items"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/webrtcsessions/list", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/webrtcsessions/list", nil, &out)
require.Equal(t, true, out.Items[0].PeerConnectionEstablished)
}
@ -767,6 +761,8 @@ func TestAPIProtocolGet(t *testing.T) { @@ -767,6 +761,8 @@ func TestAPIProtocolGet(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
medi := testMediaH264
switch ca { //nolint:dupl
@ -865,7 +861,7 @@ func TestAPIProtocolGet(t *testing.T) { @@ -865,7 +861,7 @@ func TestAPIProtocolGet(t *testing.T) {
}()
func() {
res, err := http.Get("http://localhost:8888/mypath/index.m3u8")
res, err := hc.Get("http://localhost:8888/mypath/index.m3u8")
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, 200, res.StatusCode)
@ -878,7 +874,7 @@ func TestAPIProtocolGet(t *testing.T) { @@ -878,7 +874,7 @@ func TestAPIProtocolGet(t *testing.T) {
require.NoError(t, err)
defer source.Close()
c := newWebRTCTestClient(t, "http://localhost:8889/mypath/whep", false)
c := newWebRTCTestClient(t, hc, "http://localhost:8889/mypath/whep", false)
defer c.close()
time.Sleep(500 * time.Millisecond)
@ -929,16 +925,14 @@ func TestAPIProtocolGet(t *testing.T) { @@ -929,16 +925,14 @@ func TestAPIProtocolGet(t *testing.T) {
var out1 struct {
Items []item `json:"items"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out1)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out1)
if ca != "rtsp conns" && ca != "rtsps conns" {
require.Equal(t, "publish", out1.Items[0].State)
}
var out2 item
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/get/"+out1.Items[0].ID, nil, &out2)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/get/"+out1.Items[0].ID, nil, &out2)
if ca != "rtsp conns" && ca != "rtsps conns" {
require.Equal(t, "publish", out2.State)
@ -951,8 +945,7 @@ func TestAPIProtocolGet(t *testing.T) { @@ -951,8 +945,7 @@ func TestAPIProtocolGet(t *testing.T) {
}
var out item
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/hlsmuxers/get/mypath", nil, &out)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/hlsmuxers/get/mypath", nil, &out)
s := fmt.Sprintf("^%d-", time.Now().Year())
require.Regexp(t, s, out.Created)
@ -973,12 +966,10 @@ func TestAPIProtocolGet(t *testing.T) { @@ -973,12 +966,10 @@ func TestAPIProtocolGet(t *testing.T) {
var out1 struct {
Items []item `json:"items"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/webrtcsessions/list", nil, &out1)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/webrtcsessions/list", nil, &out1)
var out2 item
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/webrtcsessions/get/"+out1.Items[0].ID, nil, &out2)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/webrtcsessions/get/"+out1.Items[0].ID, nil, &out2)
require.Equal(t, true, out2.PeerConnectionEstablished)
}
@ -1018,6 +1009,8 @@ func TestAPIProtocolKick(t *testing.T) { @@ -1018,6 +1009,8 @@ func TestAPIProtocolKick(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
hc := &http.Client{Transport: &http.Transport{}}
medi := testMediaH264
switch ca {
@ -1055,7 +1048,7 @@ func TestAPIProtocolKick(t *testing.T) { @@ -1055,7 +1048,7 @@ func TestAPIProtocolKick(t *testing.T) {
require.NoError(t, err)
case "webrtc":
c := newWebRTCTestClient(t, "http://localhost:8889/mypath/whip", true)
c := newWebRTCTestClient(t, hc, "http://localhost:8889/mypath/whip", true)
defer c.close()
}
@ -1079,19 +1072,16 @@ func TestAPIProtocolKick(t *testing.T) { @@ -1079,19 +1072,16 @@ func TestAPIProtocolKick(t *testing.T) {
ID string `json:"id"`
} `json:"items"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out1)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out1)
err = httpRequest(http.MethodPost, "http://localhost:9997/v2/"+pa+"/kick/"+out1.Items[0].ID, nil, nil)
require.NoError(t, err)
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/"+pa+"/kick/"+out1.Items[0].ID, nil, nil)
var out2 struct {
Items []struct {
ID string `json:"id"`
} `json:"items"`
}
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out2)
require.NoError(t, err)
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out2)
require.Equal(t, 0, len(out2.Items))
})
}

28
internal/core/hls_manager_test.go

@ -3,7 +3,6 @@ package core @@ -3,7 +3,6 @@ package core
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
@ -86,18 +85,19 @@ func (ts *testHTTPAuthenticator) onAuth(ctx *gin.Context) { @@ -86,18 +85,19 @@ func (ts *testHTTPAuthenticator) onAuth(ctx *gin.Context) {
}
}
func httpPullFile(u string) ([]byte, error) {
res, err := http.Get(u)
if err != nil {
return nil, err
}
func httpPullFile(t *testing.T, hc *http.Client, u string) []byte {
res, err := hc.Get(u)
require.NoError(t, err)
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status code: %v", res.StatusCode)
t.Errorf("bad status code: %v", res.StatusCode)
}
return io.ReadAll(res.Body)
byts, err := io.ReadAll(res.Body)
require.NoError(t, err)
return byts
}
func TestHLSReadNotFound(t *testing.T) {
@ -108,7 +108,9 @@ func TestHLSReadNotFound(t *testing.T) { @@ -108,7 +108,9 @@ func TestHLSReadNotFound(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8888/stream/", nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
hc := &http.Client{Transport: &http.Transport{}}
res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, http.StatusNotFound, res.StatusCode)
@ -161,8 +163,9 @@ func TestHLSRead(t *testing.T) { @@ -161,8 +163,9 @@ func TestHLSRead(t *testing.T) {
})
}
cnt, err := httpPullFile("http://localhost:8888/stream/index.m3u8")
require.NoError(t, err)
hc := &http.Client{Transport: &http.Transport{}}
cnt := httpPullFile(t, hc, "http://localhost:8888/stream/index.m3u8")
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-VERSION:9\n"+
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
@ -171,8 +174,7 @@ func TestHLSRead(t *testing.T) { @@ -171,8 +174,7 @@ func TestHLSRead(t *testing.T) {
"CODECS=\"avc1.42c028\",RESOLUTION=1920x1080,FRAME-RATE=30.000\n"+
"stream.m3u8\n", string(cnt))
cnt, err = httpPullFile("http://localhost:8888/stream/stream.m3u8")
require.NoError(t, err)
cnt = httpPullFile(t, hc, "http://localhost:8888/stream/stream.m3u8")
require.Regexp(t, "#EXTM3U\n"+
"#EXT-X-VERSION:9\n"+
"#EXT-X-TARGETDURATION:1\n"+

9
internal/core/metrics_test.go

@ -3,6 +3,7 @@ package core @@ -3,6 +3,7 @@ package core
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"os"
"testing"
@ -36,8 +37,9 @@ func TestMetrics(t *testing.T) { @@ -36,8 +37,9 @@ func TestMetrics(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
bo, err := httpPullFile("http://localhost:9998/metrics")
require.NoError(t, err)
hc := &http.Client{Transport: &http.Transport{}}
bo := httpPullFile(t, hc, "http://localhost:9998/metrics")
require.Equal(t, `paths 0
hls_muxers 0
@ -101,8 +103,7 @@ webrtc_sessions_bytes_sent 0 @@ -101,8 +103,7 @@ webrtc_sessions_bytes_sent 0
err = conn.WriteTracks(videoTrack, nil)
require.NoError(t, err)
bo, err = httpPullFile("http://localhost:9998/metrics")
require.NoError(t, err)
bo = httpPullFile(t, hc, "http://localhost:9998/metrics")
require.Regexp(t,
`^paths\{name=".*?",state="ready"\} 1`+"\n"+

34
internal/core/webrtc_manager_test.go

@ -17,11 +17,11 @@ import ( @@ -17,11 +17,11 @@ import (
"github.com/stretchr/testify/require"
)
func whipGetICEServers(t *testing.T, ur string) []webrtc.ICEServer {
func whipGetICEServers(t *testing.T, hc *http.Client, ur string) []webrtc.ICEServer {
req, err := http.NewRequest("OPTIONS", ur, nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
@ -35,7 +35,9 @@ func whipGetICEServers(t *testing.T, ur string) []webrtc.ICEServer { @@ -35,7 +35,9 @@ func whipGetICEServers(t *testing.T, ur string) []webrtc.ICEServer {
return servers
}
func whipPostOffer(t *testing.T, ur string, offer *webrtc.SessionDescription) (*webrtc.SessionDescription, string) {
func whipPostOffer(t *testing.T, hc *http.Client, ur string,
offer *webrtc.SessionDescription,
) (*webrtc.SessionDescription, string) {
enc, err := json.Marshal(offer)
require.NoError(t, err)
@ -44,7 +46,7 @@ func whipPostOffer(t *testing.T, ur string, offer *webrtc.SessionDescription) (* @@ -44,7 +46,7 @@ func whipPostOffer(t *testing.T, ur string, offer *webrtc.SessionDescription) (*
req.Header.Set("Content-Type", "application/sdp")
res, err := http.DefaultClient.Do(req)
res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
@ -79,7 +81,9 @@ func whipPostCandidate(t *testing.T, ur string, offer *webrtc.SessionDescription @@ -79,7 +81,9 @@ func whipPostCandidate(t *testing.T, ur string, offer *webrtc.SessionDescription
req.Header.Set("Content-Type", "application/trickle-ice-sdpfrag")
req.Header.Set("If-Match", etag)
res, err := http.DefaultClient.Do(req)
hc := &http.Client{Transport: &http.Transport{}}
res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
@ -94,8 +98,8 @@ type webRTCTestClient struct { @@ -94,8 +98,8 @@ type webRTCTestClient struct {
closed chan struct{}
}
func newWebRTCTestClient(t *testing.T, ur string, publish bool) *webRTCTestClient {
iceServers := whipGetICEServers(t, ur)
func newWebRTCTestClient(t *testing.T, hc *http.Client, ur string, publish bool) *webRTCTestClient {
iceServers := whipGetICEServers(t, hc, ur)
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
ICEServers: iceServers,
@ -170,7 +174,7 @@ func newWebRTCTestClient(t *testing.T, ur string, publish bool) *webRTCTestClien @@ -170,7 +174,7 @@ func newWebRTCTestClient(t *testing.T, ur string, publish bool) *webRTCTestClien
offer, err := pc.CreateOffer(nil)
require.NoError(t, err)
answer, etag := whipPostOffer(t, ur, &offer)
answer, etag := whipPostOffer(t, hc, ur, &offer)
// test adding additional candidates, even if it is not mandatory here
gatheringDone := make(chan struct{})
@ -260,7 +264,9 @@ func TestWebRTCRead(t *testing.T) { @@ -260,7 +264,9 @@ func TestWebRTCRead(t *testing.T) {
require.NoError(t, err)
defer source.Close()
c := newWebRTCTestClient(t, "http://localhost:8889/stream/whep", false)
hc := &http.Client{Transport: &http.Transport{}}
c := newWebRTCTestClient(t, hc, "http://localhost:8889/stream/whep", false)
defer c.close()
time.Sleep(500 * time.Millisecond)
@ -301,7 +307,9 @@ func TestWebRTCReadNotFound(t *testing.T) { @@ -301,7 +307,9 @@ func TestWebRTCReadNotFound(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
iceServers := whipGetICEServers(t, "http://localhost:8889/stream/whep")
hc := &http.Client{Transport: &http.Transport{}}
iceServers := whipGetICEServers(t, hc, "http://localhost:8889/stream/whep")
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
ICEServers: iceServers,
@ -323,7 +331,7 @@ func TestWebRTCReadNotFound(t *testing.T) { @@ -323,7 +331,7 @@ func TestWebRTCReadNotFound(t *testing.T) {
req.Header.Set("Content-Type", "application/sdp")
res, err := http.DefaultClient.Do(req)
res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
@ -336,7 +344,9 @@ func TestWebRTCPublish(t *testing.T) { @@ -336,7 +344,9 @@ func TestWebRTCPublish(t *testing.T) {
require.Equal(t, true, ok)
defer p.Close()
s := newWebRTCTestClient(t, "http://localhost:8889/stream/whip", true)
hc := &http.Client{Transport: &http.Transport{}}
s := newWebRTCTestClient(t, hc, "http://localhost:8889/stream/whip", true)
defer s.close()
c := gortsplib.Client{

4
internal/highleveltests/hls_server_test.go

@ -171,7 +171,9 @@ func TestHLSServerAuth(t *testing.T) { @@ -171,7 +171,9 @@ func TestHLSServerAuth(t *testing.T) {
usr = "testreader2"
}
res, err := http.Get("http://" + usr + ":testpass@127.0.0.1:8888/teststream/index.m3u8?param=value")
hc := &http.Client{Transport: &http.Transport{}}
res, err := hc.Get("http://" + usr + ":testpass@127.0.0.1:8888/teststream/index.m3u8?param=value")
require.NoError(t, err)
defer res.Body.Close()

Loading…
Cancel
Save