Skip to content

Commit

Permalink
core: add Running() methods for Master and Robot and increase test co…
Browse files Browse the repository at this point in the history
…verage accordingly

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed May 7, 2017
1 parent da1b53a commit d8298e6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
14 changes: 13 additions & 1 deletion master.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gobot
import (
"os"
"os/signal"
"sync/atomic"

multierror "github.com/hashicorp/go-multierror"
)
Expand Down Expand Up @@ -36,13 +37,14 @@ type Master struct {
robots *Robots
trap func(chan os.Signal)
AutoRun bool
running atomic.Value
Commander
Eventer
}

// NewMaster returns a new Gobot Master
func NewMaster() *Master {
return &Master{
m := &Master{
robots: &Robots{},
trap: func(c chan os.Signal) {
signal.Notify(c, os.Interrupt)
Expand All @@ -51,6 +53,8 @@ func NewMaster() *Master {
Commander: NewCommander(),
Eventer: NewEventer(),
}
m.running.Store(false)
return m
}

// Start calls the Start method on each robot in its collection of robots. On
Expand All @@ -62,6 +66,8 @@ func (g *Master) Start() (err error) {
return
}

g.running.Store(true)

if g.AutoRun {
c := make(chan os.Signal, 1)
g.trap(c)
Expand All @@ -82,9 +88,15 @@ func (g *Master) Stop() (err error) {
err = multierror.Append(err, rerr)
}

g.running.Store(false)
return
}

// Running returns if the Master is currently started or not
func (g *Master) Running() bool {
return g.running.Load().(bool)
}

// Robots returns all robots associated with this Gobot Master.
func (g *Master) Robots() *Robots {
return g.robots
Expand Down
18 changes: 18 additions & 0 deletions master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"testing"
"time"

multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot/gobottest"
Expand Down Expand Up @@ -73,6 +74,19 @@ func TestMasterStart(t *testing.T) {
g := initTestMaster()
gobottest.Assert(t, g.Start(), nil)
gobottest.Assert(t, g.Stop(), nil)
gobottest.Assert(t, g.Running(), false)
}

func TestMasterStartAutoRun(t *testing.T) {
g := NewMaster()
g.AddRobot(newTestRobot("Robot99"))
go g.Start()
time.Sleep(10 * time.Millisecond)
gobottest.Assert(t, g.Running(), true)

// stop it
gobottest.Assert(t, g.Stop(), nil)
gobottest.Assert(t, g.Running(), false)
}

func TestMasterStartDriverErrors(t *testing.T) {
Expand Down Expand Up @@ -145,4 +159,8 @@ func TestMasterFinalizeErrors(t *testing.T) {

gobottest.Assert(t, g.Start(), nil)
gobottest.Assert(t, g.Stop(), expected)

testAdaptorFinalize = func() (err error) {
return nil
}
}
10 changes: 10 additions & 0 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"os/signal"
"sync/atomic"

multierror "github.com/hashicorp/go-multierror"
)
Expand Down Expand Up @@ -48,6 +49,7 @@ type Robot struct {
devices *Devices
trap func(chan os.Signal)
AutoRun bool
running atomic.Value
done chan bool
Commander
Eventer
Expand Down Expand Up @@ -137,6 +139,7 @@ func NewRobot(v ...interface{}) *Robot {
}
}

r.running.Store(false)
log.Println("Robot", r.Name, "initialized.")

return r
Expand Down Expand Up @@ -166,6 +169,7 @@ func (r *Robot) Start(args ...interface{}) (err error) {
<-r.done
}()

r.running.Store(true)
if r.AutoRun {
c := make(chan os.Signal, 1)
r.trap(c)
Expand Down Expand Up @@ -194,9 +198,15 @@ func (r *Robot) Stop() error {
}

r.done <- true
r.running.Store(false)
return result
}

// Running returns if the Robot is currently started or not
func (r *Robot) Running() bool {
return r.running.Load().(bool)
}

// Devices returns all devices associated with this Robot.
func (r *Robot) Devices() *Devices {
return r.devices
Expand Down
30 changes: 30 additions & 0 deletions robot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobot

import (
"testing"
"time"

"gobot.io/x/gobot/gobottest"
)
Expand Down Expand Up @@ -35,3 +36,32 @@ func TestRobotDevicesToJSON(t *testing.T) {
gobottest.Assert(t, json.Devices[0].Connection, "Connection1")
gobottest.Assert(t, len(json.Devices[0].Commands), 1)
}

func TestRobotStart(t *testing.T) {
r := newTestRobot("Robot99")
gobottest.Assert(t, r.Start(), nil)
gobottest.Assert(t, r.Stop(), nil)
gobottest.Assert(t, r.Running(), false)
}

func TestRobotStartAutoRun(t *testing.T) {
adaptor1 := newTestAdaptor("Connection1", "/dev/null")
driver1 := newTestDriver(adaptor1, "Device1", "0")
work := func() {}
r := NewRobot("autorun",
[]Connection{adaptor1},
[]Device{driver1},
work,
)

go func() {
gobottest.Assert(t, r.Start(), nil)
}()

time.Sleep(10 * time.Millisecond)
gobottest.Assert(t, r.Running(), true)

// stop it
gobottest.Assert(t, r.Stop(), nil)
gobottest.Assert(t, r.Running(), false)
}

0 comments on commit d8298e6

Please sign in to comment.