1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/platforms/mavlink/mavlink_driver_test.go

91 lines
2.4 KiB
Go
Raw Normal View History

2014-07-31 23:39:52 -07:00
package mavlink
import (
2014-12-17 16:31:49 -08:00
"errors"
"io"
2014-07-31 23:39:52 -07:00
"testing"
2014-12-17 16:31:49 -08:00
"time"
2014-07-31 23:39:52 -07:00
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
2014-12-17 16:31:49 -08:00
common "github.com/hybridgroup/gobot/platforms/mavlink/common"
2014-07-31 23:39:52 -07:00
)
var _ gobot.Driver = (*MavlinkDriver)(nil)
2014-07-31 23:39:52 -07:00
func initTestMavlinkDriver() *MavlinkDriver {
2014-08-04 12:06:54 -07:00
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
2014-12-17 16:31:49 -08:00
m.sp = nullReadWriteCloser{}
2014-08-04 12:06:54 -07:00
return NewMavlinkDriver(m, "myDriver")
2014-07-31 23:39:52 -07:00
}
2014-12-17 16:31:49 -08:00
func TestMavlinkDriver(t *testing.T) {
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
m.sp = nullReadWriteCloser{}
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
2014-12-17 16:31:49 -08:00
d := NewMavlinkDriver(m, "myDriver")
gobottest.Assert(t, d.Name(), "myDriver")
gobottest.Assert(t, d.Connection().Name(), "myAdaptor")
gobottest.Assert(t, d.interval, 10*time.Millisecond)
2014-12-17 16:31:49 -08:00
d = NewMavlinkDriver(m, "myDriver", 100*time.Millisecond)
gobottest.Assert(t, d.interval, 100*time.Millisecond)
2014-12-17 16:31:49 -08:00
}
2014-07-31 23:39:52 -07:00
func TestMavlinkDriverStart(t *testing.T) {
d := initTestMavlinkDriver()
2014-12-17 16:31:49 -08:00
err := make(chan error, 0)
packet := make(chan *common.MAVLinkPacket, 0)
message := make(chan common.MAVLinkMessage, 0)
gobot.Once(d.Event("packet"), func(data interface{}) {
packet <- data.(*common.MAVLinkPacket)
})
gobot.Once(d.Event("message"), func(data interface{}) {
message <- data.(common.MAVLinkMessage)
})
gobot.Once(d.Event("errorIO"), func(data interface{}) {
err <- data.(error)
})
gobot.Once(d.Event("errorMAVLink"), func(data interface{}) {
2014-12-17 16:31:49 -08:00
err <- data.(error)
})
gobottest.Assert(t, len(d.Start()), 0)
2014-12-17 16:31:49 -08:00
select {
case p := <-packet:
gobottest.Assert(t, d.SendPacket(p), nil)
2014-12-17 16:31:49 -08:00
case <-time.After(100 * time.Millisecond):
t.Errorf("packet was not emitted")
}
select {
case <-message:
case <-time.After(100 * time.Millisecond):
t.Errorf("message was not emitted")
}
select {
case <-err:
case <-time.After(100 * time.Millisecond):
t.Errorf("error was not emitted")
}
payload = []byte{0xFE, 0x09, 0x4E, 0x01, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03, 0x1C, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
2014-12-17 16:31:49 -08:00
select {
case e := <-err:
gobottest.Assert(t, e, errors.New("Unknown Message ID: 255"))
2014-12-17 16:31:49 -08:00
case <-time.After(100 * time.Millisecond):
t.Errorf("error was not emitted")
}
2014-07-31 23:39:52 -07:00
}
func TestMavlinkDriverHalt(t *testing.T) {
d := initTestMavlinkDriver()
gobottest.Assert(t, len(d.Halt()), 0)
2014-07-31 23:39:52 -07:00
}