Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter for Client request timeout #1338

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions async-nats/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ impl Client {
}
}

/// Returns the default timeout for requests set when creating the client.
///
/// # Examples
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
/// let client = async_nats::connect("demo.nats.io").await?;
/// println!("default request timeout: {:?}", client.timeout());
/// # Ok(())
/// # }
/// ```
pub fn timeout(&self) -> Option<Duration> {
self.request_timeout
}

/// Returns last received info from the server.
///
/// # Examples
Expand Down
24 changes: 24 additions & 0 deletions async-nats/tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,4 +996,28 @@ mod client {
assert!(stats.out_bytes.load(Ordering::Relaxed) != 0);
assert_eq!(stats.connects.load(Ordering::Relaxed), 2);
}

#[tokio::test]
async fn client_timeout() {
let server = nats_server::run_basic_server();
let client = async_nats::connect(server.client_url()).await.unwrap();

assert_eq!(client.timeout(), Some(Duration::from_secs(10)));

let client = async_nats::ConnectOptions::new()
.request_timeout(Some(Duration::from_secs(30)))
.connect(server.client_url())
.await
.unwrap();

assert_eq!(client.timeout(), Some(Duration::from_secs(30)));

let client = async_nats::ConnectOptions::new()
.request_timeout(None)
.connect(server.client_url())
.await
.unwrap();

assert_eq!(client.timeout(), None);
}
}
Loading