protocol/rtp: made client_test.go more robust

This commit is contained in:
Saxon 2019-04-30 09:39:41 +09:30
parent 6694cab956
commit d358f70585
1 changed files with 63 additions and 44 deletions

View File

@ -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{})
clientReady := make(chan struct{})
var c *Client
// Start routine to read from client.
go func() {
// Create and start the client.
var err error
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) testErr <- fmt.Errorf("could not create client, failed with error: %v\n", err)
} }
c.Start() c.Start()
close(clientReady)
// Log any errors from client. // Read packets using the client and check them with expected.
go func() { var packetsReceived int
for { buf := make([]byte, 4096)
err := <-c.Err() for packetsReceived != packetsToSend {
t.Logf("unexpected error from client: %v", err) 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()
} }
} }