{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/22397137","id":22397137,"diff_hunk":"@@ -17,31 +17,65 @@\n \n package org.apache.spark.streaming.mqtt\n \n-import org.scalatest.FunSuite\n-\n+import org.scalatest.{BeforeAndAfter, FunSuite}\n+import org.scalatest.concurrent.Eventually\n+import scala.concurrent.duration._\n import org.apache.spark.streaming.{Seconds, StreamingContext}\n import org.apache.spark.storage.StorageLevel\n import org.apache.spark.streaming.dstream.ReceiverInputDStream\n+import org.eclipse.paho.client.mqttv3._\n+import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence\n \n-class MQTTStreamSuite extends FunSuite {\n-\n-  val batchDuration = Seconds(1)\n+class MQTTStreamSuite extends FunSuite with Eventually with BeforeAndAfter {\n \n+  private val batchDuration = Seconds(1)\n   private val master: String = \"local[2]\"\n-\n   private val framework: String = this.getClass.getSimpleName\n+  private val brokerUrl = \"tcp://localhost:1883\"\n+  private val topic = \"def\"\n+  private var ssc: StreamingContext = _\n \n-  test(\"mqtt input stream\") {\n-    val ssc = new StreamingContext(master, framework, batchDuration)\n-    val brokerUrl = \"abc\"\n-    val topic = \"def\"\n+  before {\n+    ssc = new StreamingContext(master, framework, batchDuration)\n+  }\n+  after {\n+    if (ssc != null) {\n+      ssc.stop()\n+      ssc = null\n+    }\n+  }\n \n-    // tests the API, does not actually test data receiving\n-    val test1: ReceiverInputDStream[String] = MQTTUtils.createStream(ssc, brokerUrl, topic)\n-    val test2: ReceiverInputDStream[String] =\n+  test(\"mqtt input stream\") {\n+    val sendMessage = \"MQTT demo for spark streaming\"\n+    publishData(sendMessage)\n+    val receiveStream: ReceiverInputDStream[String] =\n       MQTTUtils.createStream(ssc, brokerUrl, topic, StorageLevel.MEMORY_AND_DISK_SER_2)\n-\n-    // TODO: Actually test receiving data\n+    var receiveMessage: String = \"\"\n+    receiveStream.foreachRDD { rdd =>\n+      receiveMessage = rdd.first\n+      receiveMessage","path":"external/mqtt/src/test/scala/org/apache/spark/streaming/mqtt/MQTTStreamSuite.scala","position":55,"original_position":55,"commit_id":"fc8eb286db6aa8e78a567537996011f554eed969","original_commit_id":"fc8eb286db6aa8e78a567537996011f554eed969","user":{"login":"tdas","id":663212,"avatar_url":"https://avatars.githubusercontent.com/u/663212?v=3","gravatar_id":"","url":"https://api.github.com/users/tdas","html_url":"https://github.com/tdas","followers_url":"https://api.github.com/users/tdas/followers","following_url":"https://api.github.com/users/tdas/following{/other_user}","gists_url":"https://api.github.com/users/tdas/gists{/gist_id}","starred_url":"https://api.github.com/users/tdas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tdas/subscriptions","organizations_url":"https://api.github.com/users/tdas/orgs","repos_url":"https://api.github.com/users/tdas/repos","events_url":"https://api.github.com/users/tdas/events{/privacy}","received_events_url":"https://api.github.com/users/tdas/received_events","type":"User","site_admin":false},"body":"rdd.first can throw error if there are not records in the rdd. If by chance, the first batch in this program does not have the published data and returns an empty RDD, there are hidden exceptions made which is recipe for failures in the future - flaky tests, etc. As I said earlier, see the KafkaStreamSuite for inspiration. It does a RDD.collect() and puts all the received data into a hashmap (could be a simple buffer/list in your case). Then the eventually verifies the data in the buffer.","created_at":"2015-01-01T00:45:08Z","updated_at":"2015-01-01T00:45:08Z","html_url":"https://github.com/apache/spark/pull/3844#discussion_r22397137","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/3844","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/22397137"},"html":{"href":"https://github.com/apache/spark/pull/3844#discussion_r22397137"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/3844"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/3844","id":26684823,"html_url":"https://github.com/apache/spark/pull/3844","diff_url":"https://github.com/apache/spark/pull/3844.diff","patch_url":"https://github.com/apache/spark/pull/3844.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/3844","number":3844,"state":"open","locked":false,"title":"[SPARK-4631] unit test for MQTT","user":{"login":"Bilna","id":7123586,"avatar_url":"https://avatars.githubusercontent.com/u/7123586?v=3","gravatar_id":"","url":"https://api.github.com/users/Bilna","html_url":"https://github.com/Bilna","followers_url":"https://api.github.com/users/Bilna/followers","following_url":"https://api.github.com/users/Bilna/following{/other_user}","gists_url":"https://api.github.com/users/Bilna/gists{/gist_id}","starred_url":"https://api.github.com/users/Bilna/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bilna/subscriptions","organizations_url":"https://api.github.com/users/Bilna/orgs","repos_url":"https://api.github.com/users/Bilna/repos","events_url":"https://api.github.com/users/Bilna/events{/privacy}","received_events_url":"https://api.github.com/users/Bilna/received_events","type":"User","site_admin":false},"body":"Please review the unit test for MQTT","created_at":"2014-12-30T13:12:33Z","updated_at":"2015-01-01T00:45:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0cf0a03b8eb4f52bd66ddc6b33c6d5e451775be8","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/3844/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/3844/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments/{number}","comments_url":"https://api.github.com/repos/apache/spark/issues/3844/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/fc8eb286db6aa8e78a567537996011f554eed969","head":{"label":"Bilna:master","ref":"master","sha":"fc8eb286db6aa8e78a567537996011f554eed969","user":{"login":"Bilna","id":7123586,"avatar_url":"https://avatars.githubusercontent.com/u/7123586?v=3","gravatar_id":"","url":"https://api.github.com/users/Bilna","html_url":"https://github.com/Bilna","followers_url":"https://api.github.com/users/Bilna/followers","following_url":"https://api.github.com/users/Bilna/following{/other_user}","gists_url":"https://api.github.com/users/Bilna/gists{/gist_id}","starred_url":"https://api.github.com/users/Bilna/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bilna/subscriptions","organizations_url":"https://api.github.com/users/Bilna/orgs","repos_url":"https://api.github.com/users/Bilna/repos","events_url":"https://api.github.com/users/Bilna/events{/privacy}","received_events_url":"https://api.github.com/users/Bilna/received_events","type":"User","site_admin":false},"repo":{"id":28630516,"name":"spark","full_name":"Bilna/spark","owner":{"login":"Bilna","id":7123586,"avatar_url":"https://avatars.githubusercontent.com/u/7123586?v=3","gravatar_id":"","url":"https://api.github.com/users/Bilna","html_url":"https://github.com/Bilna","followers_url":"https://api.github.com/users/Bilna/followers","following_url":"https://api.github.com/users/Bilna/following{/other_user}","gists_url":"https://api.github.com/users/Bilna/gists{/gist_id}","starred_url":"https://api.github.com/users/Bilna/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bilna/subscriptions","organizations_url":"https://api.github.com/users/Bilna/orgs","repos_url":"https://api.github.com/users/Bilna/repos","events_url":"https://api.github.com/users/Bilna/events{/privacy}","received_events_url":"https://api.github.com/users/Bilna/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Bilna/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/Bilna/spark","forks_url":"https://api.github.com/repos/Bilna/spark/forks","keys_url":"https://api.github.com/repos/Bilna/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Bilna/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Bilna/spark/teams","hooks_url":"https://api.github.com/repos/Bilna/spark/hooks","issue_events_url":"https://api.github.com/repos/Bilna/spark/issues/events{/number}","events_url":"https://api.github.com/repos/Bilna/spark/events","assignees_url":"https://api.github.com/repos/Bilna/spark/assignees{/user}","branches_url":"https://api.github.com/repos/Bilna/spark/branches{/branch}","tags_url":"https://api.github.com/repos/Bilna/spark/tags","blobs_url":"https://api.github.com/repos/Bilna/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Bilna/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Bilna/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/Bilna/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Bilna/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/Bilna/spark/languages","stargazers_url":"https://api.github.com/repos/Bilna/spark/stargazers","contributors_url":"https://api.github.com/repos/Bilna/spark/contributors","subscribers_url":"https://api.github.com/repos/Bilna/spark/subscribers","subscription_url":"https://api.github.com/repos/Bilna/spark/subscription","commits_url":"https://api.github.com/repos/Bilna/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/Bilna/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/Bilna/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/Bilna/spark/issues/comments/{number}","contents_url":"https://api.github.com/repos/Bilna/spark/contents/{+path}","compare_url":"https://api.github.com/repos/Bilna/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Bilna/spark/merges","archive_url":"https://api.github.com/repos/Bilna/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Bilna/spark/downloads","issues_url":"https://api.github.com/repos/Bilna/spark/issues{/number}","pulls_url":"https://api.github.com/repos/Bilna/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/Bilna/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/Bilna/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Bilna/spark/labels{/name}","releases_url":"https://api.github.com/repos/Bilna/spark/releases{/id}","created_at":"2014-12-30T12:47:01Z","updated_at":"2014-12-31T09:52:36Z","pushed_at":"2014-12-31T09:52:35Z","git_url":"git://github.com/Bilna/spark.git","ssh_url":"git@github.com:Bilna/spark.git","clone_url":"https://github.com/Bilna/spark.git","svn_url":"https://github.com/Bilna/spark","homepage":null,"size":87823,"stargazers_count":1,"watchers_count":1,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"352ed6bbe3c3b67e52e298e7c535ae414d96beca","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments/{number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-01-01T00:03:04Z","pushed_at":"2015-01-01T00:03:53Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1083068,"stargazers_count":2458,"watchers_count":2458,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2179,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":269,"forks":2179,"open_issues":269,"watchers":2458,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/3844"},"html":{"href":"https://github.com/apache/spark/pull/3844"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/3844"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/3844/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/3844/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/3844/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/fc8eb286db6aa8e78a567537996011f554eed969"}}}}