mediamtx/internal/rtmp/rawmessage/writer_test.go

70 lines
1.6 KiB
Go
Raw Normal View History

2022-06-07 19:09:57 +00:00
package rawmessage
import (
"bytes"
"reflect"
2022-06-07 19:09:57 +00:00
"testing"
"time"
2022-06-07 19:09:57 +00:00
2023-05-16 14:14:20 +00:00
"github.com/bluenviron/mediamtx/internal/rtmp/bytecounter"
"github.com/bluenviron/mediamtx/internal/rtmp/chunk"
2022-06-07 19:09:57 +00:00
"github.com/stretchr/testify/require"
)
func TestWriter(t *testing.T) {
2022-07-19 14:42:11 +00:00
for _, ca := range cases {
t.Run(ca.name, func(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(bytecounter.NewWriter(&buf), true)
2022-06-07 19:09:57 +00:00
for _, msg := range ca.messages {
err := w.Write(msg)
require.NoError(t, err)
}
2022-06-07 19:09:57 +00:00
for i, cach := range ca.chunks {
ch := reflect.New(reflect.TypeOf(cach).Elem()).Interface().(chunk.Chunk)
err := ch.Read(&buf, ca.chunkSizes[i])
require.NoError(t, err)
require.Equal(t, cach, ch)
}
2022-06-07 19:09:57 +00:00
})
}
2022-06-07 19:09:57 +00:00
}
2022-06-08 18:47:36 +00:00
func TestWriterAcknowledge(t *testing.T) {
2022-07-16 10:42:48 +00:00
for _, ca := range []string{"standard", "overflow"} {
t.Run(ca, func(t *testing.T) {
var buf bytes.Buffer
bcw := bytecounter.NewWriter(&buf)
w := NewWriter(bcw, true)
2022-07-16 10:42:48 +00:00
if ca == "overflow" {
bcw.SetCount(4294967096)
w.ackValue = 4294967096
}
w.SetChunkSize(65536)
w.SetWindowAckSize(100)
err := w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576 * time.Millisecond,
Type: 6,
2022-07-16 10:42:48 +00:00
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x03}, 200),
})
require.NoError(t, err)
err = w.Write(&Message{
ChunkStreamID: 27,
Timestamp: 18576 * time.Millisecond,
Type: 6,
2022-07-16 10:42:48 +00:00
MessageStreamID: 3123,
Body: bytes.Repeat([]byte{0x03}, 200),
})
require.EqualError(t, err, "no acknowledge received within window")
2022-06-08 18:47:36 +00:00
})
}
}