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. // Read will get the next chunk from the ringBuffer and copy the bytes to p.
func (c *Client) Read(p []byte) (int, error) { 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) chunk, err := c.ring.Next(c.rt)
if err != nil { if err != nil {
return 0, io.EOF 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...) dl.Logf(msg+"\n", args...)
} }
// TestReceiveNoOp will check that we can successfully use a Client to receive // TestReceive checks that the Client can correctly receive RTP packets and
// RTP using no operation. // perform a specificed operation on the packets before storing in the ringBuffer.
func TestReceiveNoOp(t *testing.T) { func TestReceive(t *testing.T) {
const clientAddr = "localhost:8000" const (
const packetsToSend = 20 clientAddr = "localhost:8000"
packetsToSend = 20
)
// Create new client; note that op function set to nil, i.e. we don't want to for i, op := range []func([]byte) ([]byte, error){nil, Payload} {
// perform any operation on packet before storing in ringbuffer. t.Logf("running op: %v", i)
c, err := NewClient(clientAddr, nil, (*dummyLogger)(t).log, 1*time.Millisecond) // Create new client and give current operation we are testing.
c, err := NewClient(clientAddr, op, (*dummyLogger)(t).log, 1*time.Millisecond)
if err != nil { if err != nil {
t.Fatalf("could not create client, failed with error: %v", err) 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) 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++ { for i := 0; i < packetsToSend; i++ {
p := (&Pkt{V: rtpVer, Payload: []byte{byte(i)}}).Bytes(nil) p := (&Pkt{V: rtpVer, Payload: []byte{byte(i)}}).Bytes(nil)
_, err := conn.Write(p) _, err := conn.Write(p)
@ -117,6 +120,7 @@ func TestReceiveNoOp(t *testing.T) {
if packetsReceived == packetsToSend { if packetsReceived == packetsToSend {
break break
} }
n, err := c.Read(buf) n, err := c.Read(buf)
switch err { switch err {
case nil: case nil:
@ -125,7 +129,15 @@ func TestReceiveNoOp(t *testing.T) {
default: default:
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
expect := (&Pkt{V: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil) 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] got := buf[:n]
if !bytes.Equal(got, expect) { if !bytes.Equal(got, expect) {
t.Errorf("did not get expected result. \nGot: %v\n Want: %v\n", 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() c.Stop()
} }
func TestReceiveOp(t *testing.T) {
} }