Github user JoshRosen commented on a diff in the pull request:
https://github.com/apache/spark/pull/3801#discussion_r22449238
--- Diff:
streaming/src/test/scala/org/apache/spark/streaming/TestSuiteBase.scala ---
@@ -104,6 +106,77 @@ class TestOutputStreamWithPartitions[T:
ClassTag](parent: DStream[T],
}
/**
+ * This is an interface that can be used to block until certain events
occur, such as
+ * the start/completion of batches. This is much less brittle than
waiting on wall-clock time.
+ * Internally, this is implemented using a StreamingListener.
Constructing a new instance of this
+ * class automatically registers a StreamingListener on the given
StreamingContext.
+ */
+class StreamingTestWaiter(ssc: StreamingContext) {
+
+ // All access to this state should be guarded by
`StreamingTestWaiter.this.synchronized`
+ private var numCompletedBatches = 0
+ private var numStartedBatches = 0
+
+ private val listener = new StreamingListener {
+ override def onBatchStarted(batchStarted:
StreamingListenerBatchStarted): Unit =
+ StreamingTestWaiter.this.synchronized {
+ numStartedBatches += 1
+ StreamingTestWaiter.this.notifyAll()
+ }
+ override def onBatchCompleted(batchCompleted:
StreamingListenerBatchCompleted): Unit =
+ StreamingTestWaiter.this.synchronized {
+ numCompletedBatches += 1
+ StreamingTestWaiter.this.notifyAll()
+ }
+ }
+ ssc.addStreamingListener(listener)
+
+ def getNumCompletedBatches: Int = this.synchronized {
+ numCompletedBatches
+ }
+
+ def getNumStartedBatches: Int = this.synchronized {
+ numStartedBatches
+ }
+
+ /**
+ * Block until the number of completed batches reaches the given
threshold.
+ */
+ def waitForTotalBatchesCompleted(
--- End diff --
Actually, something even simpler (since this is just for test code): I can
just copy ScalaTest's `eventually` and modify it to use synchronize / notify
instead of polling.
---
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]