2024-11-01 19:54:20 +08:00
|
|
|
//nolint:forcetypeassert,nilnil // ok here
|
2014-08-01 14:39:52 +08:00
|
|
|
package mavlink
|
|
|
|
|
|
|
|
import (
|
2014-12-19 07:27:23 +08:00
|
|
|
"io"
|
2017-04-06 16:52:59 +08:00
|
|
|
"strings"
|
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
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
common "gobot.io/x/gobot/v2/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)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotNil(t, d.Connection())
|
|
|
|
assert.Equal(t, 10*time.Millisecond, d.interval)
|
2014-12-18 08:31:49 +08:00
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
d = NewDriver(m, 100*time.Millisecond)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 100*time.Millisecond, d.interval)
|
2014-12-18 08:31:49 +08:00
|
|
|
}
|
2016-08-30 23:10:50 +08:00
|
|
|
|
2017-04-06 16:52:59 +08:00
|
|
|
func TestMavlinkDriverName(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "Mavlink"))
|
2017-04-06 16:52:59 +08:00
|
|
|
d.SetName("NewName")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "NewName", d.Name())
|
2017-04-06 16:52:59 +08:00
|
|
|
}
|
|
|
|
|
2014-08-01 14:39:52 +08:00
|
|
|
func TestMavlinkDriverStart(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2023-05-19 20:16:22 +08:00
|
|
|
err := make(chan error)
|
|
|
|
packet := make(chan *common.MAVLinkPacket)
|
|
|
|
message := make(chan common.MAVLinkMessage)
|
2014-12-18 08:31:49 +08:00
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.On(PacketEvent, func(data interface{}) {
|
2014-12-18 08:31:49 +08:00
|
|
|
packet <- data.(*common.MAVLinkPacket)
|
|
|
|
})
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.On(MessageEvent, func(data interface{}) {
|
2014-12-18 08:31:49 +08:00
|
|
|
message <- data.(common.MAVLinkMessage)
|
|
|
|
})
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.On(ErrorIOEvent, func(data interface{}) {
|
2014-12-19 04:54:07 +08:00
|
|
|
err <- data.(error)
|
|
|
|
})
|
2023-06-13 01:51:25 +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
|
|
|
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
2014-12-18 08:31:49 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case p := <-packet:
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.SendPacket(p))
|
2014-12-18 08:31:49 +08:00
|
|
|
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "packet was not emitted")
|
2014-12-18 08:31:49 +08:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-message:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "message was not emitted")
|
2014-12-18 08:31:49 +08:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-err:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "error was not emitted")
|
2014-12-18 08:31:49 +08:00
|
|
|
}
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkDriverHalt(t *testing.T) {
|
|
|
|
d := initTestMavlinkDriver()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Halt())
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|