-
Notifications
You must be signed in to change notification settings - Fork 36
/
endpoint_custom_test.go
139 lines (118 loc) · 2.81 KB
/
endpoint_custom_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gomavlib
import (
"errors"
"io"
"testing"
"github.com/stretchr/testify/require"
"github.com/bluenviron/gomavlib/v3/pkg/dialect"
"github.com/bluenviron/gomavlib/v3/pkg/frame"
)
var _ endpointChannelSingle = (*endpointCustom)(nil)
type dummyReadWriter struct {
chOut chan []byte
chIn chan []byte
chReadErr chan struct{}
}
func newDummyReadWriterPair() (*dummyReadWriter, *dummyReadWriter) {
one := &dummyReadWriter{
chOut: make(chan []byte),
chIn: make(chan []byte),
chReadErr: make(chan struct{}),
}
two := &dummyReadWriter{
chOut: one.chIn,
chIn: one.chOut,
chReadErr: make(chan struct{}),
}
return one, two
}
func (e *dummyReadWriter) simulateReadError() {
close(e.chReadErr)
}
func (e *dummyReadWriter) Close() error {
close(e.chOut)
close(e.chIn)
return nil
}
func (e *dummyReadWriter) Read(p []byte) (int, error) {
select {
case buf, ok := <-e.chOut:
if !ok {
return 0, io.EOF
}
return copy(p, buf), nil
case <-e.chReadErr:
return 0, errors.New("custom error")
}
}
func (e *dummyReadWriter) Write(p []byte) (int, error) {
e.chIn <- p
return len(p), nil
}
func TestEndpointCustom(t *testing.T) {
remote, local := newDummyReadWriterPair()
node, err := NewNode(NodeConf{
Dialect: testDialect,
OutVersion: V2,
OutSystemID: 10,
Endpoints: []EndpointConf{EndpointCustom{remote}},
HeartbeatDisable: true,
})
require.NoError(t, err)
defer node.Close()
evt := <-node.Events()
require.Equal(t, &EventChannelOpen{
Channel: evt.(*EventChannelOpen).Channel,
}, evt)
dialectRW, err := dialect.NewReadWriter(testDialect)
require.NoError(t, err)
rw, err := frame.NewReadWriter(frame.ReadWriterConf{
ReadWriter: local,
DialectRW: dialectRW,
OutVersion: frame.V2,
OutSystemID: 11,
})
require.NoError(t, err)
for i := 0; i < 3; i++ { //nolint:dupl
msg := &MessageHeartbeat{
Type: 1,
Autopilot: 2,
BaseMode: 3,
CustomMode: 6,
SystemStatus: 4,
MavlinkVersion: 5,
}
err = rw.WriteMessage(msg)
require.NoError(t, err)
evt = <-node.Events()
require.Equal(t, &EventFrame{
Frame: &frame.V2Frame{
SequenceNumber: byte(i),
SystemID: 11,
ComponentID: 1,
Message: msg,
Checksum: evt.(*EventFrame).Frame.GetChecksum(),
},
Channel: evt.(*EventFrame).Channel,
}, evt)
msg = &MessageHeartbeat{
Type: 6,
Autopilot: 5,
BaseMode: 4,
CustomMode: 3,
SystemStatus: 2,
MavlinkVersion: 1,
}
err := node.WriteMessageAll(msg)
require.NoError(t, err)
fr, err := rw.Read()
require.NoError(t, err)
require.Equal(t, &frame.V2Frame{
SequenceNumber: byte(i),
SystemID: 10,
ComponentID: 1,
Message: msg,
Checksum: fr.GetChecksum(),
}, fr)
}
}