Skip to content

Commit

Permalink
sphero: eliminate race conditions
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Apr 2, 2017
1 parent dfa02a2 commit 5987a73
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
56 changes: 32 additions & 24 deletions platforms/sphero/sphero_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,56 @@ import (

var _ gobot.Adaptor = (*Adaptor)(nil)

type nullReadWriteCloser struct{}

var testAdaptorRead = func(p []byte) (int, error) {
return len(p), nil
}

func (nullReadWriteCloser) Write(p []byte) (int, error) {
return testAdaptorRead(p)
type nullReadWriteCloser struct {
testAdaptorRead func(p []byte) (int, error)
testAdaptorWrite func(b []byte) (int, error)
testAdaptorClose func() error
}

var testAdaptorWrite = func(b []byte) (int, error) {
return len(b), nil
func (n *nullReadWriteCloser) Write(p []byte) (int, error) {
return n.testAdaptorWrite(p)
}

func (nullReadWriteCloser) Read(b []byte) (int, error) {
return testAdaptorWrite(b)
func (n *nullReadWriteCloser) Read(b []byte) (int, error) {
return n.testAdaptorRead(b)
}

var testAdaptorClose = func() error {
return nil
func (n *nullReadWriteCloser) Close() error {
return n.testAdaptorClose()
}

func (nullReadWriteCloser) Close() error {
return testAdaptorClose()
func NewNullReadWriteCloser() *nullReadWriteCloser {
return &nullReadWriteCloser{
testAdaptorRead: func(p []byte) (int, error) {
return len(p), nil
},
testAdaptorWrite: func(b []byte) (int, error) {
return len(b), nil
},
testAdaptorClose: func() error {
return nil
},
}
}

func initTestSpheroAdaptor() *Adaptor {
func initTestSpheroAdaptor() (*Adaptor, *nullReadWriteCloser) {
a := NewAdaptor("/dev/null")
rwc := NewNullReadWriteCloser()

a.connect = func(string) (io.ReadWriteCloser, error) {
return &nullReadWriteCloser{}, nil
return rwc, nil
}
return a
return a, rwc
}

func TestSpheroAdaptor(t *testing.T) {
a := initTestSpheroAdaptor()
a, _ := initTestSpheroAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Sphero"), true)
gobottest.Assert(t, a.Port(), "/dev/null")
}

func TestSpheroAdaptorReconnect(t *testing.T) {
a := initTestSpheroAdaptor()
a, _ := initTestSpheroAdaptor()
a.Connect()
gobottest.Assert(t, a.connected, true)
a.Reconnect()
Expand All @@ -65,11 +73,11 @@ func TestSpheroAdaptorReconnect(t *testing.T) {
}

func TestSpheroAdaptorFinalize(t *testing.T) {
a := initTestSpheroAdaptor()
a, rwc := initTestSpheroAdaptor()
a.Connect()
gobottest.Assert(t, a.Finalize(), nil)

testAdaptorClose = func() error {
rwc.testAdaptorClose = func() error {
return errors.New("close error")
}

Expand All @@ -78,7 +86,7 @@ func TestSpheroAdaptorFinalize(t *testing.T) {
}

func TestSpheroAdaptorConnect(t *testing.T) {
a := initTestSpheroAdaptor()
a, _ := initTestSpheroAdaptor()
gobottest.Assert(t, a.Connect(), nil)

a.connect = func(string) (io.ReadWriteCloser, error) {
Expand Down
6 changes: 6 additions & 0 deletions platforms/sphero/sphero_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"errors"
"sync"
"time"

"gobot.io/x/gobot"
Expand All @@ -30,6 +31,7 @@ type packet struct {
type SpheroDriver struct {
name string
connection gobot.Connection
mtx sync.Mutex
seq uint8
asyncResponse [][]uint8
syncResponse [][]uint8
Expand Down Expand Up @@ -357,6 +359,8 @@ func (s *SpheroDriver) getSyncResponse(packet *packet) []byte {
}

func (s *SpheroDriver) craftPacket(body []uint8, did byte, cid byte) *packet {
s.mtx.Lock()
defer s.mtx.Unlock()
packet := new(packet)
packet.body = body
dlen := len(packet.body) + 1
Expand All @@ -366,6 +370,8 @@ func (s *SpheroDriver) craftPacket(body []uint8, did byte, cid byte) *packet {
}

func (s *SpheroDriver) write(packet *packet) (err error) {
s.mtx.Lock()
defer s.mtx.Unlock()
buf := append(packet.header, packet.body...)
buf = append(buf, packet.checksum)
length, err := s.adaptor().sp.Write(buf)
Expand Down
4 changes: 2 additions & 2 deletions platforms/sphero/sphero_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
var _ gobot.Driver = (*SpheroDriver)(nil)

func initTestSpheroDriver() *SpheroDriver {
a := NewAdaptor("/dev/null")
a.sp = nullReadWriteCloser{}
a, _ := initTestSpheroAdaptor()
a.Connect()
return NewSpheroDriver(a)
}

Expand Down

0 comments on commit 5987a73

Please sign in to comment.