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 (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"testing"
|
||||
|
@ -74,33 +75,68 @@ func TestReceive(t *testing.T) {
|
|||
packetsToSend = 20
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
c.Start()
|
||||
for _, op := range []func([]byte) ([]byte, error){nil, Payload} {
|
||||
testErr := make(chan error)
|
||||
serverErr := make(chan error)
|
||||
done := make(chan struct{})
|
||||
clientReady := make(chan struct{})
|
||||
var c *Client
|
||||
|
||||
// Log any errors from client.
|
||||
// Start routine to read from client.
|
||||
go func() {
|
||||
for {
|
||||
err := <-c.Err()
|
||||
t.Logf("unexpected error from client: %v", err)
|
||||
// Create and start the client.
|
||||
var err error
|
||||
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() {
|
||||
<-clientReady
|
||||
cAddr, err := net.ResolveUDPAddr("udp", clientAddr)
|
||||
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)
|
||||
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.
|
||||
|
@ -108,42 +144,25 @@ func TestReceive(t *testing.T) {
|
|||
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)
|
||||
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.
|
||||
var packetsReceived int
|
||||
buf := make([]byte, 4096)
|
||||
<-clientReady
|
||||
loop:
|
||||
for {
|
||||
if packetsReceived == packetsToSend {
|
||||
break
|
||||
}
|
||||
|
||||
n, err := c.Read(buf)
|
||||
switch err {
|
||||
case nil:
|
||||
case io.EOF:
|
||||
continue
|
||||
select {
|
||||
case err := <-c.Err():
|
||||
t.Log(err)
|
||||
case err := <-testErr:
|
||||
t.Fatal(err)
|
||||
case err := <-serverErr:
|
||||
t.Fatal(err)
|
||||
case <-done:
|
||||
break loop
|
||||
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