2013-12-31 08:51:21 +08:00
|
|
|
package gobot
|
|
|
|
|
|
|
|
import (
|
2014-11-30 16:19:53 +08:00
|
|
|
"errors"
|
2023-06-13 01:51:25 +08:00
|
|
|
"fmt"
|
2014-06-13 05:38:03 +08:00
|
|
|
"log"
|
2015-10-22 15:12:00 +08:00
|
|
|
"os"
|
2014-06-13 05:38:03 +08:00
|
|
|
"testing"
|
2017-05-07 19:28:11 +08:00
|
|
|
"time"
|
2016-02-22 13:21:24 +08:00
|
|
|
|
2016-11-08 03:40:43 +08:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
2013-12-31 08:51:21 +08:00
|
|
|
)
|
|
|
|
|
2016-10-19 00:23:59 +08:00
|
|
|
func initTestMaster() *Master {
|
2014-06-14 07:01:39 +08:00
|
|
|
log.SetOutput(&NullReadWriteCloser{})
|
2016-10-16 02:02:54 +08:00
|
|
|
g := NewMaster()
|
2015-10-22 15:12:00 +08:00
|
|
|
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
|
|
|
|
2016-10-19 00:23:59 +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})
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, i, 3)
|
2014-11-30 16:19:53 +08:00
|
|
|
i, _ = n.Read(make([]byte, 10))
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, i, 10)
|
|
|
|
gobottest.Assert(t, n.Close(), nil)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
2014-05-03 18:22:22 +08:00
|
|
|
|
2017-02-25 19:00:32 +08:00
|
|
|
func TestMasterRobot(t *testing.T) {
|
2016-10-19 00:23:59 +08:00
|
|
|
g := initTestMaster()
|
2016-02-22 13:21:24 +08:00
|
|
|
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))
|
|
|
|
gobottest.Assert(t, g.Robot("Robot4").Connection("Connection1"), (Connection)(nil))
|
|
|
|
gobottest.Assert(t, g.Robot("Robot1").Device("Device4"), (Device)(nil))
|
|
|
|
gobottest.Assert(t, g.Robot("Robot1").Device("Device1").Name(), "Device1")
|
|
|
|
gobottest.Assert(t, g.Robot("Robot1").Devices().Len(), 3)
|
|
|
|
gobottest.Assert(t, g.Robot("Robot1").Connection("Connection4"), (Connection)(nil))
|
|
|
|
gobottest.Assert(t, g.Robot("Robot1").Connections().Len(), 3)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
2014-07-11 02:14:08 +08:00
|
|
|
|
2017-02-25 19:00:32 +08:00
|
|
|
func TestMasterToJSON(t *testing.T) {
|
2016-10-19 00:23:59 +08:00
|
|
|
g := initTestMaster()
|
2014-07-11 02:14:08 +08:00
|
|
|
g.AddCommand("test_function", func(params map[string]interface{}) interface{} {
|
|
|
|
return nil
|
|
|
|
})
|
2016-10-16 02:02:54 +08:00
|
|
|
json := NewJSONMaster(g)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, len(json.Robots), g.Robots().Len())
|
|
|
|
gobottest.Assert(t, len(json.Commands), len(g.Commands()))
|
2014-07-11 02:14:08 +08:00
|
|
|
}
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2016-10-19 00:23:59 +08:00
|
|
|
func TestMasterStart(t *testing.T) {
|
|
|
|
g := initTestMaster()
|
2016-11-07 21:55:21 +08:00
|
|
|
gobottest.Assert(t, g.Start(), nil)
|
|
|
|
gobottest.Assert(t, g.Stop(), nil)
|
2017-05-07 19:28:11 +08:00
|
|
|
gobottest.Assert(t, g.Running(), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMasterStartAutoRun(t *testing.T) {
|
|
|
|
g := NewMaster()
|
|
|
|
g.AddRobot(newTestRobot("Robot99"))
|
2023-06-13 01:51:25 +08:00
|
|
|
go func() { _ = g.Start() }()
|
2017-05-07 19:28:11 +08:00
|
|
|
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)
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|
|
|
|
|
2016-10-19 00:23:59 +08:00
|
|
|
func TestMasterStartDriverErrors(t *testing.T) {
|
|
|
|
g := initTestMaster1Robot()
|
2016-11-08 03:40:43 +08:00
|
|
|
e := errors.New("driver start error 1")
|
2016-11-07 21:55:21 +08:00
|
|
|
testDriverStart = func() (err error) {
|
2016-11-08 03:40:43 +08:00
|
|
|
return e
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
var want error
|
|
|
|
want = multierror.Append(want, e)
|
|
|
|
want = multierror.Append(want, e)
|
|
|
|
want = multierror.Append(want, e)
|
2016-11-08 03:40:43 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
gobottest.Assert(t, g.Start(), want)
|
2016-11-07 21:55:21 +08:00
|
|
|
gobottest.Assert(t, g.Stop(), nil)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2016-11-07 21:55:21 +08:00
|
|
|
testDriverStart = func() (err error) { return }
|
2016-10-19 00:23:59 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 19:00:32 +08:00
|
|
|
func TestMasterHaltFromRobotDriverErrors(t *testing.T) {
|
2017-02-23 04:51:57 +08:00
|
|
|
g := initTestMaster1Robot()
|
2023-06-13 01:51:25 +08:00
|
|
|
var ec int
|
2017-02-23 04:51:57 +08:00
|
|
|
testDriverHalt = func() (err error) {
|
2023-06-13 01:51:25 +08:00
|
|
|
ec++
|
|
|
|
return fmt.Errorf("driver halt error %d", ec)
|
2017-02-23 04:51:57 +08:00
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
defer func() { testDriverHalt = func() error { return nil } }()
|
2017-02-23 04:51:57 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
var want error
|
|
|
|
for i := 1; i <= 3; i++ {
|
|
|
|
e := fmt.Errorf("driver halt error %d", i)
|
|
|
|
want = multierror.Append(want, e)
|
|
|
|
}
|
2017-02-23 04:51:57 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
gobottest.Assert(t, g.Start(), want)
|
2017-02-23 04:51:57 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 19:00:32 +08:00
|
|
|
func TestMasterStartRobotAdaptorErrors(t *testing.T) {
|
2016-10-19 00:23:59 +08:00
|
|
|
g := initTestMaster1Robot()
|
2023-06-13 01:51:25 +08:00
|
|
|
var ec int
|
2016-11-07 21:55:21 +08:00
|
|
|
testAdaptorConnect = func() (err error) {
|
2023-06-13 01:51:25 +08:00
|
|
|
ec++
|
|
|
|
return fmt.Errorf("adaptor start error %d", ec)
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
defer func() { testAdaptorConnect = func() error { return nil } }()
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
var want error
|
|
|
|
for i := 1; i <= 3; i++ {
|
|
|
|
e := fmt.Errorf("adaptor start error %d", i)
|
|
|
|
want = multierror.Append(want, e)
|
|
|
|
}
|
2016-11-08 03:40:43 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
gobottest.Assert(t, g.Start(), want)
|
2016-11-07 21:55:21 +08:00
|
|
|
gobottest.Assert(t, g.Stop(), nil)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2016-11-07 21:55:21 +08:00
|
|
|
testAdaptorConnect = func() (err error) { return }
|
2016-10-19 00:23:59 +08:00
|
|
|
}
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2016-11-08 03:40:43 +08:00
|
|
|
func TestMasterFinalizeErrors(t *testing.T) {
|
2016-10-19 00:23:59 +08:00
|
|
|
g := initTestMaster1Robot()
|
2023-06-13 01:51:25 +08:00
|
|
|
var ec int
|
2016-11-07 21:55:21 +08:00
|
|
|
testAdaptorFinalize = func() (err error) {
|
2023-06-13 01:51:25 +08:00
|
|
|
ec++
|
|
|
|
return fmt.Errorf("adaptor finalize error %d", ec)
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
defer func() { testAdaptorFinalize = func() error { return nil } }()
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
var want error
|
|
|
|
for i := 1; i <= 3; i++ {
|
|
|
|
e := fmt.Errorf("adaptor finalize error %d", i)
|
|
|
|
want = multierror.Append(want, e)
|
2017-05-07 19:28:11 +08:00
|
|
|
}
|
2023-06-13 01:51:25 +08:00
|
|
|
|
|
|
|
gobottest.Assert(t, g.Start(), want)
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|