Skip to content

Commit 7dba32a

Browse files
committed
SparkSessionBuilderSuite should clean up stopped sessions.
`SparkSessionBuilderSuite` should clean up stopped sessions. Otherwise, it leaves behind some stopped `SparkContext`s interfereing with other test suites using `ShardSQLContext`. Recently, master branch fails consequtively. - https://amplab.cs.berkeley.edu/jenkins/view/Spark%20QA%20Test%20(Dashboard)/ Pass the Jenkins with a updated suite. Author: Dongjoon Hyun <[email protected]> Closes #18567 from dongjoon-hyun/SPARK-SESSION.
1 parent 7f7b63b commit 7dba32a

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

sql/core/src/test/scala/org/apache/spark/sql/SparkSessionBuilderSuite.scala

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,48 @@
1717

1818
package org.apache.spark.sql
1919

20+
import org.scalatest.BeforeAndAfterEach
21+
2022
import org.apache.spark.{SparkConf, SparkContext, SparkFunSuite}
2123

2224
/**
2325
* Test cases for the builder pattern of [[SparkSession]].
2426
*/
25-
class SparkSessionBuilderSuite extends SparkFunSuite {
27+
class SparkSessionBuilderSuite extends SparkFunSuite with BeforeAndAfterEach {
2628

27-
private var initialSession: SparkSession = _
29+
override def afterEach(): Unit = {
30+
// This suite should not interfere with the other test suites.
31+
SparkSession.getActiveSession.foreach(_.stop())
32+
SparkSession.clearActiveSession()
33+
SparkSession.getDefaultSession.foreach(_.stop())
34+
SparkSession.clearDefaultSession()
35+
}
2836

29-
private lazy val sparkContext: SparkContext = {
30-
initialSession = SparkSession.builder()
37+
test("create with config options and propagate them to SparkContext and SparkSession") {
38+
val session = SparkSession.builder()
3139
.master("local")
3240
.config("spark.ui.enabled", value = false)
3341
.config("some-config", "v2")
3442
.getOrCreate()
35-
initialSession.sparkContext
36-
}
37-
38-
test("create with config options and propagate them to SparkContext and SparkSession") {
39-
// Creating a new session with config - this works by just calling the lazy val
40-
sparkContext
41-
assert(initialSession.sparkContext.conf.get("some-config") == "v2")
42-
assert(initialSession.conf.get("some-config") == "v2")
43-
SparkSession.clearDefaultSession()
43+
assert(session.sparkContext.conf.get("some-config") == "v2")
44+
assert(session.conf.get("some-config") == "v2")
4445
}
4546

4647
test("use global default session") {
47-
val session = SparkSession.builder().getOrCreate()
48+
val session = SparkSession.builder().master("local").getOrCreate()
4849
assert(SparkSession.builder().getOrCreate() == session)
49-
SparkSession.clearDefaultSession()
5050
}
5151

5252
test("config options are propagated to existing SparkSession") {
53-
val session1 = SparkSession.builder().config("spark-config1", "a").getOrCreate()
53+
val session1 = SparkSession.builder().master("local").config("spark-config1", "a").getOrCreate()
5454
assert(session1.conf.get("spark-config1") == "a")
5555
val session2 = SparkSession.builder().config("spark-config1", "b").getOrCreate()
5656
assert(session1 == session2)
5757
assert(session1.conf.get("spark-config1") == "b")
58-
SparkSession.clearDefaultSession()
5958
}
6059

6160
test("use session from active thread session and propagate config options") {
62-
val defaultSession = SparkSession.builder().getOrCreate()
61+
val defaultSession = SparkSession.builder().master("local").getOrCreate()
6362
val activeSession = defaultSession.newSession()
6463
SparkSession.setActiveSession(activeSession)
6564
val session = SparkSession.builder().config("spark-config2", "a").getOrCreate()
@@ -70,16 +69,14 @@ class SparkSessionBuilderSuite extends SparkFunSuite {
7069
SparkSession.clearActiveSession()
7170

7271
assert(SparkSession.builder().getOrCreate() == defaultSession)
73-
SparkSession.clearDefaultSession()
7472
}
7573

7674
test("create a new session if the default session has been stopped") {
77-
val defaultSession = SparkSession.builder().getOrCreate()
75+
val defaultSession = SparkSession.builder().master("local").getOrCreate()
7876
SparkSession.setDefaultSession(defaultSession)
7977
defaultSession.stop()
8078
val newSession = SparkSession.builder().master("local").getOrCreate()
8179
assert(newSession != defaultSession)
82-
newSession.stop()
8380
}
8481

8582
test("create a new session if the active thread session has been stopped") {
@@ -88,11 +85,9 @@ class SparkSessionBuilderSuite extends SparkFunSuite {
8885
activeSession.stop()
8986
val newSession = SparkSession.builder().master("local").getOrCreate()
9087
assert(newSession != activeSession)
91-
newSession.stop()
9288
}
9389

9490
test("create SparkContext first then SparkSession") {
95-
sparkContext.stop()
9691
val conf = new SparkConf().setAppName("test").setMaster("local").set("key1", "value1")
9792
val sparkContext2 = new SparkContext(conf)
9893
val session = SparkSession.builder().config("key2", "value2").getOrCreate()
@@ -101,14 +96,12 @@ class SparkSessionBuilderSuite extends SparkFunSuite {
10196
assert(session.sparkContext.conf.get("key1") == "value1")
10297
assert(session.sparkContext.conf.get("key2") == "value2")
10398
assert(session.sparkContext.conf.get("spark.app.name") == "test")
104-
session.stop()
10599
}
106100

107101
test("SPARK-15887: hive-site.xml should be loaded") {
108102
val session = SparkSession.builder().master("local").getOrCreate()
109103
assert(session.sessionState.newHadoopConf().get("hive.in.test") == "true")
110104
assert(session.sparkContext.hadoopConfiguration.get("hive.in.test") == "true")
111-
session.stop()
112105
}
113106

114107
test("SPARK-15991: Set global Hadoop conf") {
@@ -120,7 +113,6 @@ class SparkSessionBuilderSuite extends SparkFunSuite {
120113
assert(session.sessionState.newHadoopConf().get(mySpecialKey) == mySpecialValue)
121114
} finally {
122115
session.sparkContext.hadoopConfiguration.unset(mySpecialKey)
123-
session.stop()
124116
}
125117
}
126118
}

0 commit comments

Comments
 (0)