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
mod test_shared;
#[cfg(all(
feature = "tokio",
feature = "transport-udp",
feature = "dialect-common"
))]
mod test_udp_connections {
use mavlink::MessageData;
/// Test whether we can send a message via UDP and receive it OK using async_connect
#[tokio::test]
async fn test_udp_loopback() {
const RECEIVE_CHECK_COUNT: i32 = 3;
let server = mavlink::connect_async("udpin:0.0.0.0:14552")
.await
.expect("Couldn't create server");
// have the client send one heartbeat per second
tokio::spawn({
async move {
let msg = mavlink::dialects::common::MavMessage::HEARTBEAT(
crate::test_shared::get_heartbeat_msg(),
);
let client = mavlink::connect_async("udpout:127.0.0.1:14552")
.await
.expect("Couldn't create client");
loop {
client.send_default(&msg).await.ok();
}
}
});
//TODO use std::sync::WaitTimeoutResult to timeout ourselves if recv fails?
let mut recv_count = 0;
for _i in 0..RECEIVE_CHECK_COUNT {
match server.recv().await {
Ok((_header, msg)) => {
if let mavlink::dialects::common::MavMessage::HEARTBEAT(_heartbeat_msg) = msg {
recv_count += 1;
} else {
// one message parse failure fails the test
break;
}
}
Err(..) => {
// one message read failure fails the test
break;
}
}
}
assert_eq!(recv_count, RECEIVE_CHECK_COUNT);
}
/// Test whether we can send a message via UDP and receive it OK using async_connect recv_raw
#[tokio::test]
async fn test_udp_loopback_recv_raw() {
const RECEIVE_CHECK_COUNT: i32 = 3;
let server =
mavlink::connect_async::<mavlink::dialects::common::MavMessage>("udpin:0.0.0.0:14562")
.await
.expect("Couldn't create server");
// have the client send one heartbeat per second
tokio::spawn({
async move {
let msg = mavlink::dialects::common::MavMessage::HEARTBEAT(
crate::test_shared::get_heartbeat_msg(),
);
let client = mavlink::connect_async("udpout:127.0.0.1:14562")
.await
.expect("Couldn't create client");
loop {
client.send_default(&msg).await.ok();
}
}
});
//TODO use std::sync::WaitTimeoutResult to timeout ourselves if recv fails?
let mut recv_count = 0;
for _i in 0..RECEIVE_CHECK_COUNT {
match server.recv_raw().await {
Ok(message) => {
if message.message_id() == mavlink::dialects::common::HEARTBEAT_DATA::ID {
recv_count += 1;
} else {
// one message parse failure fails the test
break;
}
}
Err(..) => {
// one message read failure fails the test
break;
}
}
}
assert_eq!(recv_count, RECEIVE_CHECK_COUNT);
}
}