hybridgroup.gobot/master_test.go

164 lines
3.9 KiB
Go
Raw Normal View History

2013-12-31 08:51:21 +08:00
package gobot
import (
2014-11-30 16:19:53 +08:00
"errors"
"fmt"
2014-06-13 05:38:03 +08:00
"log"
"os"
2014-06-13 05:38:03 +08:00
"testing"
"time"
multierror "github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2013-12-31 08:51:21 +08:00
)
func initTestMaster() *Master {
2014-06-14 07:01:39 +08:00
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
2014-11-30 16:19:53 +08:00
g.AddRobot(newTestRobot("Robot1"))
g.AddRobot(newTestRobot("Robot2"))
g.AddRobot(newTestRobot(""))
2014-06-14 07:01:39 +08:00
return g
2014-06-13 05:38:03 +08:00
}
2013-12-31 08:51:21 +08:00
func initTestMaster1Robot() *Master {
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.AddRobot(newTestRobot("Robot99"))
return g
}
2014-11-30 16:19:53 +08:00
func TestNullReadWriteCloser(t *testing.T) {
n := &NullReadWriteCloser{}
i, _ := n.Write([]byte{1, 2, 3})
assert.Equal(t, 3, i)
2014-11-30 16:19:53 +08:00
i, _ = n.Read(make([]byte, 10))
assert.Equal(t, 10, i)
require.NoError(t, n.Close())
2014-06-13 05:38:03 +08:00
}
2014-05-03 18:22:22 +08:00
func TestMasterRobot(t *testing.T) {
g := initTestMaster()
assert.Equal(t, "Robot1", g.Robot("Robot1").Name)
assert.Equal(t, (*Robot)(nil), g.Robot("Robot4"))
assert.Equal(t, (Device)(nil), g.Robot("Robot4").Device("Device1"))
assert.Equal(t, (Connection)(nil), g.Robot("Robot4").Connection("Connection1"))
assert.Equal(t, (Device)(nil), g.Robot("Robot1").Device("Device4"))
assert.Equal(t, "Device1", g.Robot("Robot1").Device("Device1").Name())
assert.Equal(t, 3, g.Robot("Robot1").Devices().Len())
assert.Equal(t, (Connection)(nil), g.Robot("Robot1").Connection("Connection4"))
assert.Equal(t, 3, g.Robot("Robot1").Connections().Len())
2014-06-13 05:38:03 +08:00
}
2014-07-11 02:14:08 +08:00
func TestMasterToJSON(t *testing.T) {
g := initTestMaster()
2014-07-11 02:14:08 +08:00
g.AddCommand("test_function", func(params map[string]interface{}) interface{} {
return nil
})
json := NewJSONMaster(g)
assert.Len(t, json.Robots, g.Robots().Len())
assert.Len(t, json.Commands, len(g.Commands()))
2014-07-11 02:14:08 +08:00
}
2014-11-30 16:19:53 +08:00
func TestMasterStart(t *testing.T) {
g := initTestMaster()
require.NoError(t, g.Start())
require.NoError(t, g.Stop())
assert.False(t, g.Running())
}
func TestMasterStartAutoRun(t *testing.T) {
g := NewMaster()
g.AddRobot(newTestRobot("Robot99"))
go func() { _ = g.Start() }()
time.Sleep(10 * time.Millisecond)
assert.True(t, g.Running())
// stop it
require.NoError(t, g.Stop())
assert.False(t, g.Running())
2014-11-30 16:19:53 +08:00
}
func TestMasterStartDriverErrors(t *testing.T) {
g := initTestMaster1Robot()
e := errors.New("driver start error 1")
testDriverStart = func() error {
return e
2014-11-30 16:19:53 +08:00
}
var want error
want = multierror.Append(want, e)
want = multierror.Append(want, e)
want = multierror.Append(want, e)
assert.Equal(t, want, g.Start())
require.NoError(t, g.Stop())
2014-11-30 16:19:53 +08:00
testDriverStart = func() error { return nil }
}
func TestMasterHaltFromRobotDriverErrors(t *testing.T) {
g := initTestMaster1Robot()
var ec int
testDriverHalt = func() error {
ec++
return fmt.Errorf("driver halt error %d", ec)
}
defer func() { testDriverHalt = func() error { return nil } }()
var want error
for i := 1; i <= 3; i++ {
e := fmt.Errorf("driver halt error %d", i)
want = multierror.Append(want, e)
}
assert.Equal(t, want, g.Start())
}
func TestMasterStartRobotAdaptorErrors(t *testing.T) {
g := initTestMaster1Robot()
var ec int
testAdaptorConnect = func() error {
ec++
return fmt.Errorf("adaptor start error %d", ec)
2014-11-30 16:19:53 +08:00
}
defer func() { testAdaptorConnect = func() error { return nil } }()
2014-11-30 16:19:53 +08:00
var want error
for i := 1; i <= 3; i++ {
e := fmt.Errorf("adaptor start error %d", i)
want = multierror.Append(want, e)
}
assert.Equal(t, want, g.Start())
require.NoError(t, g.Stop())
2014-11-30 16:19:53 +08:00
testAdaptorConnect = func() error { return nil }
}
2014-11-30 16:19:53 +08:00
func TestMasterFinalizeErrors(t *testing.T) {
g := initTestMaster1Robot()
var ec int
testAdaptorFinalize = func() error {
ec++
return fmt.Errorf("adaptor finalize error %d", ec)
2014-11-30 16:19:53 +08:00
}
defer func() { testAdaptorFinalize = func() error { return nil } }()
2014-11-30 16:19:53 +08:00
var want error
for i := 1; i <= 3; i++ {
e := fmt.Errorf("adaptor finalize error %d", i)
want = multierror.Append(want, e)
}
assert.Equal(t, want, g.Start())
2014-11-30 16:19:53 +08:00
}