PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/PerSubscriptionMessageHandlerTest.java

http://github.com/eclipse/paho.mqtt.java
Java | 369 lines | 215 code | 73 blank | 81 comment | 7 complexity | b567e3f2c00149bf21a63c25b768559f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*******************************************************************************
  2. * Copyright (c) 2015 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0
  10. * and the Eclipse Distribution License is available at
  11. * https://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. *******************************************************************************/
  16. package org.eclipse.paho.client.mqttv3.test;
  17. import java.net.URI;
  18. import java.util.ArrayList;
  19. import java.util.UUID;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import org.junit.AfterClass;
  23. import org.junit.Assert;
  24. import org.junit.BeforeClass;
  25. import org.junit.Test;
  26. import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
  27. import org.eclipse.paho.client.mqttv3.IMqttClient;
  28. import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
  29. import org.eclipse.paho.client.mqttv3.IMqttToken;
  30. import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
  31. import org.eclipse.paho.client.mqttv3.MqttClient;
  32. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  33. import org.eclipse.paho.client.mqttv3.MqttMessage;
  34. import org.eclipse.paho.client.mqttv3.test.client.MqttClientFactoryPaho;
  35. import org.eclipse.paho.client.mqttv3.test.logging.LoggingUtilities;
  36. import org.eclipse.paho.client.mqttv3.test.properties.TestProperties;
  37. import org.eclipse.paho.client.mqttv3.test.utilities.Utility;
  38. public class PerSubscriptionMessageHandlerTest {
  39. static final Class<?> cclass = PerSubscriptionMessageHandlerTest.class;
  40. static final String className = cclass.getName();
  41. static final Logger log = Logger.getLogger(className);
  42. private static URI serverURI;
  43. private static MqttClientFactoryPaho clientFactory;
  44. private static String topicPrefix;
  45. /**
  46. * @throws Exception
  47. */
  48. @BeforeClass
  49. public static void setUpBeforeClass() throws Exception {
  50. try {
  51. String methodName = Utility.getMethodName();
  52. LoggingUtilities.banner(log, cclass, methodName);
  53. serverURI = TestProperties.getServerURI();
  54. clientFactory = new MqttClientFactoryPaho();
  55. clientFactory.open();
  56. topicPrefix = "PerSubscriptionMessageHandlerTest-" + UUID.randomUUID().toString() + "-";
  57. }
  58. catch (Exception exception) {
  59. log.log(Level.SEVERE, "caught exception:", exception);
  60. throw exception;
  61. }
  62. }
  63. /**
  64. * @throws Exception
  65. */
  66. @AfterClass
  67. public static void tearDownAfterClass() throws Exception {
  68. String methodName = Utility.getMethodName();
  69. LoggingUtilities.banner(log, cclass, methodName);
  70. try {
  71. if (clientFactory != null) {
  72. clientFactory.close();
  73. clientFactory.disconnect();
  74. }
  75. }
  76. catch (Exception exception) {
  77. log.log(Level.SEVERE, "caught exception:", exception);
  78. }
  79. }
  80. class listener implements IMqttMessageListener {
  81. final ArrayList<MqttMessage> messages;
  82. public listener() {
  83. messages = new ArrayList<MqttMessage>();
  84. }
  85. public MqttMessage getNextMessage() {
  86. synchronized (messages) {
  87. if (messages.size() == 0) {
  88. try {
  89. messages.wait(1000);
  90. }
  91. catch (InterruptedException e) {
  92. // empty
  93. }
  94. }
  95. if (messages.size() == 0) {
  96. return null;
  97. }
  98. return messages.remove(0);
  99. }
  100. }
  101. public void messageArrived(String topic, MqttMessage message) throws Exception {
  102. log.info("message arrived: '" + new String(message.getPayload()) + "' "+this.hashCode()+
  103. " " + (message.isDuplicate() ? "duplicate" : ""));
  104. if (!message.isDuplicate()) {
  105. synchronized (messages) {
  106. messages.add(message);
  107. messages.notifyAll();
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Basic test with 1 subscription for the synchronous client
  114. *
  115. * @throws Exception
  116. */
  117. @Test
  118. public void testSyncSubs1() throws Exception {
  119. final String methodName = Utility.getMethodName();
  120. LoggingUtilities.banner(log, cclass, methodName);
  121. log.entering(className, methodName);
  122. listener mylistener = new listener();
  123. IMqttClient mqttClient = clientFactory.createMqttClient(serverURI, methodName);
  124. String mytopic = topicPrefix + "PerSubscriptionTest/topic";
  125. mqttClient.connect();
  126. log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
  127. mqttClient.subscribe(mytopic, 2, mylistener);
  128. MqttMessage message = new MqttMessage();
  129. message.setPayload("testSyncSubs1".getBytes());
  130. mqttClient.publish(mytopic, message);
  131. log.info("Checking msg");
  132. MqttMessage msg = mylistener.getNextMessage();
  133. Assert.assertNotNull(msg);
  134. Assert.assertEquals("testSyncSubs1", msg.toString());
  135. mqttClient.disconnect();
  136. mqttClient.close();
  137. }
  138. @Test
  139. public void testAsyncSubs1() throws Exception {
  140. final String methodName = Utility.getMethodName();
  141. LoggingUtilities.banner(log, cclass, methodName);
  142. log.entering(className, methodName);
  143. listener mylistener = new listener();
  144. IMqttAsyncClient mqttClient = clientFactory.createMqttAsyncClient(serverURI, methodName);
  145. String mytopic = topicPrefix + "PerSubscriptionTest/topic";
  146. IMqttToken token = mqttClient.connect(null, null);
  147. log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
  148. token.waitForCompletion();
  149. token = mqttClient.subscribe(mytopic, 2, mylistener);
  150. token.waitForCompletion();
  151. MqttMessage message = new MqttMessage();
  152. message.setPayload("testAsyncSubs1".getBytes());
  153. token = mqttClient.publish(mytopic, message);
  154. token.waitForCompletion();
  155. log.info("Checking msg");
  156. MqttMessage msg = mylistener.getNextMessage();
  157. Assert.assertNotNull(msg);
  158. Assert.assertEquals("testAsyncSubs1", msg.toString());
  159. token = mqttClient.disconnect();
  160. token.waitForCompletion();
  161. mqttClient.close();
  162. }
  163. /*
  164. * Check handlers still exist after reconnection non-cleansession
  165. */
  166. @Test
  167. public void testSyncCleanSessionFalse() throws Exception {
  168. final String methodName = Utility.getMethodName();
  169. LoggingUtilities.banner(log, cclass, methodName);
  170. log.entering(className, methodName);
  171. listener mylistener = new listener();
  172. IMqttClient mqttClient = clientFactory.createMqttClient(serverURI, methodName);
  173. String mytopic = topicPrefix + "PerSubscriptionTest/topic";
  174. MqttConnectOptions opts = new MqttConnectOptions();
  175. opts.setCleanSession(false);
  176. mqttClient.connect(opts);
  177. log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
  178. mqttClient.subscribe(mytopic, 2, mylistener);
  179. MqttMessage message = new MqttMessage();
  180. message.setPayload("testSyncCleanSessionFalse".getBytes());
  181. mqttClient.publish(mytopic, message);
  182. log.info("Checking msg");
  183. MqttMessage msg = mylistener.getNextMessage();
  184. Assert.assertNotNull(msg);
  185. Assert.assertEquals("testSyncCleanSessionFalse", msg.toString());
  186. mqttClient.disconnect();
  187. /* subscription handler should still exist on reconnect */
  188. mqttClient.connect(opts);
  189. log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
  190. message = new MqttMessage();
  191. message.setPayload("testSyncCleanSessionFalse1".getBytes());
  192. mqttClient.publish(mytopic, message);
  193. log.info("Checking msg");
  194. msg = mylistener.getNextMessage();
  195. Assert.assertNotNull(msg);
  196. Assert.assertEquals("testSyncCleanSessionFalse1", msg.toString());
  197. mqttClient.disconnect();
  198. /* clean up by connecting cleansession */
  199. mqttClient.connect();
  200. mqttClient.disconnect();
  201. mqttClient.close();
  202. }
  203. @Test
  204. public void testAsyncCleanSessionFalse() throws Exception {
  205. final String methodName = Utility.getMethodName();
  206. LoggingUtilities.banner(log, cclass, methodName);
  207. log.entering(className, methodName);
  208. listener mylistener = new listener();
  209. IMqttAsyncClient mqttClient = clientFactory.createMqttAsyncClient(serverURI, methodName);
  210. String mytopic = topicPrefix + "PerSubscriptionTest/topic";
  211. MqttConnectOptions opts = new MqttConnectOptions();
  212. opts.setCleanSession(false);
  213. IMqttToken token = mqttClient.connect(opts, null, null);
  214. log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
  215. token.waitForCompletion();
  216. token = mqttClient.subscribe(mytopic, 2, mylistener);
  217. token.waitForCompletion();
  218. MqttMessage message = new MqttMessage();
  219. message.setPayload("testAsyncCleanSessionFalse".getBytes());
  220. token = mqttClient.publish(mytopic, message);
  221. token.waitForCompletion();
  222. log.info("Checking msg");
  223. MqttMessage msg = mylistener.getNextMessage();
  224. Assert.assertNotNull(msg);
  225. Assert.assertEquals("testAsyncCleanSessionFalse", msg.toString());
  226. token = mqttClient.disconnect();
  227. token.waitForCompletion();
  228. /* subscription handler should still exist on reconnect */
  229. token = mqttClient.connect(opts, null, null);
  230. log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
  231. token.waitForCompletion();
  232. message = new MqttMessage();
  233. message.setPayload("testAsyncCleanSessionFalse1".getBytes());
  234. token = mqttClient.publish(mytopic, message);
  235. token.waitForCompletion();
  236. log.info("Checking msg");
  237. msg = mylistener.getNextMessage();
  238. Assert.assertNotNull(msg);
  239. Assert.assertEquals("testAsyncCleanSessionFalse1", msg.toString());
  240. token = mqttClient.disconnect();
  241. token.waitForCompletion();
  242. /* clean up by connecting cleansession */
  243. token = mqttClient.connect();
  244. token.waitForCompletion();
  245. token = mqttClient.disconnect();
  246. token.waitForCompletion();
  247. mqttClient.close();
  248. }
  249. /* check unsubscribe removes handlers */
  250. /*
  251. @Test
  252. public void testSyncUnsubscribeRemove() throws Exception {
  253. final String methodName = Utility.getMethodName();
  254. LoggingUtilities.banner(log, cclass, methodName);
  255. log.entering(className, methodName);
  256. listener mylistener = new listener();
  257. IMqttClient mqttClient = clientFactory.createMqttClient(serverURI, methodName);
  258. String mytopic = topicPrefix + "PerSubscriptionTest/topic";
  259. MqttConnectOptions opts = new MqttConnectOptions();
  260. opts.setCleanSession(false);
  261. mqttClient.connect(opts);
  262. log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
  263. mqttClient.subscribe(mytopic, 2, mylistener);
  264. MqttMessage message = new MqttMessage();
  265. message.setPayload("foo".getBytes());
  266. mqttClient.publish(mytopic, message);
  267. log.info("Checking msg");
  268. MqttMessage msg = mylistener.getNextMessage();
  269. Assert.assertNotNull(msg);
  270. Assert.assertEquals("foo", msg.toString());
  271. mqttClient.unsubscribe(mytopic); // unsubscribe will remove the message handler
  272. mqttClient.subscribe(mytopic, 2); // but so will this
  273. message = new MqttMessage();
  274. message.setPayload("foo1".getBytes());
  275. mqttClient.publish(mytopic, message);
  276. log.info("Checking msg");
  277. msg = mylistener.getNextMessage();
  278. Assert.assertNotNull(msg);
  279. Assert.assertEquals("foo1", msg.toString());
  280. mqttClient.disconnect();
  281. mqttClient.close();
  282. }*/
  283. /* check can resubscribe after client object recreation */
  284. }