Skip to content

Commit e902d83

Browse files
committed
Fixed and refactored realtime message tests
1 parent bc24bfd commit e902d83

File tree

1 file changed

+37
-43
lines changed

1 file changed

+37
-43
lines changed

lib/src/test/java/io/ably/lib/test/realtime/RealtimeMessageTest.java

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package io.ably.lib.test.realtime;
22

3-
import static org.junit.Assert.assertArrayEquals;
4-
import static org.junit.Assert.assertEquals;
5-
import static org.junit.Assert.assertNotEquals;
6-
import static org.junit.Assert.assertTrue;
7-
import static org.junit.Assert.fail;
8-
93
import java.io.IOException;
104
import java.util.ArrayList;
115
import java.util.HashMap;
@@ -51,18 +45,24 @@
5145
import io.ably.lib.types.ProtocolMessage;
5246
import io.ably.lib.util.Log;
5347

48+
import static org.junit.Assert.assertArrayEquals;
49+
import static org.junit.Assert.assertEquals;
50+
import static org.junit.Assert.assertNotEquals;
51+
import static org.junit.Assert.assertNull;
52+
import static org.junit.Assert.assertTrue;
53+
import static org.junit.Assert.fail;
54+
5455
public class RealtimeMessageTest extends ParameterizedTest {
5556

5657
private static final String testMessagesEncodingFile = "ably-common/test-resources/messages-encoding.json";
57-
private static Gson gson = new Gson();
58+
private static final Gson gson = new Gson();
5859

5960
@Rule
6061
public Timeout testTimeout = Timeout.seconds(300);
6162

6263
/**
6364
* Connect to the service and attach, subscribe to an event, and publish on that channel
6465
*/
65-
@Ignore("FIXME: fix exception")
6666
@Test
6767
public void single_send() {
6868
AblyRealtime ably = null;
@@ -107,7 +107,6 @@ public void single_send() {
107107
* attach, subscribe to an event, publish on one
108108
* connection and confirm receipt on the other.
109109
*/
110-
@Ignore("FIXME: fix exception")
111110
@Test
112111
public void single_send_noecho() {
113112
AblyRealtime txAbly = null;
@@ -166,7 +165,6 @@ public void single_send_noecho() {
166165
* Get a channel and subscribe without explicitly attaching.
167166
* Verify that the channel reaches the attached state.
168167
*/
169-
@Ignore("FIXME: fix exception")
170168
@Test
171169
public void subscribe_implicit_attach() {
172170
AblyRealtime ably = null;
@@ -243,7 +241,7 @@ private void _multiple_send(String channelName, int messageCount, int msgSize, b
243241

244242
/* wait for the publish callback to be called */
245243
ErrorInfo[] errors = msgComplete.waitFor();
246-
assertTrue("Verify success from all message callbacks", errors.length == 0);
244+
assertEquals("Verify success from all message callbacks", 0, errors.length);
247245

248246
/* wait for the subscription callback to be called */
249247
messageWaiter.waitFor(messageCount);
@@ -261,13 +259,13 @@ private void _multiple_send(String channelName, int messageCount, int msgSize, b
261259
channel.publish("test_event", messageData, msgComplete.add());
262260
try {
263261
Thread.sleep(delay);
264-
} catch(InterruptedException e) {
262+
} catch(InterruptedException ignored) {
265263
}
266264
}
267265

268266
/* wait for the publish callback to be called */
269267
ErrorInfo[] errors = msgComplete.waitFor();
270-
assertTrue("Verify success from all message callbacks", errors.length == 0);
268+
assertEquals("Verify success from all message callbacks", 0, errors.length);
271269

272270
/* wait for the subscription callback to be called */
273271
messageWaiter.waitFor(messageCount);
@@ -293,7 +291,6 @@ private void _multiple_send(String channelName, int messageCount, int msgSize, b
293291
* Test right and wrong channel states to publish messages
294292
* Tests RTL6c
295293
*/
296-
@Ignore("FIXME: fix exception")
297294
@Test
298295
public void publish_channel_state() {
299296
AblyRealtime ably = null;
@@ -384,7 +381,7 @@ private void _multiple_send_batch(String channelName, int messageCount, int batc
384381

385382
/* wait for the publish callback to be called */
386383
ErrorInfo[] errors = msgComplete.waitFor();
387-
assertTrue("Verify success from all message callbacks", errors.length == 0);
384+
assertEquals("Verify success from all message callbacks", 0, errors.length);
388385

389386
/* wait for the subscription callback to be called */
390387
messageWaiter.waitFor(messageCount);
@@ -399,55 +396,48 @@ private void _multiple_send_batch(String channelName, int messageCount, int batc
399396
}
400397
}
401398

402-
@Ignore("FIXME: fix exception")
403399
@Test
404400
public void multiple_send_10_1000_16_string() {
405401
int messageCount = 10;
406402
long delay = 1000L;
407403
_multiple_send("multiple_send_10_1000_16_string_" + testParams.name, messageCount, 16, false, delay);
408404
}
409405

410-
@Ignore("FIXME: fix exception")
411406
@Test
412407
public void multiple_send_10_1000_16_binary() {
413408
int messageCount = 10;
414409
long delay = 1000L;
415410
_multiple_send("multiple_send_10_1000_16_binary_" + testParams.name, messageCount, 16, true, delay);
416411
}
417412

418-
@Ignore("FIXME: fix exception")
419413
@Test
420414
public void multiple_send_10_1000_512_string() {
421415
int messageCount = 10;
422416
long delay = 1000L;
423417
_multiple_send("multiple_send_10_1000_512_string_" + testParams.name, messageCount, 512, false, delay);
424418
}
425419

426-
@Ignore("FIXME: fix exception")
427420
@Test
428421
public void multiple_send_10_1000_512_binary() {
429422
int messageCount = 10;
430423
long delay = 1000L;
431424
_multiple_send("multiple_send_10_1000_512_binary_" + testParams.name, messageCount, 512, true, delay);
432425
}
433426

434-
@Ignore("FIXME: fix exception")
435427
@Test
436428
public void multiple_send_20_200() {
437429
int messageCount = 20;
438430
long delay = 200L;
439431
_multiple_send("multiple_send_20_200_" + testParams.name, messageCount, 256, true, delay);
440432
}
441433

442-
@Ignore("FIXME: fix exception")
443434
@Test
444435
public void multiple_send_200_50() {
445436
int messageCount = 200;
446437
long delay = 50L;
447438
_multiple_send("multiple_send_binary_200_50_" + testParams.name, messageCount, 256, true, delay);
448439
}
449440

450-
@Ignore("FIXME: fix exception")
451441
@Test
452442
public void multiple_send_1000_10() {
453443
int messageCount = 1000;
@@ -522,7 +512,6 @@ public void ensure_disconnect_with_error_does_not_move_to_failed() {
522512
}
523513
}
524514

525-
@Ignore("FIXME: fix exception")
526515
@Test
527516
public void messages_encoding_fixtures() {
528517
MessagesEncodingData fixtures;
@@ -587,7 +576,6 @@ public MessagesEncodingDataItem[] handleResponse(HttpCore.Response response, Err
587576
}
588577
}
589578

590-
@Ignore("FIXME: fix exception")
591579
@Test
592580
public void messages_msgpack_and_json_encoding_is_compatible() {
593581
MessagesEncodingData fixtures;
@@ -739,8 +727,10 @@ public void println(int severity, String tag, String msg, Throwable tr) {
739727
synchronized(log) {
740728
boolean foundErrorMessage = false;
741729
for(String logMessage : log) {
742-
if(logMessage.contains("encryption is not set up"))
730+
if (logMessage.contains("encryption is not set up")) {
743731
foundErrorMessage = true;
732+
break;
733+
}
744734
}
745735
assertTrue("Verify logged error messages", foundErrorMessage);
746736
}
@@ -754,22 +744,27 @@ public void println(int severity, String tag, String msg, Throwable tr) {
754744
}
755745

756746
private void expectDataToMatch(MessagesEncodingDataItem fixtureMessage, Message receivedMessage) {
757-
if(fixtureMessage.expectedType.equals("string")) {
758-
assertEquals("Verify decoded message data", fixtureMessage.expectedValue.getAsString(), receivedMessage.data);
759-
} else if(fixtureMessage.expectedType.equals("jsonObject")) {
760-
assertEquals("Verify decoded message data", fixtureMessage.expectedValue.getAsJsonObject(), receivedMessage.data);
761-
} else if(fixtureMessage.expectedType.equals("jsonArray")) {
762-
assertEquals("Verify decoded message data", fixtureMessage.expectedValue.getAsJsonArray(), receivedMessage.data);
763-
} else if(fixtureMessage.expectedType.equals("binary")) {
764-
byte[] receivedData = (byte[]) receivedMessage.data;
765-
StringBuilder sb = new StringBuilder(receivedData.length * 2);
766-
for(byte b : receivedData) {
767-
sb.append(String.format("%02x", b & 0xff));
768-
}
769-
String receivedDataHex = sb.toString();
770-
assertEquals("Verify decoded message data", fixtureMessage.expectedHexValue, receivedDataHex);
771-
} else {
772-
throw new RuntimeException(String.format(Locale.ROOT, "unhandled: %s", fixtureMessage.expectedType));
747+
switch (fixtureMessage.expectedType) {
748+
case "string":
749+
assertEquals("Verify decoded message data", fixtureMessage.expectedValue.getAsString(), receivedMessage.data);
750+
break;
751+
case "jsonObject":
752+
assertEquals("Verify decoded message data", fixtureMessage.expectedValue.getAsJsonObject(), receivedMessage.data);
753+
break;
754+
case "jsonArray":
755+
assertEquals("Verify decoded message data", fixtureMessage.expectedValue.getAsJsonArray(), receivedMessage.data);
756+
break;
757+
case "binary":
758+
byte[] receivedData = (byte[]) receivedMessage.data;
759+
StringBuilder sb = new StringBuilder(receivedData.length * 2);
760+
for (byte b : receivedData) {
761+
sb.append(String.format("%02x", b & 0xff));
762+
}
763+
String receivedDataHex = sb.toString();
764+
assertEquals("Verify decoded message data", fixtureMessage.expectedHexValue, receivedDataHex);
765+
break;
766+
default:
767+
throw new RuntimeException(String.format(Locale.ROOT, "unhandled: %s", fixtureMessage.expectedType));
773768
}
774769
}
775770

@@ -804,7 +799,7 @@ public void reject_invalid_message_data() throws AblyException {
804799
message.encode(null);
805800
fail("reject_invalid_message_data: Expected AblyException to be thrown.");
806801
} catch(AblyException e) {
807-
assertEquals(null, message.encoding);
802+
assertNull(message.encoding);
808803
assertEquals(data, message.data);
809804
} catch(Throwable t) {
810805
fail("reject_invalid_message_data: Unexpected exception");
@@ -873,7 +868,6 @@ public void message_from_encoded_json_object() throws AblyException {
873868
* Refer Spec. TM3
874869
* @throws AblyException
875870
*/
876-
@Ignore("FIXME: fix exception")
877871
@Test
878872
public void messages_from_encoded_json_array() throws AblyException {
879873
JsonArray fixtures = null;

0 commit comments

Comments
 (0)