This is an automated email from the ASF dual-hosted git repository.

cloud-fan pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 315d0d626237 [SPARK-58407][SQL][TEST][CONNECT] Fix 
connect.SessionQueryTest beforeAll, afterAll
315d0d626237 is described below

commit 315d0d626237786f906b0d017527a2bad7eba696
Author: Matthis Gördel <[email protected]>
AuthorDate: Sat Aug 8 01:03:53 2026 +0800

    [SPARK-58407][SQL][TEST][CONNECT] Fix connect.SessionQueryTest beforeAll, 
afterAll
    
    ### What changes were proposed in this pull request?
    
    Changes `connect.SparkSessionBinder` setup / teardown, so that suites can 
use `spark` in `beforeAll` and `afterAll`.
    
    ### Why are the changes needed?
    
    Some tests do setup/cleanup calls (e.g. create/delete some shared table) in 
`beforeAll`/`afterAll`.
    
    While this works with `sql.SessionQueryTest`, this is broken in 
`connect.SessionQueryTest`.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No
    
    ### How was this patch tested?
    
    This is a test-only change
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    No
    
    Closes #57606 from fwc/fix-connect-sessionquerytest-lifecycle.
    
    Lead-authored-by: Matthis Gördel <[email protected]>
    Co-authored-by: Matthis Gördel <[email protected]>
    Signed-off-by: Wenchen Fan <[email protected]>
    (cherry picked from commit 56dcb54ca13b0937e43ccb36672d6e1df33a0d91)
    Signed-off-by: Wenchen Fan <[email protected]>
---
 .../SessionQueryTestBeforeAfterHooksSuite.scala    | 24 +++++++++++
 .../spark/sql/connect/SparkSessionBinder.scala     | 17 +++++---
 .../SessionQueryTestBeforeAfterHooksSuite.scala    | 47 ++++++++++++++++++++++
 3 files changed, 82 insertions(+), 6 deletions(-)

diff --git 
a/sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SessionQueryTestBeforeAfterHooksSuite.scala
 
b/sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SessionQueryTestBeforeAfterHooksSuite.scala
new file mode 100644
index 000000000000..9cc30139859b
--- /dev/null
+++ 
b/sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SessionQueryTestBeforeAfterHooksSuite.scala
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.connect
+
+import org.apache.spark.sql
+
+class SessionQueryTestBeforeAfterHooksSuite
+    extends sql.SessionQueryTestBeforeAfterHooksSuite
+    with SessionQueryTest
diff --git 
a/sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SparkSessionBinder.scala
 
b/sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SparkSessionBinder.scala
index b30bc55f7df1..fea98122ed8a 100644
--- 
a/sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SparkSessionBinder.scala
+++ 
b/sql/connect/server/src/test/scala/org/apache/spark/sql/connect/SparkSessionBinder.scala
@@ -38,7 +38,8 @@ trait SparkSessionBinder extends sql.SparkSessionBinder { 
self: SparkFunSuite =>
   protected override def spark: SparkSession = _connectSpark
 
   override protected def beforeAll(): Unit = {
-    super.beforeAll()
+    initializeSession()
+
     // Other suites using mocks leave a mess in the global executionManager,
     // shut it down so that it's cleared before starting server.
     SparkConnectService.executionManager.shutdown()
@@ -60,14 +61,18 @@ trait SparkSessionBinder extends sql.SparkSessionBinder { 
self: SparkFunSuite =>
       .builder()
       .client(client)
       .create()
+    super.beforeAll()
   }
 
   override def afterAll(): Unit = {
-    if (_connectSpark != null) {
-      _connectSpark.close()
-      _connectSpark = null
+    try {
+      super.afterAll()
+    } finally {
+      if (_connectSpark != null) {
+        _connectSpark.close()
+        _connectSpark = null
+      }
+      SparkConnectService.stop()
     }
-    SparkConnectService.stop()
-    super.afterAll()
   }
 }
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/SessionQueryTestBeforeAfterHooksSuite.scala
 
b/sql/core/src/test/scala/org/apache/spark/sql/SessionQueryTestBeforeAfterHooksSuite.scala
new file mode 100644
index 000000000000..f3253def7113
--- /dev/null
+++ 
b/sql/core/src/test/scala/org/apache/spark/sql/SessionQueryTestBeforeAfterHooksSuite.scala
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql
+
+class SessionQueryTestBeforeAfterHooksSuite extends SessionQueryTest {
+
+  override protected def beforeAll(): Unit = {
+    super.beforeAll()
+    checkAnswer(spark.sql("SELECT 1"), Seq(Row(1)))
+  }
+
+  override protected def beforeEach(): Unit = {
+    checkAnswer(spark.sql("SELECT 1"), Seq(Row(1)))
+    super.beforeEach()
+    checkAnswer(spark.sql("SELECT 1"), Seq(Row(1)))
+  }
+
+  override protected def afterEach(): Unit = {
+    checkAnswer(spark.sql("SELECT 1"), Seq(Row(1)))
+    super.afterEach()
+    checkAnswer(spark.sql("SELECT 1"), Seq(Row(1)))
+  }
+
+  override protected def afterAll(): Unit = {
+    checkAnswer(spark.sql("SELECT 1"), Seq(Row(1)))
+    super.afterAll()
+  }
+
+  test("assert spark is available in BeforeAndAfter hooks") {
+    checkAnswer(spark.sql("SELECT 1"), Seq(Row(1)))
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to