diff --git a/protocol/rtp/client.go b/protocol/rtp/client.go index 00682308..daab5fee 100644 --- a/protocol/rtp/client.go +++ b/protocol/rtp/client.go @@ -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 diff --git a/protocol/rtp/client_test.go b/protocol/rtp/client_test.go index d3f3fcb6..23753c64 100644 --- a/protocol/rtp/client_test.go +++ b/protocol/rtp/client_test.go @@ -66,75 +66,84 @@ 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) - if err != nil { - t.Fatalf("could not create client, failed with error: %v", err) - } - c.Start() - - // Log any errors from client. - go func() { - for { - err := <-c.ErrChan - t.Logf("unexpected error from client: %v", err) - } - }() - - // Start the RTP 'server'. - go func() { - cAddr, err := net.ResolveUDPAddr("udp", clientAddr) + 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 resolve server address, failed with err: %v", err) + t.Fatalf("could not create client, failed with error: %v", err) } + c.Start() - conn, err := net.DialUDP("udp", nil, cAddr) - if err != nil { - t.Fatalf("could not dial udp, failed with err: %v", err) - } - - // Send packets to the client. Packet payload will just be the packet number. - for i := 0; i < packetsToSend; i++ { - p := (&Pkt{V: rtpVer, Payload: []byte{byte(i)}}).Bytes(nil) - _, err := conn.Write(p) - if err != nil { - t.Errorf("could not write packet to conn, failed with err: %v", err) + // Log any errors from client. + go func() { + for { + err := <-c.ErrChan + t.Logf("unexpected error from client: %v", err) } - } - }() + }() - // Read packets using the client and check them with expected. - var packetsReceived int - buf := make([]byte, 4096) - for { - if packetsReceived == packetsToSend { - break + // Start the RTP 'server'. + go func() { + cAddr, err := net.ResolveUDPAddr("udp", clientAddr) + if err != nil { + t.Fatalf("could not resolve server address, failed with err: %v", err) + } + + conn, err := net.DialUDP("udp", nil, cAddr) + if err != nil { + t.Fatalf("could not dial udp, failed with err: %v", err) + } + + // 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) + if err != nil { + t.Errorf("could not write packet to conn, failed with err: %v", err) + } + } + }() + + // Read packets using the client and check them with expected. + var packetsReceived int + buf := make([]byte, 4096) + for { + if packetsReceived == packetsToSend { + break + } + + n, err := c.Read(buf) + switch err { + case nil: + case io.EOF: + continue + 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) + } + packetsReceived++ } - n, err := c.Read(buf) - switch err { - case nil: - case io.EOF: - continue - default: - t.Fatalf("unexpected error: %v", err) - } - expect := (&Pkt{V: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil) - got := buf[:n] - if !bytes.Equal(got, expect) { - t.Errorf("did not get expected result. \nGot: %v\n Want: %v\n", got, expect) - } - packetsReceived++ + c.Stop() } - c.Stop() -} - -func TestReceiveOp(t *testing.T) { - }