Skip to content

Commit

Permalink
core: Now both Robot and Master operate using AutoRun as expected
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 18, 2016
1 parent 21a77a1 commit 68662f8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
5 changes: 5 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gobot

import "os"

type NullReadWriteCloser struct{}

func (NullReadWriteCloser) Write(p []byte) (int, error) {
Expand Down Expand Up @@ -79,6 +81,9 @@ func newTestRobot(name string) *Robot {
work,
)
r.AddCommand("RobotCommand", func(params map[string]interface{}) interface{} { return nil })
r.trap = func(c chan os.Signal) {
c <- os.Interrupt
}

return r
}
1 change: 1 addition & 0 deletions master.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (g *Master) Start() (errs []error) {
log.Println("Error:", err)
errs = append(errs, err)
}
return
}

if g.AutoRun {
Expand Down
49 changes: 27 additions & 22 deletions master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestConnectionEach(t *testing.T) {
gobottest.Assert(t, r.Connections().Len(), i)
}

func initTestGobot() *Master {
func initTestMaster() *Master {
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()
g.trap = func(c chan os.Signal) {
Expand All @@ -31,6 +31,17 @@ func initTestGobot() *Master {
return g
}

func initTestMaster1Robot() *Master {
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.AddRobot(newTestRobot("Robot99"))

return g
}

func TestVersion(t *testing.T) {
gobottest.Assert(t, version, Version())
}
Expand All @@ -45,7 +56,7 @@ func TestNullReadWriteCloser(t *testing.T) {
}

func TestGobotRobot(t *testing.T) {
g := initTestGobot()
g := initTestMaster()
gobottest.Assert(t, g.Robot("Robot1").Name, "Robot1")
gobottest.Assert(t, g.Robot("Robot4"), (*Robot)(nil))
gobottest.Assert(t, g.Robot("Robot4").Device("Device1"), (Device)(nil))
Expand All @@ -58,7 +69,7 @@ func TestGobotRobot(t *testing.T) {
}

func TestGobotToJSON(t *testing.T) {
g := initTestGobot()
g := initTestMaster()
g.AddCommand("test_function", func(params map[string]interface{}) interface{} {
return nil
})
Expand All @@ -67,24 +78,14 @@ func TestGobotToJSON(t *testing.T) {
gobottest.Assert(t, len(json.Commands), len(g.Commands()))
}

func TestGobotStart(t *testing.T) {
g := initTestGobot()
func TestMasterStart(t *testing.T) {
g := initTestMaster()
gobottest.Assert(t, len(g.Start()), 0)
gobottest.Assert(t, len(g.Stop()), 0)
}

func TestGobotStartErrors(t *testing.T) {
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()

adaptor1 := newTestAdaptor("Connection1", "/dev/null")
driver1 := newTestDriver(adaptor1, "Device1", "0")
r := NewRobot("Robot1",
[]Connection{adaptor1},
[]Device{driver1},
)

g.AddRobot(r)
func TestMasterStartDriverErrors(t *testing.T) {
g := initTestMaster1Robot()

testDriverStart = func() (errs []error) {
return []error{
Expand All @@ -96,6 +97,11 @@ func TestGobotStartErrors(t *testing.T) {
gobottest.Assert(t, len(g.Stop()), 0)

testDriverStart = func() (errs []error) { return }
}

func TestMasterStartAdaptorErrors(t *testing.T) {
g := initTestMaster1Robot()

testAdaptorConnect = func() (errs []error) {
return []error{
errors.New("adaptor start error 1"),
Expand All @@ -105,12 +111,11 @@ func TestGobotStartErrors(t *testing.T) {
gobottest.Assert(t, len(g.Start()), 1)
gobottest.Assert(t, len(g.Stop()), 0)

testDriverStart = func() (errs []error) { return }
testAdaptorConnect = func() (errs []error) { return }
}

g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
func TestMasterHaltErrors(t *testing.T) {
g := initTestMaster1Robot()

testDriverHalt = func() (errs []error) {
return []error{
Expand All @@ -125,5 +130,5 @@ func TestGobotStartErrors(t *testing.T) {
}

gobottest.Assert(t, len(g.Start()), 0)
gobottest.Assert(t, len(g.Stop()), 2)
gobottest.Assert(t, len(g.Stop()), 6)
}
18 changes: 10 additions & 8 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func NewRobot(v ...interface{}) *Robot {
Name: fmt.Sprintf("%X", Rand(int(^uint(0)>>1))),
connections: &Connections{},
devices: &Devices{},
done: make(chan bool),
done: make(chan bool, 1),
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
},
Expand Down Expand Up @@ -157,14 +157,16 @@ func (r *Robot) Start(args ...interface{}) (errs []error) {
errs = append(errs, derrs...)
return
}
if r.Work != nil {
log.Println("Starting work...")
go func() {
r.Work()
<-r.done
}()
if r.Work == nil {
r.Work = func() {}
}

log.Println("Starting work...")
go func() {
r.Work()
<-r.done
}()

if r.AutoRun {
c := make(chan os.Signal, 1)
r.trap(c)
Expand All @@ -177,7 +179,7 @@ func (r *Robot) Start(args ...interface{}) (errs []error) {
// waiting for interrupt coming on the channel
<-c

// Stop calls the Stop method on itself, it we are "auto-running".
// Stop calls the Stop method on itself, if we are "auto-running".
r.Stop()
}

Expand Down

0 comments on commit 68662f8

Please sign in to comment.