mirror of https://bitbucket.org/ausocean/av.git
protocol/rtp: made client_test.go more robust
This commit is contained in:
parent
6694cab956
commit
d358f70585
|
@ -30,6 +30,7 @@ package rtp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -74,33 +75,68 @@ func TestReceive(t *testing.T) {
|
||||||
packetsToSend = 20
|
packetsToSend = 20
|
||||||
)
|
)
|
||||||
|
|
||||||
for i, op := range []func([]byte) ([]byte, error){nil, Payload} {
|
for _, op := range []func([]byte) ([]byte, error){nil, Payload} {
|
||||||
t.Logf("running op: %v", i)
|
testErr := make(chan error)
|
||||||
// Create new client and give current operation we are testing.
|
serverErr := make(chan error)
|
||||||
c, err := NewClient(clientAddr, op, (*dummyLogger)(t).log, 1*time.Millisecond)
|
done := make(chan struct{})
|
||||||
if err != nil {
|
clientReady := make(chan struct{})
|
||||||
t.Fatalf("could not create client, failed with error: %v", err)
|
var c *Client
|
||||||
}
|
|
||||||
c.Start()
|
|
||||||
|
|
||||||
// Log any errors from client.
|
// Start routine to read from client.
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
// Create and start the client.
|
||||||
err := <-c.Err()
|
var err error
|
||||||
t.Logf("unexpected error from client: %v", err)
|
c, err = NewClient(clientAddr, op, (*dummyLogger)(t).log, 1*time.Millisecond)
|
||||||
|
if err != nil {
|
||||||
|
testErr <- fmt.Errorf("could not create client, failed with error: %v\n", err)
|
||||||
}
|
}
|
||||||
|
c.Start()
|
||||||
|
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:
|
||||||
|
testErr <- fmt.Errorf("unexpected error from c.Read: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create expected data and apply operation if there is one.
|
||||||
|
expect := (&Pkt{V: rtpVer, Payload: []byte{byte(packetsReceived)}}).Bytes(nil)
|
||||||
|
if op != nil {
|
||||||
|
expect, err = op(expect)
|
||||||
|
if err != nil {
|
||||||
|
testErr <- fmt.Errorf("unexpected error when applying op: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
packetsReceived++
|
||||||
|
}
|
||||||
|
c.Stop()
|
||||||
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Start the RTP 'server'.
|
// Start the RTP server.
|
||||||
go func() {
|
go func() {
|
||||||
|
<-clientReady
|
||||||
cAddr, err := net.ResolveUDPAddr("udp", clientAddr)
|
cAddr, err := net.ResolveUDPAddr("udp", clientAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not resolve server address, failed with err: %v", err)
|
serverErr <- fmt.Errorf("could not resolve server address, failed with err: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := net.DialUDP("udp", nil, cAddr)
|
conn, err := net.DialUDP("udp", nil, cAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not dial udp, failed with err: %v", err)
|
serverErr <- fmt.Errorf("could not dial udp, failed with err: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send packets to the client.
|
// Send packets to the client.
|
||||||
|
@ -108,42 +144,25 @@ func TestReceive(t *testing.T) {
|
||||||
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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not write packet to conn, failed with err: %v", err)
|
serverErr <- fmt.Errorf("could not write packet to conn, failed with err: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Read packets using the client and check them with expected.
|
<-clientReady
|
||||||
var packetsReceived int
|
loop:
|
||||||
buf := make([]byte, 4096)
|
|
||||||
for {
|
for {
|
||||||
if packetsReceived == packetsToSend {
|
select {
|
||||||
break
|
case err := <-c.Err():
|
||||||
}
|
t.Log(err)
|
||||||
|
case err := <-testErr:
|
||||||
n, err := c.Read(buf)
|
t.Fatal(err)
|
||||||
switch err {
|
case err := <-serverErr:
|
||||||
case nil:
|
t.Fatal(err)
|
||||||
case io.EOF:
|
case <-done:
|
||||||
continue
|
break loop
|
||||||
default:
|
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++
|
|
||||||
}
|
}
|
||||||
c.Stop()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue