Skip to content
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package org.apache.spark.sql

import org.scalatest.BeforeAndAfterEach

import org.apache.spark.{SparkConf, SparkContext, SparkFunSuite}
import org.apache.spark.sql.internal.SQLConf

/**
* Test cases for the builder pattern of [[SparkSession]].
*/
class SparkSessionBuilderSuite extends SparkFunSuite {
class SparkSessionBuilderSuite extends SparkFunSuite with BeforeAndAfterEach {

private var initialSession: SparkSession = _

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this is only used in the first test case? shall we just move it to the first test case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also used in sparkContext and sparkContext is used in 3 test cases.

private lazy val sparkContext: SparkContext = {
    initialSession = SparkSession.builder()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. The other two reference are just stop. I will move this.


Expand All @@ -36,18 +38,22 @@ class SparkSessionBuilderSuite extends SparkFunSuite {
initialSession.sparkContext
}

override def afterEach(): Unit = {
// This suite should not interfere with the other test suites.
SparkSession.clearDefaultSession()
SparkSession.clearActiveSession()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we also stop the session?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the unit test case because SparkContext requires spark.master. I'll update like that.

    if (!_conf.contains("spark.master")) {
      throw new SparkException("A master URL must be set in your configuration")
    }

}

test("create with config options and propagate them to SparkContext and SparkSession") {
// Creating a new session with config - this works by just calling the lazy val
sparkContext
assert(initialSession.sparkContext.conf.get("some-config") == "v2")
assert(initialSession.conf.get("some-config") == "v2")
SparkSession.clearDefaultSession()
}

test("use global default session") {
val session = SparkSession.builder().getOrCreate()
assert(SparkSession.builder().getOrCreate() == session)
SparkSession.clearDefaultSession()
}

test("config options are propagated to existing SparkSession") {
Expand All @@ -56,7 +62,6 @@ class SparkSessionBuilderSuite extends SparkFunSuite {
val session2 = SparkSession.builder().config("spark-config1", "b").getOrCreate()
assert(session1 == session2)
assert(session1.conf.get("spark-config1") == "b")
SparkSession.clearDefaultSession()
}

test("use session from active thread session and propagate config options") {
Expand All @@ -73,7 +78,6 @@ class SparkSessionBuilderSuite extends SparkFunSuite {
SparkSession.clearActiveSession()

assert(SparkSession.builder().getOrCreate() == defaultSession)
SparkSession.clearDefaultSession()
}

test("create a new session if the default session has been stopped") {
Expand Down