2014-08-01 14:39:52 +08:00
|
|
|
package mavlink
|
|
|
|
|
|
|
|
import (
|
2014-12-19 07:27:23 +08:00
|
|
|
"io"
|
2014-08-01 14:39:52 +08:00
|
|
|
"testing"
|
2014-12-18 08:31:49 +08:00
|
|
|
"time"
|
2014-08-01 14:39:52 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
common "gobot.io/x/gobot/platforms/mavlink/common"
|
2014-08-01 14:39:52 +08:00
|
|
|
)
|
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
var _ gobot.Driver = (*Driver)(nil)
|
2016-08-27 17:56:01 +08:00
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
func initTestMavlinkDriver() *Driver {
|
|
|
|
m := NewAdaptor("/dev/null")
|
2014-12-19 07:27:23 +08:00
|
|
|
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
|
2014-12-18 08:31:49 +08:00
|
|
|
m.sp = nullReadWriteCloser{}
|
2016-09-26 03:44:40 +08:00
|
|
|
return NewDriver(m)
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 08:31:49 +08:00
|
|
|
func TestMavlinkDriver(t *testing.T) {
|
2016-09-26 03:44:40 +08:00
|
|
|
m := NewAdaptor("/dev/null")
|
2014-12-18 08:31:49 +08:00
|
|
|
m.sp = nullReadWriteCloser{}
|
2014-12-19 07:27:23 +08:00
|
|
|
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
|
2014-12-18 08:31:49 +08:00
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
d := NewDriver(m)
|
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.interval, 10*time.Millisecond)
|
2014-12-18 08:31:49 +08:00
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
d = NewDriver(m, 100*time.Millisecond)
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.interval, 100*time.Millisecond)
|
2014-12-18 08:31:49 +08:00
|
|
|
}
|
2016-08-30 23:10:50 +08:00
|
|
|
|
2014-08-01 14:39:52 +08:00
|
|
|
func TestMavlinkDriverStart(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2014-12-18 08:31:49 +08:00
|
|
|
err := make(chan error, 0)
|
|
|
|
packet := make(chan *common.MAVLinkPacket, 0)
|
|
|
|
message := make(chan common.MAVLinkMessage, 0)
|
|
|
|
|
2016-09-13 00:01:31 +08:00
|
|
|
d.On(PacketEvent, func(data interface{}) {
|
2014-12-18 08:31:49 +08:00
|
|
|
packet <- data.(*common.MAVLinkPacket)
|
|
|
|
})
|
2016-09-13 00:01:31 +08:00
|
|
|
d.On(MessageEvent, func(data interface{}) {
|
2014-12-18 08:31:49 +08:00
|
|
|
message <- data.(common.MAVLinkMessage)
|
|
|
|
})
|
2016-09-13 00:01:31 +08:00
|
|
|
d.On(ErrorIOEvent, func(data interface{}) {
|
2014-12-19 04:54:07 +08:00
|
|
|
err <- data.(error)
|
|
|
|
})
|
2016-09-13 00:01:31 +08:00
|
|
|
d.On(ErrorMAVLinkEvent, func(data interface{}) {
|
2014-12-18 08:31:49 +08:00
|
|
|
err <- data.(error)
|
|
|
|
})
|
2014-12-19 04:54:07 +08:00
|
|
|
|
2016-11-08 02:44:57 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2014-12-18 08:31:49 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case p := <-packet:
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, d.SendPacket(p), nil)
|
2014-12-18 08: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")
|
|
|
|
}
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkDriverHalt(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2016-11-08 02:44:57 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|