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
use ruknet::{app::RukMessageContext, Peer, Priority, Reliability};
#[tokio::test]
async fn big_message() {
let mut server = Peer::new("127.0.0.1:19132", "ping response").await.unwrap();
let mut client = Peer::new("127.0.0.1:19134", "ping response").await.unwrap();
server.listen(10).await.unwrap();
client.listen(1).await.unwrap();
client.connect("127.0.0.1:19132", 3, 2000, None).unwrap();
let server_task = tokio::spawn(async move {
loop {
tokio::time::sleep(tokio::time::Duration::from_millis(25)).await;
if let Some(msg) = server.recv() {
if let RukMessageContext::App { data } = msg.context {
server
.send(
msg.addr,
Priority::Immediate,
Reliability::Reliable,
0,
data,
)
.await;
// wait for the tick to send the message. data is big, so it will take some time because of slow start algorithm.
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
break;
}
}
}
});
let client_task = tokio::spawn(async move {
// first byte is the message id. Some of them are reserved for internal use.
let mut send_data = vec![0xfe];
// very big message
send_data.extend_from_slice(&[0; 1024 * 4]);
let timeout_duration = tokio::time::Duration::from_secs(5);
let result = tokio::time::timeout(timeout_duration, async {
loop {
tokio::time::sleep(tokio::time::Duration::from_millis(25)).await;
if let Some(msg) = client.recv() {
match msg.context {
RukMessageContext::ConnectionRequestAccepted => {
client
.send(
msg.addr,
Priority::Immediate,
Reliability::Reliable,
0,
send_data.clone(),
)
.await;
}
RukMessageContext::App { data } => {
if data == send_data {
break;
}
}
_ => {}
}
}
}
})
.await;
assert!(result.is_ok());
});
server_task.await.unwrap();
client_task.await.unwrap();
}