robocar-simulator/camera/camera_test.go

172 lines
3.5 KiB
Go
Raw Normal View History

2020-09-20 21:16:06 +00:00
package camera
import (
"bufio"
"fmt"
"github.com/cyrilix/robocar-protobuf/go/events"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net"
"strings"
"sync"
"testing"
)
type MockPublisher struct {
2020-11-29 17:42:33 +00:00
notifyChan chan []byte
initNotifyChan sync.Once
2020-09-20 21:16:06 +00:00
}
2020-11-29 17:42:33 +00:00
func (p *MockPublisher) Close() error {
if p.notifyChan != nil {
close(p.notifyChan)
}
return nil
2020-09-20 21:16:06 +00:00
}
2020-11-29 17:42:33 +00:00
func (p *MockPublisher) Publish(payload []byte) {
p.notifyChan <- payload
}
func (p *MockPublisher) Notify() <-chan []byte {
p.initNotifyChan.Do(func() { p.notifyChan = make(chan []byte) })
return p.notifyChan
2020-09-20 21:16:06 +00:00
}
func TestPart_ListenEvents(t *testing.T) {
2020-11-29 17:42:33 +00:00
simulatorMock := SimulatorMock{}
err := simulatorMock.Start()
2020-09-20 21:16:06 +00:00
if err != nil {
t.Errorf("unable to start mock server: %v", err)
}
defer func() {
2020-11-29 17:42:33 +00:00
if err := simulatorMock.Close(); err != nil {
2020-09-20 21:16:06 +00:00
t.Errorf("unable to close mock server: %v", err)
}
}()
2020-11-29 17:42:33 +00:00
publisher := MockPublisher{}
2020-09-20 21:16:06 +00:00
2020-11-29 17:42:33 +00:00
part := New(&publisher, simulatorMock.Addr())
2020-09-20 21:16:06 +00:00
go func() {
err := part.Start()
if err != nil {
t.Fatalf("unable to start camera simulator: %v", err)
}
}()
defer func() {
if err := part.Close(); err != nil {
t.Errorf("unable to close camera simulator: %v", err)
}
}()
2020-11-29 17:42:33 +00:00
simulatorMock.WaitConnection()
log.Trace("read test data")
2020-09-20 21:16:06 +00:00
testContent, err := ioutil.ReadFile("testdata/msg.json")
lines := strings.Split(string(testContent), "\n")
for idx, line := range lines {
2020-11-29 17:42:33 +00:00
err = simulatorMock.EmitMsg(line)
2020-09-20 21:16:06 +00:00
if err != nil {
t.Errorf("[line %v/%v] unable to write line: %v", idx+1, len(lines), err)
}
2020-11-29 17:42:33 +00:00
byteMsg := <-publisher.Notify()
2020-09-20 21:16:06 +00:00
var msg events.FrameMessage
2020-11-29 17:42:33 +00:00
err = proto.Unmarshal(byteMsg, &msg)
2020-09-20 21:16:06 +00:00
if err != nil {
t.Errorf("unable to unmarshal frame msg: %v", err)
continue
}
if msg.GetId() == nil {
t.Error("frame msg has not Id")
}
if len(msg.Frame) < 10 {
t.Errorf("[%v] invalid frame image: %v", msg.Id, msg.GetFrame())
}
}
}
2020-11-29 17:42:33 +00:00
type SimulatorMock struct {
ln net.Listener
muConn sync.Mutex
conn net.Conn
writer *bufio.Writer
newConnection chan net.Conn
logger *log.Entry
2020-09-20 21:16:06 +00:00
}
2020-11-29 17:42:33 +00:00
func (c *SimulatorMock) EmitMsg(p string) (err error) {
c.muConn.Lock()
defer c.muConn.Unlock()
2020-09-20 21:16:06 +00:00
_, err = c.writer.WriteString(p + "\n")
if err != nil {
2020-11-29 17:42:33 +00:00
c.logger.Errorf("unable to write response: %v", err)
2020-09-20 21:16:06 +00:00
}
if err == io.EOF {
2020-11-29 17:42:33 +00:00
c.logger.Info("Connection closed")
2020-09-20 21:16:06 +00:00
return err
}
err = c.writer.Flush()
return err
}
2020-11-29 17:42:33 +00:00
func (c *SimulatorMock) WaitConnection() {
c.muConn.Lock()
defer c.muConn.Unlock()
c.logger.Debug("simulator waiting connection")
if c.conn != nil {
return
}
c.logger.Debug("new connection")
conn := <-c.newConnection
c.conn = conn
c.writer = bufio.NewWriter(conn)
}
func (c *SimulatorMock) Start() error {
c.logger = log.WithField("simulator", "mock")
c.newConnection = make(chan net.Conn)
2020-09-20 21:16:06 +00:00
ln, err := net.Listen("tcp", "127.0.0.1:")
c.ln = ln
if err != nil {
return fmt.Errorf("unable to listen on port: %v", err)
}
go func() {
for {
2020-11-29 17:42:33 +00:00
conn, err := c.ln.Accept()
2020-09-20 21:16:06 +00:00
if err != nil && err == io.EOF {
2020-11-29 17:42:33 +00:00
c.logger.Errorf("connection close: %v", err)
2020-09-20 21:16:06 +00:00
break
}
2020-11-29 17:42:33 +00:00
if c.newConnection == nil {
break
}
c.newConnection <- conn
2020-09-20 21:16:06 +00:00
}
}()
return nil
}
2020-11-29 17:42:33 +00:00
func (c *SimulatorMock) Addr() string {
2020-09-20 21:16:06 +00:00
return c.ln.Addr().String()
}
2020-11-29 17:42:33 +00:00
func (c *SimulatorMock) Close() error {
c.logger.Debug("close mock server")
if c == nil {
return nil
}
close(c.newConnection)
c.newConnection = nil
2020-09-20 21:16:06 +00:00
err := c.ln.Close()
if err != nil {
return fmt.Errorf("unable to close mock server: %v", err)
}
return nil
}