@@ -22,6 +22,7 @@ import java.util.concurrent.{CountDownLatch, TimeUnit}
2222
2323import scala .language .postfixOps
2424
25+ import com .google .common .base .Charsets .UTF_8
2526import org .apache .activemq .broker .{BrokerService , TransportConnector }
2627import org .apache .commons .lang3 .RandomUtils
2728import org .eclipse .paho .client .mqttv3 ._
@@ -46,6 +47,8 @@ private class MQTTTestUtils extends Logging {
4647 private var broker : BrokerService = _
4748 private var connector : TransportConnector = _
4849
50+ private var receiverStartedLatch = new CountDownLatch (1 )
51+
4952 def brokerUri : String = {
5053 s " $brokerHost: $brokerPort"
5154 }
@@ -69,6 +72,8 @@ private class MQTTTestUtils extends Logging {
6972 connector.stop()
7073 connector = null
7174 }
75+ Utils .deleteRecursively(persistenceDir)
76+ receiverStartedLatch = null
7277 }
7378
7479 private def findFreePort (): Int = {
@@ -88,7 +93,7 @@ private class MQTTTestUtils extends Logging {
8893 client.connect()
8994 if (client.isConnected) {
9095 val msgTopic = client.getTopic(topic)
91- val message = new MqttMessage (data.getBytes(" utf-8 " ))
96+ val message = new MqttMessage (data.getBytes(UTF_8 ))
9297 message.setQos(1 )
9398 message.setRetained(true )
9499
@@ -110,27 +115,37 @@ private class MQTTTestUtils extends Logging {
110115 }
111116
112117 /**
113- * Block until at least one receiver has started or timeout occurs.
118+ * Call this one before starting StreamingContext so that we won't miss the
119+ * StreamingListenerReceiverStarted event.
114120 */
115- def waitForReceiverToStart (ssc : StreamingContext ) : Unit = {
116- val latch = new CountDownLatch (1 )
121+ def registerStreamingListener (jssc : JavaStreamingContext ): Unit = {
122+ registerStreamingListener(jssc.ssc)
123+ }
124+
125+ /**
126+ * Call this one before starting StreamingContext so that we won't miss the
127+ * StreamingListenerReceiverStarted event.
128+ */
129+ def registerStreamingListener (ssc : StreamingContext ): Unit = {
117130 ssc.addStreamingListener(new StreamingListener {
118131 override def onReceiverStarted (receiverStarted : StreamingListenerReceiverStarted ) {
119- latch .countDown()
132+ receiverStartedLatch .countDown()
120133 }
121134 })
122-
123- assert(latch.await(10 , TimeUnit .SECONDS ), " Timeout waiting for receiver to start." )
124135 }
125136
126- def waitForReceiverToStart (jssc : JavaStreamingContext ) : Unit = {
127- val latch = new CountDownLatch (1 )
128- jssc.addStreamingListener(new StreamingListener {
129- override def onReceiverStarted (receiverStarted : StreamingListenerReceiverStarted ) {
130- latch.countDown()
131- }
132- })
137+ /**
138+ * Block until at least one receiver has started or timeout occurs.
139+ */
140+ def waitForReceiverToStart (jssc : JavaStreamingContext ): Unit = {
141+ waitForReceiverToStart(jssc.ssc)
142+ }
133143
134- assert(latch.await(10 , TimeUnit .SECONDS ), " Timeout waiting for receiver to start." )
144+ /**
145+ * Block until at least one receiver has started or timeout occurs.
146+ */
147+ def waitForReceiverToStart (ssc : StreamingContext ): Unit = {
148+ assert(
149+ receiverStartedLatch.await(10 , TimeUnit .SECONDS ), " Timeout waiting for receiver to start." )
135150 }
136151}
0 commit comments