protocol/rtp: simplified test

Simplified clien_test.go file by testing two different RTP packet operations using loop. We now first test no operation,
and then test a rtp.Payload operation, which gets the payload of the packets and stores them in the client ringbuffer for the
user of the client to read.
This commit is contained in:
Saxon 2019-04-21 23:07:12 +09:30
parent be76998c7d
commit a0c324a813
2 changed files with 73 additions and 64 deletions

View File

@ -160,7 +160,7 @@ func (c *Client) recv() {
//
// Read will get the next chunk from the ringBuffer and copy the bytes to p.
func (c *Client) Read(p []byte) (int, error) {
c.log(logger.Debug, pkg+"user reading packet")
c.log(logger.Debug, pkg+"user reading data")
chunk, err := c.ring.Next(c.rt)
if err != nil {
return 0, io.EOF

View File

@ -66,15 +66,18 @@ func (dl *dummyLogger) log(lvl int8, msg string, args ...interface{}) {
dl.Logf(msg+"\n", args...)
}
// TestReceiveNoOp will check that we can successfully use a Client to receive
// RTP using no operation.
func TestReceiveNoOp(t *testing.T) {
const clientAddr = "localhost:8000"
const packetsToSend = 20
// TestReceive checks that the Client can correctly receive RTP packets and
// perform a specificed operation on the packets before storing in the ringBuffer.
func TestReceive(t *testing.T) {
const (
clientAddr = "localhost:8000"
packetsToSend = 20
)
// Create new client; note that op function set to nil, i.e. we don't want to
// perform any operation on packet before storing in ringbuffer.
c, err := NewClient(clientAddr, nil, (*dummyLogger)(t).log, 1*time.Millisecond)
for i, op := range []func([]byte) ([]byte, error){nil, Payload} {
t.Logf("running op: %v", i)
// Create new client and give current operation we are testing.
c, err := NewClient(clientAddr, op, (*dummyLogger)(t).log, 1*time.Millisecond)
if err != nil {
t.Fatalf("could not create client, failed with error: %v", err)
}
@ -100,7 +103,7 @@ func TestReceiveNoOp(t *testing.T) {
t.Fatalf("could not dial udp, failed with err: %v", err)
}
// Send packets to the client. Packet payload will just be the packet number.
// Send packets to the client.
for i := 0; i < packetsToSend; i++ {
p := (&Pkt{V: rtpVer, Payload: []byte{byte(i)}}).Bytes(nil)
_, err := conn.Write(p)
@ -117,6 +120,7 @@ func TestReceiveNoOp(t *testing.T) {
if packetsReceived == packetsToSend {
break
}
n, err := c.Read(buf)
switch err {
case nil:
@ -125,7 +129,15 @@ func TestReceiveNoOp(t *testing.T) {
default:
t.Fatalf("unexpected error: %v", err)
}
expect := (&Pkt{V: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil)
if op != nil {
expect, err = op(expect)
if err != nil {
t.Fatalf("unexpected error when applying op: %v", err)
}
}
got := buf[:n]
if !bytes.Equal(got, expect) {
t.Errorf("did not get expected result. \nGot: %v\n Want: %v\n", got, expect)
@ -134,7 +146,4 @@ func TestReceiveNoOp(t *testing.T) {
}
c.Stop()
}
func TestReceiveOp(t *testing.T) {
}