Skip to content

Commit

Permalink
Add getter for Client request timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <[email protected]>
  • Loading branch information
Jarema committed Oct 31, 2024
1 parent e40abcf commit 5220ff6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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);
}
}

0 comments on commit 5220ff6

Please sign in to comment.