|
13 | 13 |
|
14 | 14 | package org.eclipse.paho.client.mqttv3.test; |
15 | 15 |
|
| 16 | +import java.io.IOException; |
16 | 17 | import java.net.URI; |
17 | 18 | import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Random; |
18 | 21 | import java.util.logging.Level; |
19 | 22 | import java.util.logging.Logger; |
20 | 23 |
|
@@ -183,6 +186,62 @@ public void testWebSocketPubSub() throws Exception { |
183 | 186 | } |
184 | 187 | } |
185 | 188 | } |
| 189 | + |
| 190 | + /** |
| 191 | + * Tests Websocker support for packets over 16KB |
| 192 | + * Prompted by Bug: 482432 |
| 193 | + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=482432 |
| 194 | + * This test connects to a broker via WebSockets, subscribes |
| 195 | + * to a topic, publishes a large payload to it and checks |
| 196 | + * that it recieves the same payload. |
| 197 | + * @throws Exception |
| 198 | + */ |
| 199 | + @Test |
| 200 | + public void largePayloadTest() throws Exception{ |
| 201 | + // Generate large byte array; |
| 202 | + byte[] largeByteArray = new byte[32000]; |
| 203 | + new Random().nextBytes(largeByteArray); |
| 204 | + String methodName = Utility.getMethodName(); |
| 205 | + LoggingUtilities.banner(log, cclass, methodName); |
| 206 | + |
| 207 | + IMqttClient client = null; |
| 208 | + try { |
| 209 | + String topicStr = "topic_largeFile_01"; |
| 210 | + String clientId = methodName; |
| 211 | + client = clientFactory.createMqttClient(serverURI, clientId); |
| 212 | + |
| 213 | + log.info("Assigning callback..."); |
| 214 | + MessageListener listener = new MessageListener(); |
| 215 | + client.setCallback(listener); |
| 216 | + |
| 217 | + log.info("Connecting... serverURI:" + serverURI + ", ClientId:" + clientId); |
| 218 | + client.connect(); |
| 219 | + |
| 220 | + log.info("Subscribing to..." + topicStr); |
| 221 | + client.subscribe(topicStr); |
| 222 | + |
| 223 | + log.info("Publishing to..." + topicStr); |
| 224 | + MqttTopic topic = client.getTopic(topicStr); |
| 225 | + MqttMessage message = new MqttMessage(largeByteArray); |
| 226 | + topic.publish(message); |
| 227 | + |
| 228 | + log.info("Checking msg"); |
| 229 | + MqttMessage msg = listener.getNextMessage(); |
| 230 | + Assert.assertNotNull(msg); |
| 231 | + Assert.assertTrue(Arrays.equals(largeByteArray, msg.getPayload())); |
| 232 | + log.info("Disconnecting..."); |
| 233 | + client.disconnect(); |
| 234 | + log.info("Disconnected..."); |
| 235 | + } catch (Exception e){ |
| 236 | + e.printStackTrace(); |
| 237 | + } finally { |
| 238 | + if (client != null) { |
| 239 | + log.info("Close..."); |
| 240 | + client.close(); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + } |
186 | 245 |
|
187 | 246 |
|
188 | 247 |
|
@@ -211,7 +270,8 @@ public MqttMessage getNextMessage() { |
211 | 270 | synchronized (messages) { |
212 | 271 | if (messages.size() == 0) { |
213 | 272 | try { |
214 | | - messages.wait(1000); |
| 273 | + // Wait a bit longer than usual because of the largePayloadTest |
| 274 | + messages.wait(10000); |
215 | 275 | } |
216 | 276 | catch (InterruptedException e) { |
217 | 277 | // empty |
|
0 commit comments