@@ -36,11 +36,25 @@ import scala.xml.Node
3636
3737class UISuite extends FunSuite {
3838
39+ /**
40+ * Create a test SparkContext with the SparkUI enabled.
41+ * It is safe to `get` the SparkUI directly from the SparkContext returned here.
42+ */
43+ private def newSparkContext (): SparkContext = {
44+ val conf = new SparkConf ()
45+ .setMaster(" local" )
46+ .setAppName(" test" )
47+ .set(" spark.ui.enabled" , " true" )
48+ val sc = new SparkContext (conf)
49+ assert(sc.ui.isDefined)
50+ sc
51+ }
52+
3953 ignore(" basic ui visibility" ) {
40- withSpark(new SparkContext ( " local " , " test " )) { sc =>
54+ withSpark(newSparkContext( )) { sc =>
4155 // test if the ui is visible, and all the expected tabs are visible
4256 eventually(timeout(10 seconds), interval(50 milliseconds)) {
43- val html = Source .fromURL(sc.ui.appUIAddress).mkString
57+ val html = Source .fromURL(sc.ui.get. appUIAddress).mkString
4458 assert(! html.contains(" random data that should not be present" ))
4559 assert(html.toLowerCase.contains(" stages" ))
4660 assert(html.toLowerCase.contains(" storage" ))
@@ -51,7 +65,7 @@ class UISuite extends FunSuite {
5165 }
5266
5367 ignore(" visibility at localhost:4040" ) {
54- withSpark(new SparkContext ( " local " , " test " )) { sc =>
68+ withSpark(newSparkContext( )) { sc =>
5569 // test if visible from http://localhost:4040
5670 eventually(timeout(10 seconds), interval(50 milliseconds)) {
5771 val html = Source .fromURL(" http://localhost:4040" ).mkString
@@ -61,8 +75,8 @@ class UISuite extends FunSuite {
6175 }
6276
6377 ignore(" attaching a new tab" ) {
64- withSpark(new SparkContext ( " local " , " test " )) { sc =>
65- val sparkUI = sc.ui
78+ withSpark(newSparkContext( )) { sc =>
79+ val sparkUI = sc.ui.get
6680
6781 val newTab = new WebUITab (sparkUI, " foo" ) {
6882 attachPage(new WebUIPage (" " ) {
@@ -73,7 +87,7 @@ class UISuite extends FunSuite {
7387 }
7488 sparkUI.attachTab(newTab)
7589 eventually(timeout(10 seconds), interval(50 milliseconds)) {
76- val html = Source .fromURL(sc.ui .appUIAddress).mkString
90+ val html = Source .fromURL(sparkUI .appUIAddress).mkString
7791 assert(! html.contains(" random data that should not be present" ))
7892
7993 // check whether new page exists
@@ -87,7 +101,7 @@ class UISuite extends FunSuite {
87101 }
88102
89103 eventually(timeout(10 seconds), interval(50 milliseconds)) {
90- val html = Source .fromURL(sc.ui .appUIAddress.stripSuffix(" /" ) + " /foo" ).mkString
104+ val html = Source .fromURL(sparkUI .appUIAddress.stripSuffix(" /" ) + " /foo" ).mkString
91105 // check whether new page exists
92106 assert(html.contains(" magic" ))
93107 }
@@ -129,16 +143,20 @@ class UISuite extends FunSuite {
129143 }
130144
131145 test(" verify appUIAddress contains the scheme" ) {
132- withSpark(new SparkContext (" local" , " test" )) { sc =>
133- val uiAddress = sc.ui.appUIAddress
134- assert(uiAddress.equals(" http://" + sc.ui.appUIHostPort))
146+ withSpark(newSparkContext()) { sc =>
147+ val ui = sc.ui.get
148+ val uiAddress = ui.appUIAddress
149+ val uiHostPort = ui.appUIHostPort
150+ assert(uiAddress.equals(" http://" + uiHostPort))
135151 }
136152 }
137153
138154 test(" verify appUIAddress contains the port" ) {
139- withSpark(new SparkContext (" local" , " test" )) { sc =>
140- val splitUIAddress = sc.ui.appUIAddress.split(':' )
141- assert(splitUIAddress(2 ).toInt == sc.ui.boundPort)
155+ withSpark(newSparkContext()) { sc =>
156+ val ui = sc.ui.get
157+ val splitUIAddress = ui.appUIAddress.split(':' )
158+ val boundPort = ui.boundPort
159+ assert(splitUIAddress(2 ).toInt == boundPort)
142160 }
143161 }
144162}
0 commit comments