Skip to content

Commit 526896f

Browse files
committed
Beef up test cases.
1 parent c665771 commit 526896f

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

sql/core/src/main/scala/org/apache/spark/sql/SparkSession.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ object SparkSession {
646646

647647
/**
648648
* Sets a name for the application, which will be shown in the Spark web UI.
649+
* If no application name is set, a randomly generated name will be used.
649650
*
650651
* @since 2.0.0
651652
*/
@@ -658,7 +659,7 @@ object SparkSession {
658659
* @since 2.0.0
659660
*/
660661
def config(key: String, value: String): Builder = synchronized {
661-
options += key -> value
662+
options += (key -> value)
662663
this
663664
}
664665

@@ -738,6 +739,9 @@ object SparkSession {
738739
* and if yes, return that one. If no valid global default SparkSession exists, the method
739740
* creates a new SparkSession and assigns the newly created SparkSession as the global default.
740741
*
742+
* In case an existing SparkSession is returned, the config options specified in this builder
743+
* will be applied to the existing SparkSession.
744+
*
741745
* @since 2.0.0
742746
*/
743747
def getOrCreate(): SparkSession = synchronized {
@@ -759,11 +763,17 @@ object SparkSession {
759763

760764
// No active nor global default session. Create a new one.
761765
val sparkContext = userSuppliedContext.getOrElse {
766+
// set app name if not given
767+
if (!options.contains("spark.app.name")) {
768+
options += "spark.app.name" -> java.util.UUID.randomUUID().toString
769+
}
770+
762771
val sparkConf = new SparkConf()
763772
options.foreach { case (k, v) => sparkConf.set(k, v) }
764773
SparkContext.getOrCreate(sparkConf)
765774
}
766775
session = new SparkSession(sparkContext)
776+
options.foreach { case (k, v) => session.conf.set(k, v) }
767777
defaultSession.set(session)
768778

769779
// Register a successfully instantiated context to the singleton. This should be at the

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,77 @@
1717

1818
package org.apache.spark.sql
1919

20+
import org.apache.spark.{SparkContext, SparkFunSuite}
21+
2022
/**
2123
* Test cases for the builder pattern of [[SparkSession]].
2224
*/
23-
class SparkSessionBuilderSuite {
25+
class SparkSessionBuilderSuite extends SparkFunSuite {
26+
27+
private var initialSession: SparkSession = _
28+
29+
private lazy val sparkContext: SparkContext = {
30+
initialSession = SparkSession.builder()
31+
.master("local")
32+
.config("spark.ui.enabled", value = false)
33+
.config("some-config", "v2")
34+
.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()
44+
}
45+
46+
test("use global default session") {
47+
val session = SparkSession.builder().getOrCreate()
48+
assert(SparkSession.builder().getOrCreate() == session)
49+
SparkSession.clearDefaultSession()
50+
}
51+
52+
test("config options are propagated to existing SparkSession") {
53+
val session1 = SparkSession.builder().config("spark-config1", "a").getOrCreate()
54+
assert(session1.conf.get("spark-config1") == "a")
55+
val session2 = SparkSession.builder().config("spark-config1", "b").getOrCreate()
56+
assert(session1 == session2)
57+
assert(session1.conf.get("spark-config1") == "b")
58+
SparkSession.clearDefaultSession()
59+
}
60+
61+
test("use session from active thread session and propagate config options") {
62+
val defaultSession = SparkSession.builder().getOrCreate()
63+
val activeSession = defaultSession.newSession()
64+
SparkSession.setActiveSession(activeSession)
65+
val session = SparkSession.builder().config("spark-config2", "a").getOrCreate()
66+
67+
assert(activeSession != defaultSession)
68+
assert(session == activeSession)
69+
assert(session.conf.get("spark-config2") == "a")
70+
SparkSession.clearActiveSession()
71+
72+
assert(SparkSession.builder().getOrCreate() == defaultSession)
73+
SparkSession.clearDefaultSession()
74+
}
75+
76+
test("create a new session if the default session has been stopped") {
77+
val defaultSession = SparkSession.builder().getOrCreate()
78+
SparkSession.setDefaultSession(defaultSession)
79+
defaultSession.stop()
80+
val newSession = SparkSession.builder().master("local").getOrCreate()
81+
assert(newSession != defaultSession)
82+
newSession.stop()
83+
}
2484

85+
test("create a new session if the active thread session has been stopped") {
86+
val activeSession = SparkSession.builder().master("local").getOrCreate()
87+
SparkSession.setActiveSession(activeSession)
88+
activeSession.stop()
89+
val newSession = SparkSession.builder().master("local").getOrCreate()
90+
assert(newSession != activeSession)
91+
newSession.stop()
92+
}
2593
}

0 commit comments

Comments
 (0)