-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-4631] unit test for MQTT #3844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8616495
[SPARK-4631] unit test for MQTT
5ca6691
Update MQTTStreamSuite.scala
Bilna e8b6623
Update MQTTStreamSuite.scala
Bilna 5f6bfd2
Added BeforeAndAfter
b1ac4ad
Added BeforeAndAfter
4b58094
Update MQTTStreamSuite.scala
Bilna fc8eb28
Merge remote-tracking branch 'upstream/master'
89d804e
Merge remote-tracking branch 'upstream/master'
04503cf
Added embedded broker service for mqtt test
4b34ee7
Update MQTTStreamSuite.scala
Bilna ed9db4c
Merge remote-tracking branch 'upstream/master'
fac3904
Correction in Indentation and coding style
28681fa
Merge remote-tracking branch 'upstream/master'
acea3a3
Adding dependency with scope test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,31 +17,111 @@ | |
|
|
||
| package org.apache.spark.streaming.mqtt | ||
|
|
||
| import org.scalatest.FunSuite | ||
| import java.net.{URI, ServerSocket} | ||
|
|
||
| import org.apache.spark.streaming.{Seconds, StreamingContext} | ||
| import org.apache.activemq.broker.{TransportConnector, BrokerService} | ||
| import org.apache.spark.util.Utils | ||
| import org.scalatest.{BeforeAndAfter, FunSuite} | ||
| import org.scalatest.concurrent.Eventually | ||
| import scala.concurrent.duration._ | ||
| import org.apache.spark.streaming.{Milliseconds, StreamingContext} | ||
| import org.apache.spark.storage.StorageLevel | ||
| import org.apache.spark.streaming.dstream.ReceiverInputDStream | ||
| import org.eclipse.paho.client.mqttv3._ | ||
| import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence | ||
|
|
||
| class MQTTStreamSuite extends FunSuite { | ||
|
|
||
| val batchDuration = Seconds(1) | ||
| class MQTTStreamSuite extends FunSuite with Eventually with BeforeAndAfter { | ||
|
|
||
| private val batchDuration = Milliseconds(500) | ||
| private val master: String = "local[2]" | ||
|
|
||
| private val framework: String = this.getClass.getSimpleName | ||
| private val freePort = findFreePort() | ||
| private val brokerUri = "//localhost:" + freePort | ||
| private val topic = "def" | ||
| private var ssc: StreamingContext = _ | ||
| private val persistenceDir = Utils.createTempDir() | ||
| private var broker: BrokerService = _ | ||
| private var connector: TransportConnector = _ | ||
|
|
||
| test("mqtt input stream") { | ||
| val ssc = new StreamingContext(master, framework, batchDuration) | ||
| val brokerUrl = "abc" | ||
| val topic = "def" | ||
| before { | ||
| ssc = new StreamingContext(master, framework, batchDuration) | ||
| setupMQTT() | ||
| } | ||
|
|
||
| // tests the API, does not actually test data receiving | ||
| val test1: ReceiverInputDStream[String] = MQTTUtils.createStream(ssc, brokerUrl, topic) | ||
| val test2: ReceiverInputDStream[String] = | ||
| MQTTUtils.createStream(ssc, brokerUrl, topic, StorageLevel.MEMORY_AND_DISK_SER_2) | ||
| after { | ||
| if (ssc != null) { | ||
| ssc.stop() | ||
| ssc = null | ||
| } | ||
| Utils.deleteRecursively(persistenceDir) | ||
| tearDownMQTT() | ||
| } | ||
|
|
||
| // TODO: Actually test receiving data | ||
| test("mqtt input stream") { | ||
| val sendMessage = "MQTT demo for spark streaming" | ||
| val receiveStream: ReceiverInputDStream[String] = | ||
| MQTTUtils.createStream(ssc, "tcp:" + brokerUri, topic, StorageLevel.MEMORY_ONLY) | ||
| var receiveMessage: List[String] = List() | ||
| receiveStream.foreachRDD { rdd => | ||
| if (rdd.collect.length > 0) { | ||
| receiveMessage = receiveMessage ::: List(rdd.first) | ||
| receiveMessage | ||
| } | ||
| } | ||
| ssc.start() | ||
| publishData(sendMessage) | ||
| eventually(timeout(10000 milliseconds), interval(100 milliseconds)) { | ||
| assert(sendMessage.equals(receiveMessage(0))) | ||
| } | ||
| ssc.stop() | ||
| } | ||
|
|
||
| private def setupMQTT() { | ||
| broker = new BrokerService() | ||
| connector = new TransportConnector() | ||
| connector.setName("mqtt") | ||
| connector.setUri(new URI("mqtt:" + brokerUri)) | ||
| broker.addConnector(connector) | ||
| broker.start() | ||
| } | ||
|
|
||
| private def tearDownMQTT() { | ||
| if (broker != null) { | ||
| broker.stop() | ||
| broker = null | ||
| } | ||
| if (connector != null) { | ||
| connector.stop() | ||
| connector = null | ||
| } | ||
| } | ||
|
|
||
| private def findFreePort(): Int = { | ||
| Utils.startServiceOnPort(23456, (trialPort: Int) => { | ||
| val socket = new ServerSocket(trialPort) | ||
| socket.close() | ||
| (null, trialPort) | ||
| })._2 | ||
| } | ||
|
|
||
| def publishData(data: String): Unit = { | ||
| var client: MqttClient = null | ||
| try { | ||
| val persistence: MqttClientPersistence = new MqttDefaultFilePersistence(persistenceDir.getAbsolutePath) | ||
| client = new MqttClient("tcp:" + brokerUri, MqttClient.generateClientId(), persistence) | ||
| client.connect() | ||
| if (client.isConnected) { | ||
| val msgTopic: MqttTopic = client.getTopic(topic) | ||
| val message: MqttMessage = new MqttMessage(data.getBytes("utf-8")) | ||
| message.setQos(1) | ||
| message.setRetained(true) | ||
| for (i <- 0 to 100) | ||
| msgTopic.publish(message) | ||
| } | ||
| } finally { | ||
| client.disconnect() | ||
| client.close() | ||
| client = null | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldnt there be a |
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I missed this in the last pass, but this violates the Scala syntax that we follow. I wont block this PR for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain what is the correction here. Just to understand what went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such code block should either be in one line or be within braces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.. thanks.