This is an automated email from the ASF dual-hosted git repository.
viirya pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git
The following commit(s) were added to refs/heads/master by this push:
new a75b3dbf [SPARK-61430][CONNECT][TESTS] Make the request structure
tests of the client send their requests
a75b3dbf is described below
commit a75b3dbf44e646d9905c0388f5ee88f1e382e873
Author: Liang-Chi Hsieh <[email protected]>
AuthorDate: Tue Nov 10 20:26:03 2026 -0800
[SPARK-61430][CONNECT][TESTS] Make the request structure tests of the
client send their requests
### What changes were proposed in this pull request?
Seven unit tests of `crates/spark-connect-core/src/client.rs` now send
their request to the recording service of `request_fields_tests` and assert
what the server received:
| Test | Asserted request |
|---|---|
| `test_get_configs_request_structure` | `Get { keys }`, in order |
| `test_set_config_request_structure` | `Set { pairs: [(key, Some(value))],
silent: None }` |
| `test_unset_config_request_structure` | `Unset { keys: [key] }` |
| `test_interrupt_all_request_structure` | `InterruptType::All`, no
interrupt |
| `test_interrupt_tag_request_structure` | `InterruptType::Tag`,
`OperationTag(tag)` |
| `test_interrupt_operation_request_structure` |
`InterruptType::OperationId`, `OperationId(id)` |
| `test_release_session_request_structure` | client type is the user agent,
`allow_reconnect` unset |
Each also asserts the session ID, the user ID of the user context, and that
the server received that one request only.
The tests keep their names and move from `mod tests` to `mod
request_fields_tests`, where the recorder is, with four small helpers there
(`recorded_client`, `only_request`, `config_operation`, `interruption`).
`test_metadata_headers` now also asserts that the header of the connection
string and the user agent are attached to a request. No non-test code changes.
### Why are the changes needed?
The seven tests built the future of the call and dropped it without polling
it:
```rust
let client = SparkConnectClient::connect(&builder).await.unwrap();
// Note: This will fail against a real server, but tests the structure
let _result = client.get_configs(&["spark.sql.shuffle.partitions"]);
```
No request was built or sent, so they passed whatever the methods did. With
the request construction of all seven methods deliberately broken (empty keys,
a wrong key, interrupt type 99, a wrong tag, operation ID and session ID), all
seven still passed.
No other test covered these fields:
`requests_carry_the_client_type_extensions_and_observed_session_of_pyspark`
calls `config` and `interrupt` with empty requests and checks the client type,
the extensions and the observed server-side session ID only.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
`cargo test -p apache-spark-connect-core --release --lib` (181 passed) and
`cargo fmt --all --check`.
With the same deliberate breakage of the seven methods, the rewritten tests
all fail, e.g.
```
test_get_configs_request_structure: left: Get(Get { keys: [] })
test_interrupt_all_request_structure: left: (99, None) right: (1, None)
test_interrupt_tag_request_structure: left: (2,
Some(OperationTag("broken")))
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code
Closes #506 from viirya/SPARK-61430-client-request-tests.
Authored-by: Liang-Chi Hsieh <[email protected]>
Signed-off-by: Liang-Chi Hsieh <[email protected]>
---
crates/spark-connect-core/src/client.rs | 245 +++++++++++++++++++++++---------
1 file changed, 180 insertions(+), 65 deletions(-)
diff --git a/crates/spark-connect-core/src/client.rs
b/crates/spark-connect-core/src/client.rs
index 968c4cb2..7d2f2d85 100644
--- a/crates/spark-connect-core/src/client.rs
+++ b/crates/spark-connect-core/src/client.rs
@@ -2819,6 +2819,17 @@ mod tests {
let builder =
ChannelBuilder::parse("sc://localhost/;custom_header=custom_value").unwrap();
let client = SparkConnectClient::connect(&builder).await.unwrap();
assert!(client.user_agent.contains("spark/"));
+ // The header of the connection string and the user agent are on a
request.
+ let mut request = Request::new(());
+ client._attach_metadata(&mut request);
+ assert_eq!(
+ request.metadata().get("custom_header").unwrap(),
+ "custom_value"
+ );
+ assert_eq!(
+ request.metadata().get("user-agent").unwrap(),
+ client.user_agent.as_str()
+ );
}
#[test]
@@ -3064,71 +3075,6 @@ mod tests {
}
}
- #[tokio::test]
- async fn test_get_configs_request_structure() {
- let builder = ChannelBuilder::parse("sc://localhost").unwrap();
- let client = SparkConnectClient::connect(&builder).await.unwrap();
-
- // Test that get_configs returns a Vec of Option<String>
- // Note: This will fail against a real server, but tests the structure
- let _result = client.get_configs(&["spark.sql.shuffle.partitions"]);
- // We don't assert on the result since we may not have a server
- }
-
- #[tokio::test]
- async fn test_set_config_request_structure() {
- let builder = ChannelBuilder::parse("sc://localhost").unwrap();
- let client = SparkConnectClient::connect(&builder).await.unwrap();
-
- // Test that set_config can be called without panicking
- let _result = client.set_config("spark.sql.shuffle.partitions", "200");
- }
-
- #[tokio::test]
- async fn test_unset_config_request_structure() {
- let builder = ChannelBuilder::parse("sc://localhost").unwrap();
- let client = SparkConnectClient::connect(&builder).await.unwrap();
-
- // Test that unset_config can be called without panicking
- let _result = client.unset_config("spark.sql.shuffle.partitions");
- }
-
- #[tokio::test]
- async fn test_interrupt_all_request_structure() {
- let builder = ChannelBuilder::parse("sc://localhost").unwrap();
- let client = SparkConnectClient::connect(&builder).await.unwrap();
-
- // Test that interrupt_all can be called without panicking
- let _result = client.interrupt_all();
- }
-
- #[tokio::test]
- async fn test_interrupt_tag_request_structure() {
- let builder = ChannelBuilder::parse("sc://localhost").unwrap();
- let client = SparkConnectClient::connect(&builder).await.unwrap();
-
- // Test that interrupt_tag can be called without panicking
- let _result = client.interrupt_tag("my-tag");
- }
-
- #[tokio::test]
- async fn test_interrupt_operation_request_structure() {
- let builder = ChannelBuilder::parse("sc://localhost").unwrap();
- let client = SparkConnectClient::connect(&builder).await.unwrap();
-
- // Test that interrupt_operation can be called without panicking
- let _result = client.interrupt_operation("operation-123");
- }
-
- #[tokio::test]
- async fn test_release_session_request_structure() {
- let builder = ChannelBuilder::parse("sc://localhost").unwrap();
- let client = SparkConnectClient::connect(&builder).await.unwrap();
-
- // Test that release_session can be called without panicking
- let _result = client.release_session();
- }
-
/// A loopback service of ReleaseSession only: its first `unavailable`
calls answer
/// UNAVAILABLE, the later ones succeed. `calls` counts every call.
struct ReleaseCalls {
@@ -3976,6 +3922,175 @@ mod request_fields_tests {
}
}
+ /// A client of the user `u1` of a recording server, what that server
received, and the
+ /// server.
+ async fn recorded_client() -> (SparkConnectClient, Received,
tokio::task::JoinHandle<()>) {
+ let received: Received = Default::default();
+ let listener =
tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let address = listener.local_addr().unwrap();
+ let incoming = futures::stream::unfold(listener, |listener| async {
+ Some((listener.accept().await.map(|(socket, _)| socket), listener))
+ });
+ let service = Recorder(received.clone());
+ let server = tokio::spawn(async move {
+ tonic::transport::Server::builder()
+ .add_service(service)
+ .serve_with_incoming(incoming)
+ .await
+ .unwrap();
+ });
+ let builder = {
+ let _env =
crate::channel::set_env(&[("SPARK_CONNECT_AUTHENTICATE_TOKEN", None)]);
+ ChannelBuilder::parse(&format!("sc://127.0.0.1:{}/;user_id=u1",
address.port()))
+ .unwrap()
+ };
+ let client = SparkConnectClient::connect(&builder).await.unwrap();
+ (client, received, server)
+ }
+
+ /// The only request that the server received, which is of `method`.
+ fn only_request<T: Message + Default>(received: &Received, method: &str)
-> T {
+ let requests = received.lock().unwrap();
+ let methods: Vec<&str> = requests.iter().map(|(m, _)|
m.as_str()).collect();
+ assert_eq!(methods, [method]);
+ T::decode(requests[0].1.as_slice()).unwrap()
+ }
+
+ /// The operation of the only request that the server received, a Config
of the session and
+ /// the user of `client`.
+ fn config_operation(
+ client: &SparkConnectClient,
+ received: &Received,
+ ) -> spark_connect_proto::config_request::operation::OpType {
+ let request: ConfigRequest = only_request(received, "Config");
+ assert_eq!(request.session_id, client.session_id());
+ assert_eq!(request.user_context.unwrap().user_id, "u1");
+ request.operation.unwrap().op_type.unwrap()
+ }
+
+ /// The interrupt type and the interrupt of the only request that the
server received, an
+ /// Interrupt of the session and the user of `client`.
+ fn interruption(
+ client: &SparkConnectClient,
+ received: &Received,
+ ) -> (
+ i32,
+ Option<spark_connect_proto::interrupt_request::Interrupt>,
+ ) {
+ let request: InterruptRequest = only_request(received, "Interrupt");
+ assert_eq!(request.session_id, client.session_id());
+ assert_eq!(request.user_context.unwrap().user_id, "u1");
+ (request.interrupt_type, request.interrupt)
+ }
+
+ #[tokio::test]
+ async fn test_get_configs_request_structure() {
+ use spark_connect_proto::config_request::{operation::OpType, Get};
+ let (client, received, server) = recorded_client().await;
+ let keys = ["spark.sql.shuffle.partitions", "spark.sql.ansi.enabled"];
+ // The server answers no pair, so each key is unset.
+ assert_eq!(client.get_configs(&keys).await.unwrap(), [None, None]);
+ server.abort();
+ assert_eq!(
+ config_operation(&client, &received),
+ OpType::Get(Get {
+ keys: keys.map(str::to_string).to_vec(),
+ })
+ );
+ }
+
+ #[tokio::test]
+ async fn test_set_config_request_structure() {
+ use spark_connect_proto::config_request::{operation::OpType, Set};
+ let (client, received, server) = recorded_client().await;
+ client
+ .set_config("spark.sql.shuffle.partitions", "200")
+ .await
+ .unwrap();
+ server.abort();
+ assert_eq!(
+ config_operation(&client, &received),
+ OpType::Set(Set {
+ pairs: vec![spark_connect_proto::KeyValue {
+ key: "spark.sql.shuffle.partitions".into(),
+ value: Some("200".into()),
+ }],
+ silent: None,
+ })
+ );
+ }
+
+ #[tokio::test]
+ async fn test_unset_config_request_structure() {
+ use spark_connect_proto::config_request::{operation::OpType, Unset};
+ let (client, received, server) = recorded_client().await;
+ client
+ .unset_config("spark.sql.shuffle.partitions")
+ .await
+ .unwrap();
+ server.abort();
+ assert_eq!(
+ config_operation(&client, &received),
+ OpType::Unset(Unset {
+ keys: vec!["spark.sql.shuffle.partitions".into()],
+ })
+ );
+ }
+
+ #[tokio::test]
+ async fn test_interrupt_all_request_structure() {
+ use spark_connect_proto::interrupt_request::InterruptType;
+ let (client, received, server) = recorded_client().await;
+ client.interrupt_all().await.unwrap();
+ server.abort();
+ assert_eq!(
+ interruption(&client, &received),
+ (InterruptType::All as i32, None)
+ );
+ }
+
+ #[tokio::test]
+ async fn test_interrupt_tag_request_structure() {
+ use spark_connect_proto::interrupt_request::{Interrupt, InterruptType};
+ let (client, received, server) = recorded_client().await;
+ client.interrupt_tag("my-tag").await.unwrap();
+ server.abort();
+ assert_eq!(
+ interruption(&client, &received),
+ (
+ InterruptType::Tag as i32,
+ Some(Interrupt::OperationTag("my-tag".into()))
+ )
+ );
+ }
+
+ #[tokio::test]
+ async fn test_interrupt_operation_request_structure() {
+ use spark_connect_proto::interrupt_request::{Interrupt, InterruptType};
+ let (client, received, server) = recorded_client().await;
+ client.interrupt_operation("operation-123").await.unwrap();
+ server.abort();
+ assert_eq!(
+ interruption(&client, &received),
+ (
+ InterruptType::OperationId as i32,
+ Some(Interrupt::OperationId("operation-123".into()))
+ )
+ );
+ }
+
+ #[tokio::test]
+ async fn test_release_session_request_structure() {
+ let (client, received, server) = recorded_client().await;
+ client.release_session().await.unwrap();
+ server.abort();
+ let request: ReleaseSessionRequest = only_request(&received,
"ReleaseSession");
+ assert_eq!(request.session_id, client.session_id());
+ assert_eq!(request.user_context.unwrap().user_id, "u1");
+ assert_eq!(request.client_type, Some(client.user_agent.clone()));
+ assert!(!request.allow_reconnect);
+ }
+
#[tokio::test]
async fn requests_carry_no_user_id_header_as_pyspark() {
// PySpark's `ChannelBuilder.metadata()` leaves the user_id parameter
out: the user ID is
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]