2019-04-20 14:40:03 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
client_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
client_test.go provides testing utilities to check RTP client functionality
|
|
|
|
provided in client.go.
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This is Copyright (C) 2019 the Australian Ocean Lab (AusOcean).
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rtp
|
|
|
|
|
2019-04-21 16:10:08 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
2019-04-30 03:09:41 +03:00
|
|
|
"fmt"
|
2019-04-21 16:10:08 +03:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-04-21 16:37:12 +03:00
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
2019-05-01 07:45:39 +03:00
|
|
|
testErr := make(chan error)
|
|
|
|
serverErr := make(chan error)
|
|
|
|
done := make(chan struct{})
|
|
|
|
clientReady := make(chan struct{})
|
|
|
|
var c *Client
|
|
|
|
|
|
|
|
// Start routine to read from client.
|
|
|
|
go func() {
|
|
|
|
// Create and start the client.
|
|
|
|
var err error
|
2019-05-08 10:24:02 +03:00
|
|
|
c, err = NewClient(clientAddr)
|
2019-05-01 07:45:39 +03:00
|
|
|
if err != nil {
|
2019-12-04 06:42:20 +03:00
|
|
|
testErr <- fmt.Errorf("could not create client, failed with error: %w\n", err)
|
2019-05-01 07:45:39 +03:00
|
|
|
}
|
|
|
|
close(clientReady)
|
|
|
|
|
|
|
|
// Read packets using the client and check them with expected.
|
|
|
|
var packetsReceived int
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
for packetsReceived != packetsToSend {
|
|
|
|
n, err := c.Read(buf)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case io.EOF:
|
|
|
|
continue
|
|
|
|
default:
|
2019-12-04 06:42:20 +03:00
|
|
|
testErr <- fmt.Errorf("unexpected error from c.Read: %w\n", err)
|
2019-04-21 16:10:08 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 07:45:39 +03:00
|
|
|
// Create expected data and apply operation if there is one.
|
2019-05-08 09:15:08 +03:00
|
|
|
expect := (&Packet{V: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil)
|
2019-04-21 16:37:12 +03:00
|
|
|
|
2019-05-01 07:45:39 +03:00
|
|
|
// Compare.
|
|
|
|
got := buf[:n]
|
|
|
|
if !bytes.Equal(got, expect) {
|
|
|
|
testErr <- fmt.Errorf("did not get expected result. \nGot: %v\n Want: %v\n", got, expect)
|
2019-04-21 16:37:12 +03:00
|
|
|
}
|
2019-05-01 07:45:39 +03:00
|
|
|
packetsReceived++
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
}()
|
2019-04-21 16:37:12 +03:00
|
|
|
|
2019-05-01 07:45:39 +03:00
|
|
|
// Start the RTP server.
|
|
|
|
go func() {
|
2019-04-30 03:09:41 +03:00
|
|
|
<-clientReady
|
2019-05-01 07:45:39 +03:00
|
|
|
cAddr, err := net.ResolveUDPAddr("udp", clientAddr)
|
|
|
|
if err != nil {
|
2019-12-04 06:42:20 +03:00
|
|
|
serverErr <- fmt.Errorf("could not resolve server address, failed with err: %w\n", err)
|
2019-05-01 07:45:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.DialUDP("udp", nil, cAddr)
|
|
|
|
if err != nil {
|
2019-12-04 06:42:20 +03:00
|
|
|
serverErr <- fmt.Errorf("could not dial udp, failed with err: %w\n", err)
|
2019-05-01 07:45:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send packets to the client.
|
|
|
|
for i := 0; i < packetsToSend; i++ {
|
2019-05-08 09:15:08 +03:00
|
|
|
p := (&Packet{V: rtpVer, Payload: []byte{byte(i)}}).Bytes(nil)
|
2019-05-01 07:45:39 +03:00
|
|
|
_, err := conn.Write(p)
|
|
|
|
if err != nil {
|
2019-12-04 06:42:20 +03:00
|
|
|
serverErr <- fmt.Errorf("could not write packet to conn, failed with err: %w\n", err)
|
2019-04-21 16:37:12 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-01 07:45:39 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
<-clientReady
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-testErr:
|
|
|
|
t.Fatal(err)
|
|
|
|
case err := <-serverErr:
|
|
|
|
t.Fatal(err)
|
|
|
|
case <-done:
|
|
|
|
break loop
|
|
|
|
default:
|
|
|
|
}
|
2019-04-21 16:37:12 +03:00
|
|
|
}
|
2019-04-20 14:40:03 +03:00
|
|
|
}
|