Skip to content

Commit ef5ffd3

Browse files
WenyXucrepererum
authored andcommitted
test: test request timeout
Signed-off-by: WenyXu <wenymedia@gmail.com>
1 parent abe443d commit ef5ffd3

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/messenger.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ where
420420
let mut response = if let Some(timeout) = self.timeout {
421421
// If a request times out, return a `RequestError::IO` with a timeout error.
422422
// This allows the backoff mechanism to detect transport issues and re-establish the connection as needed.
423-
//
423+
//
424424
// Typically, timeouts occur due to abrupt TCP connection loss (e.g., a disconnected cable).
425425
tokio::time::timeout(timeout, rx).await.map_err(|_| {
426426
RequestError::IO(std::io::Error::new(
@@ -1379,6 +1379,31 @@ mod tests {
13791379
handle_network.abort();
13801380
}
13811381

1382+
#[tokio::test]
1383+
async fn test_request_timeout() {
1384+
let (tx, _rx) = tokio::io::duplex(1_000);
1385+
let mut messenger = Messenger::new(
1386+
tx,
1387+
1_000,
1388+
Arc::from(DEFAULT_CLIENT_ID),
1389+
Some(Duration::from_millis(200)),
1390+
);
1391+
messenger.set_version_ranges(HashMap::from([(
1392+
ApiKey::ApiVersions,
1393+
ApiVersionsRequest::API_VERSION_RANGE,
1394+
)]));
1395+
1396+
let err = messenger
1397+
.request(ApiVersionsRequest {
1398+
client_software_name: Some(CompactString(String::from("foo"))),
1399+
client_software_version: Some(CompactString(String::from("bar"))),
1400+
tagged_fields: Some(TaggedFields::default()),
1401+
})
1402+
.await
1403+
.unwrap_err();
1404+
assert_matches!(err, RequestError::IO(e) if e.kind() == std::io::ErrorKind::TimedOut);
1405+
}
1406+
13821407
#[derive(Debug)]
13831408
enum Message {
13841409
Send(Vec<u8>),

0 commit comments

Comments
 (0)