mirror of https://bitbucket.org/ausocean/av.git
141 lines
3.3 KiB
Go
141 lines
3.3 KiB
Go
/*
|
|
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
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
|
)
|
|
|
|
// dummyLogger will allow logging to be done by the testing pkg.
|
|
type dummyLogger testing.T
|
|
|
|
func (dl *dummyLogger) log(lvl int8, msg string, args ...interface{}) {
|
|
var l string
|
|
switch lvl {
|
|
case logger.Warning:
|
|
l = "warning"
|
|
case logger.Debug:
|
|
l = "debug"
|
|
case logger.Info:
|
|
l = "info"
|
|
case logger.Error:
|
|
l = "error"
|
|
case logger.Fatal:
|
|
l = "fatal"
|
|
}
|
|
msg = l + ": " + msg
|
|
for i := 0; i < len(args); i++ {
|
|
msg += " %v"
|
|
}
|
|
if len(args) == 0 {
|
|
dl.Log(msg + "\n")
|
|
return
|
|
}
|
|
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
|
|
|
|
// 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)
|
|
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. 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)
|
|
}
|
|
}
|
|
}()
|
|
|
|
// 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)
|
|
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()
|
|
}
|
|
|
|
func TestReceiveOp(t *testing.T) {
|
|
|
|
}
|