From 334d4eb22a5faa005ccb8b705ae8c6c8857e2e40 Mon Sep 17 00:00:00 2001 From: Maki-Grz Date: Thu, 18 Dec 2025 09:56:08 +0100 Subject: [PATCH] feat: add connect method to verify gRPC connectivity - Implemented connect() in ChannelPool to force channel initialization - Added pub async fn connect() to Qdrant client - Added integration test to verify failure on invalid URLs --- src/channel_pool.rs | 7 +++++ src/qdrant_client/mod.rs | 8 ++++++ tests/snippet_tests/mod.rs | 1 + tests/snippet_tests/test_connect.rs | 20 +++++++++++++ .../test_verify_connection_failure.rs | 28 +++++++++++++++++++ tests/snippets/connect.rs | 10 +++++++ 6 files changed, 74 insertions(+) create mode 100644 tests/snippet_tests/test_connect.rs create mode 100644 tests/snippet_tests/test_verify_connection_failure.rs create mode 100644 tests/snippets/connect.rs diff --git a/src/channel_pool.rs b/src/channel_pool.rs index aa963488..484685ce 100644 --- a/src/channel_pool.rs +++ b/src/channel_pool.rs @@ -154,6 +154,13 @@ impl ChannelPool { 0 } } + + /// Explicitly establishes a connection to at least one channel in the pool. + /// This triggers the underlying gRPC handshake. + pub async fn connect(&self) -> Result<(), Status> { + let _ = self.get_channel().await?; + Ok(()) + } } // The future returned by get_channel needs to be Send so that the client can be diff --git a/src/qdrant_client/mod.rs b/src/qdrant_client/mod.rs index bae43ee0..73e461cf 100644 --- a/src/qdrant_client/mod.rs +++ b/src/qdrant_client/mod.rs @@ -229,4 +229,12 @@ impl Qdrant { }) .await } + + /// Performs a connectivity check to the Qdrant server. + /// + /// This method forces the client to establish a connection immediately. + /// Returns an error if the TCP connection or gRPC handshake fails. + pub async fn connect(&self) -> QdrantResult<()> { + self.channel.connect().await.map_err(Into::into) + } } diff --git a/tests/snippet_tests/mod.rs b/tests/snippet_tests/mod.rs index 453928c4..a1aa0d38 100644 --- a/tests/snippet_tests/mod.rs +++ b/tests/snippet_tests/mod.rs @@ -1,6 +1,7 @@ mod test_batch_update; mod test_clear_payload; mod test_collection_exists; +mod test_connect; mod test_count_points; mod test_create_collection; mod test_create_collection_with_bq; diff --git a/tests/snippet_tests/test_connect.rs b/tests/snippet_tests/test_connect.rs new file mode 100644 index 00000000..b1978a9b --- /dev/null +++ b/tests/snippet_tests/test_connect.rs @@ -0,0 +1,20 @@ + +#[tokio::test] +async fn test_connect() { + async fn connect() -> Result<(), Box> { + // WARNING: This is a generated test snippet. + // Please, modify the snippet in the `../snippets/connect.rs` file + use qdrant_client::Qdrant; + use std::time::Duration; + + let client = Qdrant::from_url("http://localhost:1234") + .connect_timeout(Duration::from_millis(500)) + .build()?; + + let connection_result = client.connect().await; + + assert!(connection_result.is_err()); + Ok(()) + } + let _ = connect().await; +} diff --git a/tests/snippet_tests/test_verify_connection_failure.rs b/tests/snippet_tests/test_verify_connection_failure.rs new file mode 100644 index 00000000..4dd5b002 --- /dev/null +++ b/tests/snippet_tests/test_verify_connection_failure.rs @@ -0,0 +1,28 @@ + +#[tokio::test] +async fn test_verify_connection_failure() { + async fn verify_connection_failure() -> Result<(), Box> { + // WARNING: This is a generated test snippet. + // Please, modify the snippet in the `../snippets/verify_connection_failure.rs` file + #[tokio::test] + async fn test_verify_connection_failure() { + use crate::qdrant_client::Qdrant; + + // We use an address that doesn't respond + let client = Qdrant::from_url("http://127.0.0.1:1234") + .connect_timeout(std::time::Duration::from_millis(500)) + .build() + .unwrap(); + + // build() always succeeds (lazy behavior), + // but connect() should detect the failure. + let result = client.connect().await; + assert!( + result.is_err(), + "The connection should have failed on an invalid port" + ); + } + Ok(()) + } + let _ = verify_connection_failure().await; +} diff --git a/tests/snippets/connect.rs b/tests/snippets/connect.rs new file mode 100644 index 00000000..602604c9 --- /dev/null +++ b/tests/snippets/connect.rs @@ -0,0 +1,10 @@ +use qdrant_client::Qdrant; +use std::time::Duration; + +let client = Qdrant::from_url("http://localhost:1234") + .connect_timeout(Duration::from_millis(500)) + .build()?; + +let connection_result = client.connect().await; + +assert!(connection_result.is_err());