|
| 1 | +use std::time::Duration; |
| 2 | + |
1 | 3 | use bytes::Bytes; |
2 | 4 | use msg_socket::{RepSocket, ReqSocket}; |
3 | 5 | use msg_transport::{ |
@@ -144,3 +146,70 @@ async fn reqrep_mutual_tls_works() { |
144 | 146 | let response = req.request(hello.clone()).await.unwrap(); |
145 | 147 | assert_eq!(hello, response, "expected {:?}, got {:?}", hello, response); |
146 | 148 | } |
| 149 | + |
| 150 | +#[tokio::test] |
| 151 | +async fn reqrep_late_bind_works() { |
| 152 | + let _ = tracing_subscriber::fmt::try_init(); |
| 153 | + |
| 154 | + let mut rep = RepSocket::new(Tcp::default()); |
| 155 | + let mut req = ReqSocket::new(Tcp::default()); |
| 156 | + |
| 157 | + let local_addr = "localhost:64521"; |
| 158 | + req.connect(local_addr).await.unwrap(); |
| 159 | + |
| 160 | + let hello = Bytes::from_static(b"hello"); |
| 161 | + |
| 162 | + let reply = tokio::spawn(async move { req.request(hello.clone()).await.unwrap() }); |
| 163 | + |
| 164 | + tokio::time::sleep(Duration::from_millis(1000)).await; |
| 165 | + rep.bind(local_addr).await.unwrap(); |
| 166 | + |
| 167 | + let msg = rep.next().await.unwrap(); |
| 168 | + let payload = msg.msg().clone(); |
| 169 | + msg.respond(payload).unwrap(); |
| 170 | + |
| 171 | + let response = reply.await.unwrap(); |
| 172 | + let hello = Bytes::from_static(b"hello"); |
| 173 | + assert_eq!(hello, response, "expected {:?}, got {:?}", hello, response); |
| 174 | +} |
| 175 | + |
| 176 | +#[tokio::test] |
| 177 | +async fn reqrep_drop_server() { |
| 178 | + let _ = tracing_subscriber::fmt::try_init(); |
| 179 | + |
| 180 | + let mut rep = RepSocket::new(Tcp::default()); |
| 181 | + let mut req = ReqSocket::new(Tcp::default()); |
| 182 | + |
| 183 | + rep.bind("0.0.0.0:0").await.unwrap(); |
| 184 | + |
| 185 | + let addr = rep.local_addr().unwrap().clone(); |
| 186 | + req.connect(addr).await.unwrap(); |
| 187 | + |
| 188 | + tokio::spawn(async move { |
| 189 | + let request = rep.next().await.unwrap(); |
| 190 | + let msg = request.msg().clone(); |
| 191 | + request.respond(msg).unwrap(); |
| 192 | + |
| 193 | + drop(rep); |
| 194 | + }); |
| 195 | + |
| 196 | + let hello = Bytes::from_static(b"hello"); |
| 197 | + let response = req.request(hello.clone()).await.unwrap(); |
| 198 | + assert_eq!(hello, response, "expected {:?}, got {:?}", hello, response); |
| 199 | + |
| 200 | + match req.request(hello.clone()).await { |
| 201 | + Ok(response) => assert_eq!(hello, response, "expected {:?}, got {:?}", hello, response), |
| 202 | + Err(e) => tracing::warn!("Error: {:?}", e), |
| 203 | + } |
| 204 | + |
| 205 | + tokio::time::sleep(Duration::from_secs(60)).await; |
| 206 | + |
| 207 | + tokio::spawn(async move { |
| 208 | + req.request(hello.clone()).await.unwrap(); |
| 209 | + }); |
| 210 | + |
| 211 | + let mut rep = RepSocket::new(Tcp::default()); |
| 212 | + rep.bind(addr).await.unwrap(); |
| 213 | + |
| 214 | + tokio::time::sleep(Duration::from_millis(10000)).await; |
| 215 | +} |
0 commit comments