Github user zsxwing commented on a diff in the pull request:
https://github.com/apache/spark/pull/6070#discussion_r30104787
--- Diff:
streaming/src/test/scala/org/apache/spark/streaming/StreamingContextSuite.scala
---
@@ -493,6 +494,65 @@ class StreamingContextSuite extends FunSuite with
BeforeAndAfter with Timeouts w
}
}
+ test("getActiveOrCreate and getActiveContext") {
+ require(StreamingContext.getActive().isEmpty, "context exists from
before")
+ sc = new SparkContext(conf)
+
+ var newContextCreated = false
+
+ def creatingFunc(): StreamingContext = {
+ newContextCreated = true
+ val newSsc = new StreamingContext(sc, batchDuration)
+ val input = addInputStream(ssc)
+ input.foreachRDD { rdd => rdd.count }
+ newSsc
+ }
+
+ def testGetActiveOrCreate(body: => Unit): Unit = {
+ newContextCreated = false
+ try {
+ body
+ } finally {
+ if (ssc != null) {
+ ssc.stop(stopSparkContext = false)
+ }
+ ssc = null
+ }
+ }
+
+ // getOrCreate should create new context and getActiveContext should
return it only
+ // after starting the context
+ testGetActiveOrCreate {
+ ssc = StreamingContext.getActiveOrCreate(creatingFunc _)
+ assert(newContextCreated === true, "new context not created")
+ assert(StreamingContext.getActive().isEmpty,
+ "new initialized context returned before starting")
+ ssc.start()
+ assert(StreamingContext.getActive().nonEmpty,
+ "active context not returned")
+ assert(StreamingContext.getActiveOrCreate(creatingFunc _) === ssc,
+ "active context not returned")
+ ssc.stop()
+ assert(StreamingContext.getActive().nonEmpty,
--- End diff --
nit: `nonEmpty` -> `isEmpty`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]