@@ -21,10 +21,11 @@ import java.io.{ObjectInputStream, IOException}
2121
2222import scala .collection .mutable .ArrayBuffer
2323import scala .collection .mutable .SynchronizedBuffer
24- import scala .concurrent . TimeoutException
24+ import scala .language . implicitConversions
2525import scala .reflect .ClassTag
2626
2727import org .scalatest .{BeforeAndAfter , FunSuite }
28+ import org .scalatest .time .{Span , Milliseconds => ScalaTestMilliseconds }
2829
2930import org .apache .spark .streaming .dstream .{DStream , InputDStream , ForEachDStream }
3031import org .apache .spark .streaming .scheduler .{StreamingListenerBatchStarted , StreamingListenerBatchCompleted , StreamingListener }
@@ -138,42 +139,6 @@ class StreamingTestWaiter(ssc: StreamingContext) {
138139 def getNumStartedBatches : Int = this .synchronized {
139140 numStartedBatches
140141 }
141-
142- /**
143- * Block until the number of completed batches reaches the given threshold.
144- */
145- def waitForTotalBatchesCompleted (
146- targetNumBatches : Int ,
147- timeout : Duration ): Unit = this .synchronized {
148- val startTime = System .currentTimeMillis()
149- def successful = getNumCompletedBatches >= targetNumBatches
150- def timedOut = (System .currentTimeMillis() - startTime) >= timeout.milliseconds
151- while (! timedOut && ! successful) {
152- this .wait(timeout.milliseconds)
153- }
154- if (! successful && timedOut) {
155- throw new TimeoutException (s " Waited for $targetNumBatches completed batches, but only " +
156- s " $numCompletedBatches have completed after $timeout" )
157- }
158- }
159-
160- /**
161- * Block until the number of started batches reaches the given threshold.
162- */
163- def waitForTotalBatchesStarted (
164- targetNumBatches : Int ,
165- timeout : Duration ): Unit = this .synchronized {
166- val startTime = System .currentTimeMillis()
167- def successful = getNumStartedBatches >= targetNumBatches
168- def timedOut = (System .currentTimeMillis() - startTime) >= timeout.milliseconds
169- while (! timedOut && ! successful) {
170- this .wait(timeout.milliseconds)
171- }
172- if (! successful && timedOut) {
173- throw new TimeoutException (s " Waited for $targetNumBatches started batches, but only " +
174- s " $numStartedBatches have started after $timeout" )
175- }
176- }
177142}
178143
179144/**
@@ -236,6 +201,14 @@ trait TestSuiteBase extends FunSuite with BeforeAndAfter with Logging {
236201 before(beforeFunction)
237202 after(afterFunction)
238203
204+ /**
205+ * Implicit conversion which allows streaming Durations to be used with ScalaTest methods,
206+ * such as `eventually`.
207+ */
208+ implicit def streamingDurationToScalatestSpan (duration : Duration ): Span = {
209+ Span (duration.milliseconds, ScalaTestMilliseconds )
210+ }
211+
239212 /**
240213 * Run a block of code with the given StreamingContext and automatically
241214 * stop the context when the block completes or when an exception is thrown.
0 commit comments