Skip to content

Commit

Permalink
[test] 提高测试覆盖
Browse files Browse the repository at this point in the history
  • Loading branch information
q191201771 committed Dec 11, 2021
1 parent 3309992 commit ad547d0
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 61 deletions.
2 changes: 1 addition & 1 deletion app/demo/calcrtmpdelay/calcrtmpdelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func main() {
}()

// 读取flv文件
flvFilePump := httpflv.NewFileFilePump(func(option *httpflv.FlvFilePumpOption) {
flvFilePump := httpflv.NewFlvFilePump(func(option *httpflv.FlvFilePumpOption) {
option.IsRecursive = false
})
err = flvFilePump.Pump(filename, func(tag httpflv.Tag) bool {
Expand Down
2 changes: 1 addition & 1 deletion app/demo/pushrtmp/pushrtmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func push(tags []httpflv.Tag, url string, isRecursive bool) {
nazalog.Infof("push succ. url=%s", url)

go func() {
flvFilePump := httpflv.NewFileFilePump(func(option *httpflv.FlvFilePumpOption) {
flvFilePump := httpflv.NewFlvFilePump(func(option *httpflv.FlvFilePumpOption) {
option.IsRecursive = isRecursive
})
_ = flvFilePump.PumpWithTags(tags, func(tag httpflv.Tag) bool {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/q191201771/lal

go 1.14

require github.com/q191201771/naza v0.28.0
require github.com/q191201771/naza v0.28.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/q191201771/naza v0.28.0 h1:CMfs41VikFMeZJEZ+vflf+K70ILg4QunDuQiomHPWM8=
github.com/q191201771/naza v0.28.0/go.mod h1:n+dpJjQSh90PxBwxBNuifOwQttywvSIN5TkWSSYCeBk=
github.com/q191201771/naza v0.28.1 h1:lDy+U0QCls4G91kbV9WZgAJh7oDDoh49Z4pojur9ZKw=
github.com/q191201771/naza v0.28.1/go.mod h1:n+dpJjQSh90PxBwxBNuifOwQttywvSIN5TkWSSYCeBk=
3 changes: 3 additions & 0 deletions pkg/base/avpacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ const (
AvPacketPtAac AvPacketPt = RtpPacketTypeAac
)

// AvPacket
//
// 不同场景使用时,字段含义可能不同。
// 使用AvPacket的地方,应注明各字段的含义。
//
type AvPacket struct {
Timestamp uint32
PayloadType AvPacketPt
Expand Down
19 changes: 19 additions & 0 deletions pkg/base/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)

package base_test

import (
"testing"

"github.com/q191201771/lal/pkg/innertest"
)

func TestBase(t *testing.T) {
innertest.Entry(t)
}
20 changes: 10 additions & 10 deletions pkg/base/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestParseUrl(t *testing.T) {

golden := map[in]base.UrlContext{
// 常见url,url中无端口,另外设置默认端口
in{rawUrl: "rtmp://127.0.0.1/live/test110", defaultPort: 1935}: {
{rawUrl: "rtmp://127.0.0.1/live/test110", defaultPort: 1935}: {
Url: "rtmp://127.0.0.1/live/test110",
Scheme: "rtmp",
StdHost: "127.0.0.1",
Expand All @@ -44,7 +44,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://127.0.0.1/live/test110",
},
// 域名url
in{rawUrl: "rtmp://localhost/live/test110", defaultPort: 1935}: {
{rawUrl: "rtmp://localhost/live/test110", defaultPort: 1935}: {
Url: "rtmp://localhost/live/test110",
Scheme: "rtmp",
StdHost: "localhost",
Expand All @@ -59,7 +59,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://localhost/live/test110",
},
// 带参数url
in{rawUrl: "rtmp://127.0.0.1/live/test110?a=1", defaultPort: 1935}: {
{rawUrl: "rtmp://127.0.0.1/live/test110?a=1", defaultPort: 1935}: {
Url: "rtmp://127.0.0.1/live/test110?a=1",
Scheme: "rtmp",
StdHost: "127.0.0.1",
Expand All @@ -74,7 +74,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://127.0.0.1/live/test110?a=1",
},
// path多级
in{rawUrl: "rtmp://127.0.0.1:19350/a/b/test110", defaultPort: 1935}: {
{rawUrl: "rtmp://127.0.0.1:19350/a/b/test110", defaultPort: 1935}: {
Url: "rtmp://127.0.0.1:19350/a/b/test110",
Scheme: "rtmp",
StdHost: "127.0.0.1:19350",
Expand All @@ -89,7 +89,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://127.0.0.1:19350/a/b/test110",
},
// url中无端口,没有设置默认端口
in{rawUrl: "rtmp://127.0.0.1/live/test110?a=1", defaultPort: -1}: {
{rawUrl: "rtmp://127.0.0.1/live/test110?a=1", defaultPort: -1}: {
Url: "rtmp://127.0.0.1/live/test110?a=1",
Scheme: "rtmp",
StdHost: "127.0.0.1",
Expand All @@ -104,7 +104,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://127.0.0.1/live/test110?a=1",
},
// url中有端口,设置默认端口
in{rawUrl: "rtmp://127.0.0.1:19350/live/test110?a=1", defaultPort: 1935}: {
{rawUrl: "rtmp://127.0.0.1:19350/live/test110?a=1", defaultPort: 1935}: {
Url: "rtmp://127.0.0.1:19350/live/test110?a=1",
Scheme: "rtmp",
StdHost: "127.0.0.1:19350",
Expand All @@ -119,7 +119,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://127.0.0.1:19350/live/test110?a=1",
},
// 无path
in{rawUrl: "rtmp://127.0.0.1:19350", defaultPort: 1935}: {
{rawUrl: "rtmp://127.0.0.1:19350", defaultPort: 1935}: {
Url: "rtmp://127.0.0.1:19350",
Scheme: "rtmp",
StdHost: "127.0.0.1:19350",
Expand All @@ -134,7 +134,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://127.0.0.1:19350",
},
// 无path2
in{rawUrl: "rtmp://127.0.0.1:19350/", defaultPort: 1935}: {
{rawUrl: "rtmp://127.0.0.1:19350/", defaultPort: 1935}: {
Url: "rtmp://127.0.0.1:19350/",
Scheme: "rtmp",
StdHost: "127.0.0.1:19350",
Expand All @@ -149,7 +149,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://127.0.0.1:19350/",
},
// path不完整
in{rawUrl: "rtmp://127.0.0.1:19350/live", defaultPort: 1935}: {
{rawUrl: "rtmp://127.0.0.1:19350/live", defaultPort: 1935}: {
Url: "rtmp://127.0.0.1:19350/live",
Scheme: "rtmp",
StdHost: "127.0.0.1:19350",
Expand All @@ -164,7 +164,7 @@ func TestParseUrl(t *testing.T) {
RawUrlWithoutUserInfo: "rtmp://127.0.0.1:19350/live",
},
// path不完整2
in{rawUrl: "rtmp://127.0.0.1:19350/live/", defaultPort: 1935}: {
{rawUrl: "rtmp://127.0.0.1:19350/live/", defaultPort: 1935}: {
Url: "rtmp://127.0.0.1:19350/live/",
Scheme: "rtmp",
StdHost: "127.0.0.1:19350",
Expand Down
2 changes: 1 addition & 1 deletion pkg/hls/hls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ import (
)

func TestHls(t *testing.T) {
innertest.InnerTestEntry(t)
innertest.Entry(t)
}
19 changes: 17 additions & 2 deletions pkg/httpflv/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@
package httpflv

import (
"os"
"testing"

"github.com/q191201771/naza/pkg/nazalog"

"github.com/q191201771/naza/pkg/assert"
)

func BenchmarkFlvFileReader(b *testing.B) {
const flvFile = "testdata/test.flv"
if _, err := os.Lstat(flvFile); err != nil {
nazalog.Warnf("lstat %s error. err=%+v", flvFile, err)
return
}

var tmp uint32
for i := 0; i < b.N; i++ {
var r FlvFileReader
err := r.Open("testdata/test.flv")
err := r.Open(flvFile)
assert.Equal(b, nil, err)
for {
tag, err := r.ReadTag()
Expand All @@ -33,9 +42,15 @@ func BenchmarkFlvFileReader(b *testing.B) {
}

func BenchmarkCloneTag(b *testing.B) {
const flvFile = "testdata/test.flv"
if _, err := os.Lstat(flvFile); err != nil {
nazalog.Warnf("lstat %s error. err=%+v", flvFile, err)
return
}

var tmp uint32
var r FlvFileReader
err := r.Open("testdata/test.flv")
err := r.Open(flvFile)
assert.Equal(b, nil, err)
tag, err := r.ReadTag()
assert.Equal(b, nil, err)
Expand Down
18 changes: 13 additions & 5 deletions pkg/httpflv/flv_file_pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ package httpflv
import (
"time"

"github.com/q191201771/naza/pkg/mock"

"github.com/q191201771/naza/pkg/nazalog"
)

// 读取flv文件,将tag按时间戳间隔缓慢(类似于ffmpeg的-re)返回
var Clock = mock.NewStdClock()

// FlvFilePumpOption
//
// 读取flv文件,将tag按时间戳间隔缓慢(类似于ffmpeg的-re)返回
//
type FlvFilePumpOption struct {
IsRecursive bool // 如果为true,则循环返回文件内容(类似于ffmpeg的-stream_loop -1)
}
Expand All @@ -30,7 +36,7 @@ type FlvFilePump struct {

type ModFlvFilePumpOption func(option *FlvFilePumpOption)

func NewFileFilePump(modOptions ...ModFlvFilePumpOption) *FlvFilePump {
func NewFlvFilePump(modOptions ...ModFlvFilePumpOption) *FlvFilePump {
option := defaultFlvFilePumpOption
for _, fn := range modOptions {
fn(&option)
Expand All @@ -41,6 +47,8 @@ func NewFileFilePump(modOptions ...ModFlvFilePumpOption) *FlvFilePump {

type OnPumpFlvTag func(tag Tag) bool

// Pump
//
// @param onFlvTag 如果回调中返回false,则停止Pump
//
func (f *FlvFilePump) Pump(filename string, onFlvTag OnPumpFlvTag) error {
Expand Down Expand Up @@ -112,16 +120,16 @@ func (f *FlvFilePump) PumpWithTags(tags []Tag, onFlvTag OnPumpFlvTag) error {
diffTs := tag.Header.Timestamp - totalFirstTagTs

// 当前物理时间与第一轮的第一个tag的物理时间差值
diffTick := time.Now().UnixNano()/1000000 - totalFirstTagTick
diffTick := Clock.Now().UnixNano()/1000000 - totalFirstTagTick

// 如果还没到物理时间差值,就sleep
if diffTick < int64(diffTs) {
time.Sleep(time.Duration(int64(diffTs)-diffTick) * time.Millisecond)
Clock.Sleep(time.Duration(int64(diffTs)-diffTick) * time.Millisecond)
}
} else {
// 第一轮的第一个tag,记录下来

totalFirstTagTick = time.Now().UnixNano() / 1000000
totalFirstTagTick = Clock.Now().UnixNano() / 1000000
totalFirstTagTs = tag.Header.Timestamp
hasReadTotalFirstTag = true
}
Expand Down
46 changes: 44 additions & 2 deletions pkg/httpflv/httpflv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,53 @@
package httpflv_test

import (
"fmt"
"os"
"testing"

"github.com/q191201771/naza/pkg/nazalog"

"github.com/q191201771/lal/pkg/httpflv"
"github.com/q191201771/naza/pkg/assert"
"github.com/q191201771/naza/pkg/mock"
"github.com/q191201771/naza/pkg/nazamd5"

"github.com/q191201771/lal/pkg/innertest"
)

func TestRtmp(t *testing.T) {
innertest.InnerTestEntry(t)
func TestHttpflv(t *testing.T) {
innertest.Entry(t)
}

func TestFlvFilePump(t *testing.T) {
const flvFile = "../../testdata/test.flv"
if _, err := os.Lstat(flvFile); err != nil {
nazalog.Warnf("lstat %s error. err=%+v", flvFile, err)
return
}

var (
//headers []byte
tagCount int
allHeader []byte
allRaw []byte
)

httpflv.Clock = mock.NewFakeClock()
defer func() {
httpflv.Clock = mock.NewStdClock()
}()

ffp := httpflv.NewFlvFilePump()
err := ffp.Pump(flvFile, func(tag httpflv.Tag) bool {
tagCount++
allRaw = append(allRaw, tag.Raw...)
h := fmt.Sprintf("%+v", tag.Header)
allHeader = append(allHeader, []byte(h)...)
return true
})
assert.Equal(t, nil, err)
assert.Equal(t, 1746, tagCount)
assert.Equal(t, "ab7f75d2491711cc9a8d0ccd5d56280b", nazamd5.Md5(allRaw))
assert.Equal(t, "2a1cd1bd99f725c19bbd45d81d436e59", nazamd5.Md5(allHeader))
}
20 changes: 14 additions & 6 deletions pkg/innertest/innertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ import (
var (
tt *testing.T

confFile = "testdata/lalserver.conf.json"

rFlvFileName = "testdata/test.flv"
wFlvPullFileName = "testdata/flvpull.flv"
wRtmpPullFileName = "testdata/rtmppull.flv"
confFile = "../../testdata/lalserver.conf.json"
rFlvFileName = "../../testdata/test.flv"
wFlvPullFileName = "../../testdata/flvpull.flv"
wRtmpPullFileName = "../../testdata/rtmppull.flv"

pushUrl string
httpflvPullUrl string
Expand All @@ -69,7 +68,16 @@ var (
rtmpPullTagCount nazaatomic.Uint32
)

func InnerTestEntry(t *testing.T) {
func Entry(t *testing.T) {
if _, err := os.Lstat(confFile); err != nil {
nazalog.Warnf("lstat %s error. err=%+v", confFile, err)
return
}
if _, err := os.Lstat(rFlvFileName); err != nil {
nazalog.Warnf("lstat %s error. err=%+v", rFlvFileName, err)
return
}

tt = t

var err error
Expand Down
2 changes: 1 addition & 1 deletion pkg/logic/logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ import (
)

func TestLogic(t *testing.T) {
innertest.InnerTestEntry(t)
innertest.Entry(t)
}
6 changes: 6 additions & 0 deletions pkg/mpegts/mpegts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ package mpegts_test
import (
"testing"

"github.com/q191201771/lal/pkg/innertest"

"github.com/q191201771/lal/pkg/mpegts"
"github.com/q191201771/naza/pkg/nazalog"
)

func TestMpegts(t *testing.T) {
innertest.Entry(t)
}

func TestParseFixedTsPacket(t *testing.T) {
h := mpegts.ParseTsPacketHeader(mpegts.FixedFragmentHeader)
nazalog.Debugf("%+v", h)
Expand Down
19 changes: 19 additions & 0 deletions pkg/remux/remux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)

package remux_test

import (
"testing"

"github.com/q191201771/lal/pkg/innertest"
)

func TestRemux(t *testing.T) {
innertest.Entry(t)
}
2 changes: 1 addition & 1 deletion pkg/rtmp/rtmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ import (
)

func TestRtmp(t *testing.T) {
innertest.InnerTestEntry(t)
innertest.Entry(t)
}
Loading

0 comments on commit ad547d0

Please sign in to comment.