Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit e8e6ac1

Browse files
rvolosatovsdicej
authored andcommitted
test: connect and accept concurrently
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
1 parent d895200 commit e8e6ac1

6 files changed

Lines changed: 124 additions & 85 deletions

crates/test-programs/src/bin/sockets_0_3_tcp_bind.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,25 @@ async fn test_tcp_bind_reuseaddr(ip: IpAddress) {
6161

6262
let connect_addr =
6363
IpSocketAddress::new(IpAddress::new_loopback(ip.family()), bind_addr.port());
64-
client.connect(connect_addr).await.unwrap();
65-
66-
let mut sock = accept.next().await.unwrap().unwrap();
67-
assert_eq!(sock.len(), 1);
68-
let sock = sock.pop().unwrap();
69-
let (mut data_tx, data_rx) = wit_stream::new();
7064
join!(
7165
async {
72-
sock.send(data_rx).await.unwrap();
66+
client.connect(connect_addr).await.unwrap();
7367
},
7468
async {
75-
data_tx.send(vec![0; 10]).await.unwrap();
76-
drop(data_tx);
77-
}
69+
let mut sock = accept.next().await.unwrap().unwrap();
70+
assert_eq!(sock.len(), 1);
71+
let sock = sock.pop().unwrap();
72+
let (mut data_tx, data_rx) = wit_stream::new();
73+
join!(
74+
async {
75+
sock.send(data_rx).await.unwrap();
76+
},
77+
async {
78+
data_tx.send(vec![0; 10]).await.unwrap();
79+
drop(data_tx);
80+
}
81+
);
82+
},
7883
);
7984

8085
bind_addr

crates/test-programs/src/bin/sockets_0_3_tcp_connect.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use futures::{join, StreamExt as _};
12
use test_programs::p3::wasi::sockets::types::{
23
ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, TcpSocket,
34
};
@@ -96,12 +97,12 @@ async fn test_tcp_connect_dual_stack() {
9697
async fn test_tcp_connect_explicit_bind(family: IpAddressFamily) {
9798
let ip = IpAddress::new_loopback(family);
9899

99-
let listener = {
100+
let (listener, mut accept) = {
100101
let bind_address = IpSocketAddress::new(ip, 0);
101102
let listener = TcpSocket::new(family);
102103
listener.bind(bind_address).unwrap();
103-
listener.listen().unwrap();
104-
listener
104+
let accept = listener.listen().unwrap();
105+
(listener, accept)
105106
};
106107

107108
let listener_address = listener.local_address().unwrap();
@@ -111,7 +112,14 @@ async fn test_tcp_connect_explicit_bind(family: IpAddressFamily) {
111112
client.bind(IpSocketAddress::new(ip, 0)).unwrap();
112113

113114
// Connect should work:
114-
client.connect(listener_address).await.unwrap();
115+
join!(
116+
async {
117+
client.connect(listener_address).await.unwrap();
118+
},
119+
async {
120+
accept.next().await.unwrap().unwrap();
121+
}
122+
);
115123
}
116124

117125
impl test_programs::p3::exports::wasi::cli::run::Guest for Component {

crates/test-programs/src/bin/sockets_0_3_tcp_sample_application.rs

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,64 @@ async fn test_tcp_sample_application(family: IpAddressFamily, bind_address: IpSo
2020

2121
let addr = listener.local_address().unwrap();
2222

23-
{
24-
let client = TcpSocket::new(family);
25-
client.connect(addr).await.unwrap();
26-
let (mut data_tx, data_rx) = wit_stream::new();
27-
28-
join!(
29-
async {
30-
client.send(data_rx).await.unwrap();
31-
},
32-
async {
33-
data_tx.send(vec![]).await.unwrap();
34-
data_tx.send(first_message.into()).await.unwrap();
35-
drop(data_tx);
36-
}
37-
);
38-
}
39-
40-
{
41-
let mut sock = accept.next().await.unwrap().unwrap();
42-
assert_eq!(sock.len(), 1);
43-
let sock = sock.pop().unwrap();
44-
45-
let (mut data_rx, fut) = sock.receive();
46-
let data = data_rx.next().await.unwrap().unwrap();
47-
48-
// Check that we sent and received our message!
49-
assert_eq!(data, first_message); // Not guaranteed to work but should work in practice.
50-
fut.await.unwrap().unwrap().unwrap()
51-
}
23+
join!(
24+
async {
25+
let client = TcpSocket::new(family);
26+
client.connect(addr).await.unwrap();
27+
let (mut data_tx, data_rx) = wit_stream::new();
28+
join!(
29+
async {
30+
client.send(data_rx).await.unwrap();
31+
},
32+
async {
33+
data_tx.send(vec![]).await.unwrap();
34+
data_tx.send(first_message.into()).await.unwrap();
35+
drop(data_tx);
36+
}
37+
);
38+
},
39+
async {
40+
let mut sock = accept.next().await.unwrap().unwrap();
41+
assert_eq!(sock.len(), 1);
42+
let sock = sock.pop().unwrap();
43+
44+
let (mut data_rx, fut) = sock.receive();
45+
let data = data_rx.next().await.unwrap().unwrap();
46+
47+
// Check that we sent and received our message!
48+
assert_eq!(data, first_message); // Not guaranteed to work but should work in practice.
49+
fut.await.unwrap().unwrap().unwrap()
50+
},
51+
);
5252

5353
// Another client
54-
{
55-
let client = TcpSocket::new(family);
56-
client.connect(addr).await.unwrap();
57-
let (mut data_tx, data_rx) = wit_stream::new();
58-
join!(
59-
async {
60-
client.send(data_rx).await.unwrap();
61-
},
62-
async {
63-
data_tx.send(second_message.into()).await.unwrap();
64-
drop(data_tx);
65-
}
66-
);
67-
}
68-
69-
{
70-
let mut sock = accept.next().await.unwrap().unwrap();
71-
assert_eq!(sock.len(), 1);
72-
let sock = sock.pop().unwrap();
73-
let (mut data_rx, fut) = sock.receive();
74-
let data = data_rx.next().await.unwrap().unwrap();
75-
76-
// Check that we sent and received our message!
77-
assert_eq!(data, second_message); // Not guaranteed to work but should work in practice.
78-
fut.await.unwrap().unwrap().unwrap()
79-
}
54+
join!(
55+
async {
56+
let client = TcpSocket::new(family);
57+
client.connect(addr).await.unwrap();
58+
let (mut data_tx, data_rx) = wit_stream::new();
59+
join!(
60+
async {
61+
client.send(data_rx).await.unwrap();
62+
},
63+
async {
64+
data_tx.send(second_message.into()).await.unwrap();
65+
drop(data_tx);
66+
}
67+
);
68+
},
69+
async {
70+
let mut sock = accept.next().await.unwrap().unwrap();
71+
assert_eq!(sock.len(), 1);
72+
let sock = sock.pop().unwrap();
73+
let (mut data_rx, fut) = sock.receive();
74+
let data = data_rx.next().await.unwrap().unwrap();
75+
76+
// Check that we sent and received our message!
77+
assert_eq!(data, second_message); // Not guaranteed to work but should work in practice.
78+
fut.await.unwrap().unwrap().unwrap()
79+
}
80+
);
8081
}
8182

8283
impl test_programs::p3::exports::wasi::cli::run::Guest for Component {

crates/test-programs/src/bin/sockets_0_3_tcp_sockopts.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use futures::StreamExt as _;
1+
use futures::{join, StreamExt as _};
22
use test_programs::p3::wasi::sockets::types::{
33
ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, TcpSocket,
44
};
@@ -132,10 +132,16 @@ async fn test_tcp_sockopt_inheritance(family: IpAddressFamily) {
132132
let mut accept = listener.listen().unwrap();
133133
let bound_addr = listener.local_address().unwrap();
134134
let client = TcpSocket::new(family);
135-
client.connect(bound_addr).await.unwrap();
136-
let mut sock = accept.next().await.unwrap().unwrap();
137-
assert_eq!(sock.len(), 1);
138-
let sock = sock.pop().unwrap();
135+
let ((), sock) = join!(
136+
async {
137+
client.connect(bound_addr).await.unwrap();
138+
},
139+
async {
140+
let mut sock = accept.next().await.unwrap().unwrap();
141+
assert_eq!(sock.len(), 1);
142+
sock.pop().unwrap()
143+
}
144+
);
139145

140146
// Verify options on accepted socket:
141147
{
@@ -194,10 +200,16 @@ async fn test_tcp_sockopt_after_listen(family: IpAddressFamily) {
194200
}
195201

196202
let client = TcpSocket::new(family);
197-
client.connect(bound_addr).await.unwrap();
198-
let mut sock = accept.next().await.unwrap().unwrap();
199-
assert_eq!(sock.len(), 1);
200-
let sock = sock.pop().unwrap();
203+
let ((), sock) = join!(
204+
async {
205+
client.connect(bound_addr).await.unwrap();
206+
},
207+
async {
208+
let mut sock = accept.next().await.unwrap().unwrap();
209+
assert_eq!(sock.len(), 1);
210+
sock.pop().unwrap()
211+
}
212+
);
201213

202214
// Verify options on accepted socket:
203215
{

crates/test-programs/src/bin/sockets_0_3_tcp_states.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use futures::{join, StreamExt as _};
12
use test_programs::p3::wasi::sockets::types::{
23
ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, TcpSocket,
34
};
@@ -146,10 +147,17 @@ async fn test_tcp_connected_state_invariants(family: IpAddressFamily) {
146147
let bind_address = IpSocketAddress::new(IpAddress::new_loopback(family), 0);
147148
let sock_listener = TcpSocket::new(family);
148149
sock_listener.bind(bind_address).unwrap();
149-
sock_listener.listen().unwrap();
150+
let mut accept = sock_listener.listen().unwrap();
150151
let addr_listener = sock_listener.local_address().unwrap();
151152
let sock = TcpSocket::new(family);
152-
sock.connect(addr_listener).await.unwrap();
153+
join!(
154+
async {
155+
sock.connect(addr_listener).await.unwrap();
156+
},
157+
async {
158+
accept.next().await.unwrap().unwrap();
159+
}
160+
);
153161

154162
assert_eq!(sock.bind(bind_address), Err(ErrorCode::InvalidState));
155163
assert_eq!(

crates/test-programs/src/bin/sockets_0_3_tcp_streams.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@ async fn setup<Fut: Future<Output = ()>>(
140140
let mut accept = listener.listen().unwrap();
141141
let bound_address = listener.local_address().unwrap();
142142
let client_socket = TcpSocket::new(family);
143-
client_socket.connect(bound_address).await.unwrap();
144-
let mut accepted_socket = accept.next().await.unwrap().unwrap();
145-
assert_eq!(accepted_socket.len(), 1);
146-
let accepted_socket = accepted_socket.pop().unwrap();
147-
143+
let ((), accepted_socket) = join!(
144+
async {
145+
client_socket.connect(bound_address).await.unwrap();
146+
},
147+
async {
148+
let mut accepted_socket = accept.next().await.unwrap().unwrap();
149+
assert_eq!(accepted_socket.len(), 1);
150+
accepted_socket.pop().unwrap()
151+
},
152+
);
148153
body(accepted_socket, client_socket).await;
149154
}

0 commit comments

Comments
 (0)