Fix null reference on failure to connect

Refactor sphero adaptor
This commit is contained in:
Adrian Zankich 2014-12-23 07:00:23 -08:00
parent c16993ab05
commit cde191943d
4 changed files with 28 additions and 23 deletions

View File

@ -15,7 +15,7 @@ type SpheroAdaptor struct {
port string
sp io.ReadWriteCloser
connected bool
connect func(*SpheroAdaptor) (err error)
connect func(string) (io.ReadWriteCloser, error)
}
// NewSpheroAdaptor returns a new SpheroAdaptor given a name and port
@ -23,14 +23,8 @@ func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
return &SpheroAdaptor{
name: name,
port: port,
connect: func(a *SpheroAdaptor) (err error) {
c := &serial.Config{Name: a.Port(), Baud: 115200}
s, err := serial.OpenPort(c)
if err != nil {
return err
}
a.sp = s
return
connect: func(port string) (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: port, Baud: 115200})
},
}
}
@ -39,10 +33,12 @@ func (a *SpheroAdaptor) Port() string { return a.port }
// Connect initiates a connection to the Sphero. Returns true on successful connection.
func (a *SpheroAdaptor) Connect() (errs []error) {
if err := a.connect(a); err != nil {
if sp, err := a.connect(a.Port()); err != nil {
return []error{err}
} else {
a.sp = sp
a.connected = true
}
a.connected = true
return
}
@ -50,7 +46,7 @@ func (a *SpheroAdaptor) Connect() (errs []error) {
// it will first close that connection and then establish a new connection.
// Returns true on Successful reconnection
func (a *SpheroAdaptor) Reconnect() (errs []error) {
if a.connected == true {
if a.connected {
a.Disconnect()
}
return a.Connect()
@ -58,10 +54,12 @@ func (a *SpheroAdaptor) Reconnect() (errs []error) {
// Disconnect terminates the connection to the Sphero. Returns true on successful disconnect.
func (a *SpheroAdaptor) Disconnect() (errs []error) {
if err := a.sp.Close(); err != nil {
return []error{err}
if a.connected {
if err := a.sp.Close(); err != nil {
return []error{err}
}
a.connected = false
}
a.connected = false
return
}

View File

@ -2,6 +2,7 @@ package sphero
import (
"errors"
"io"
"testing"
"github.com/hybridgroup/gobot"
@ -35,8 +36,9 @@ func (nullReadWriteCloser) Close() error {
func initTestSpheroAdaptor() *SpheroAdaptor {
a := NewSpheroAdaptor("bot", "/dev/null")
a.sp = nullReadWriteCloser{}
a.connect = func(a *SpheroAdaptor) (err error) { return nil }
a.connect = func(string) (io.ReadWriteCloser, error) {
return &nullReadWriteCloser{}, nil
}
return a
}
@ -60,12 +62,14 @@ func TestSpheroAdaptorReconnect(t *testing.T) {
func TestSpheroAdaptorFinalize(t *testing.T) {
a := initTestSpheroAdaptor()
a.Connect()
gobot.Assert(t, len(a.Finalize()), 0)
testAdaptorClose = func() error {
return errors.New("close error")
}
a.connected = true
gobot.Assert(t, a.Finalize()[0], errors.New("close error"))
}
@ -73,8 +77,8 @@ func TestSpheroAdaptorConnect(t *testing.T) {
a := initTestSpheroAdaptor()
gobot.Assert(t, len(a.Connect()), 0)
a.connect = func(a *SpheroAdaptor) (err error) {
return errors.New("connect error")
a.connect = func(string) (io.ReadWriteCloser, error) {
return nil, errors.New("connect error")
}
gobot.Assert(t, a.Connect()[0], errors.New("connect error"))

View File

@ -180,10 +180,12 @@ func (s *SpheroDriver) Start() (errs []error) {
// Halt halts the SpheroDriver and sends a SpheroDriver.Stop command to the Sphero.
// Returns true on successful halt.
func (s *SpheroDriver) Halt() (errs []error) {
gobot.Every(10*time.Millisecond, func() {
s.Stop()
})
time.Sleep(1 * time.Second)
if s.adaptor().connected {
gobot.Every(10*time.Millisecond, func() {
s.Stop()
})
time.Sleep(1 * time.Second)
}
return
}

View File

@ -58,6 +58,7 @@ func TestSpheroDriverStart(t *testing.T) {
func TestSpheroDriverHalt(t *testing.T) {
d := initTestSpheroDriver()
d.adaptor().connected = true
gobot.Assert(t, len(d.Halt()), 0)
}