Skip to content

Commit 68a0944

Browse files
committed
Refactored ably-java tests, removed unnecessary callbacks
1 parent e791f8f commit 68a0944

File tree

4 files changed

+59
-69
lines changed

4 files changed

+59
-69
lines changed

lib/src/test/java/io/ably/lib/test/common/Helpers.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public synchronized ErrorInfo waitFor(ConnectionState state) {
477477
while (currentState() != state) {
478478
try {
479479
wait();
480-
} catch (InterruptedException e) {
480+
} catch (InterruptedException ignored) {
481481
}
482482
}
483483
Log.d(TAG, "waitFor done: state=" + targetStateName + ")");
@@ -493,8 +493,8 @@ public synchronized void waitFor(ConnectionState state, int count) {
493493
Log.d(TAG, "waitFor(state=" + state.getConnectionEvent().name() + ", count=" + count + ")");
494494

495495
while(getStateCount(state) < count)
496-
try { wait(); } catch(InterruptedException e) {}
497-
Log.d(TAG, "waitFor done: state=" + latestChange.current.getConnectionEvent().name() + ", count=" + getStateCount(state) + ")");
496+
try { wait(); } catch(InterruptedException ignored) {}
497+
Log.d(TAG, "waitFor done: state=" + lastStateChange().current.getConnectionEvent().name() + ", count=" + getStateCount(state) + ")");
498498
}
499499

500500
/**
@@ -511,7 +511,7 @@ public synchronized boolean waitFor(ConnectionState state, int count, long time)
511511
long remaining = time;
512512
while(getStateCount(state) < count && remaining > 0) {
513513
Log.d(TAG, "waitFor(state=" + state.getConnectionEvent().name() + ", waiting for=" + remaining + ")");
514-
try { wait(remaining); } catch(InterruptedException e) {}
514+
try { wait(remaining); } catch(InterruptedException ignored) {}
515515
remaining = targetTime - System.currentTimeMillis();
516516
}
517517
int stateCount = getStateCount(state);
@@ -552,7 +552,7 @@ public synchronized void reset() {
552552
@Override
553553
public void onConnectionStateChanged(ConnectionStateListener.ConnectionStateChange state) {
554554
synchronized(this) {
555-
latestChange = state;
555+
stateChanges.add(state);
556556
reason = state.reason;
557557
Counter counter = stateCounts.get(state.current); if(counter == null) stateCounts.put(state.current, (counter = new Counter()));
558558
counter.incr();
@@ -573,15 +573,23 @@ private synchronized int getStateCount(ConnectionState state) {
573573
}
574574

575575
private synchronized ConnectionState currentState() {
576-
return latestChange == null ? connection.state : latestChange.current;
576+
ConnectionStateChange stateChange = lastStateChange();
577+
return stateChange == null ? connection.state : stateChange.current;
578+
}
579+
580+
public synchronized ConnectionStateChange lastStateChange() {
581+
if (stateChanges.size() == 0) {
582+
return null;
583+
}
584+
return stateChanges.get(stateChanges.size() -1);
577585
}
578586

579587
/**
580588
* Internal
581589
*/
582-
private Connection connection;
590+
private final Connection connection;
583591
private ErrorInfo reason;
584-
private ConnectionStateChange latestChange;
592+
private final List<ConnectionStateChange> stateChanges = new ArrayList<>();
585593
private Map<ConnectionState, Counter> stateCounts;
586594
private static final String TAG = ConnectionWaiter.class.getName();
587595
}

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import io.ably.lib.realtime.AblyRealtime;
55
import io.ably.lib.realtime.Channel;
66
import io.ably.lib.realtime.ChannelState;
7-
import io.ably.lib.realtime.ConnectionEvent;
87
import io.ably.lib.realtime.ConnectionState;
98
import io.ably.lib.realtime.ConnectionStateListener;
109
import io.ably.lib.rest.AblyRest;
@@ -147,7 +146,8 @@ public void realtime_connection_with_auth_url_in_query_string_connects() {
147146
* Spec: RSA4d, RSA4d1
148147
*/
149148
@Test
150-
public void auth_client_fails_authorize_server_forbidden() {
149+
public void auth_client_fails() {
150+
AblyRealtime ablyRealtime = null;
151151
try {
152152
/* init ably for token */
153153
ClientOptions optsForToken = createOptions(testVars.keys[0].keyStr);
@@ -163,25 +163,13 @@ public void auth_client_fails_authorize_server_forbidden() {
163163
opts.authUrl = "https://echo.ably.io/respondwith";
164164
opts.authParams = new Param[]{ new Param("status", 403)};
165165

166-
final AblyRealtime ablyRealtime = new AblyRealtime(opts);
166+
ablyRealtime = new AblyRealtime(opts);
167167
ablyRealtime.connection.connect();
168168

169169
/* wait for connected state */
170170
Helpers.ConnectionWaiter connectionWaiter = new Helpers.ConnectionWaiter(ablyRealtime.connection);
171171
connectionWaiter.waitFor(ConnectionState.connected);
172172

173-
/* create listener for ConnectionEvent.failed */
174-
ablyRealtime.connection.once(ConnectionEvent.failed, new ConnectionStateListener() {
175-
@Override
176-
public void onConnectionStateChanged(ConnectionStateChange stateChange) {
177-
/* assert that state changes correctly */
178-
assertEquals(ConnectionState.connected, stateChange.previous);
179-
assertEquals(80019, stateChange.reason.code);
180-
assertEquals(80019, ablyRealtime.connection.reason.code);
181-
assertEquals(403, ablyRealtime.connection.reason.statusCode);
182-
}
183-
});
184-
185173
try {
186174
opts.tokenDetails = null;
187175
/* try to authorize */
@@ -194,11 +182,21 @@ public void onConnectionStateChanged(ConnectionStateChange stateChange) {
194182

195183
/* wait for failed state */
196184
connectionWaiter.waitFor(ConnectionState.failed);
185+
ConnectionStateListener.ConnectionStateChange lastStateChange = connectionWaiter.lastStateChange();
186+
assertEquals(ConnectionState.failed, lastStateChange.current);
187+
assertEquals(80019, lastStateChange.reason.code);
188+
assertEquals(403, lastStateChange.reason.statusCode);
189+
197190
assertEquals("Verify connected state has failed", ConnectionState.failed, ablyRealtime.connection.state);
198191
assertEquals("Check correct cause error code", 403, ablyRealtime.connection.reason.statusCode);
192+
assertEquals(80019, ablyRealtime.connection.reason.code);
193+
199194
} catch (AblyException e) {
200195
e.printStackTrace();
201196
fail();
197+
} finally {
198+
assert ablyRealtime != null;
199+
ablyRealtime.close();
202200
}
203201
}
204202

@@ -350,7 +348,7 @@ public void auth_client_match_token_null_clientId() {
350348
assertEquals("Verify connected state is reached", ConnectionState.connected, ablyRealtime.connection.state);
351349

352350
/* check expected clientId */
353-
assertEquals("Auth#clientId is expected to be null", null, ablyRealtime.auth.clientId);
351+
assertNull("Auth#clientId is expected to be null", ablyRealtime.auth.clientId);
354352

355353
ablyRealtime.close();
356354
} catch (AblyException e) {
@@ -383,7 +381,7 @@ public void auth_clientid_null_before_auth() {
383381
AblyRealtime ablyRealtime = new AblyRealtime(opts);
384382

385383
/* check expected clientId */
386-
assertEquals("Auth#clientId is expected to be null", null, ablyRealtime.auth.clientId);
384+
assertNull("Auth#clientId is expected to be null", ablyRealtime.auth.clientId);
387385

388386
/* wait for connected state */
389387
ablyRealtime.connection.connect();
@@ -688,7 +686,7 @@ public void auth_client_match_tokendetails_clientId_fail() {
688686
ClientOptions opts = createOptions();
689687
opts.clientId = "options clientId";
690688
opts.tokenDetails = tokenDetails;
691-
AblyRealtime ablyRealtime = new AblyRealtime(opts);
689+
new AblyRealtime(opts);
692690
} catch (AblyException e) {
693691
assertEquals("Verify error code indicates clientId mismatch", e.errorInfo.code, 40101);
694692
}
@@ -773,7 +771,7 @@ public void auth_clientid_publish_implicit() {
773771

774772
/* Get sent message */
775773
Message messagePublished = protocolListener.sentMessages.get(0).messages[0];
776-
assertEquals("Sent message does not contain clientId", messagePublished.clientId, null);
774+
assertNull("Sent message does not contain clientId", messagePublished.clientId);
777775

778776
/* wait until message received on transport */
779777
protocolListener.waitForRecv(1);
@@ -819,7 +817,7 @@ public void auth_clientid_publish_implicit() {
819817
channel.publish(messageToPublish, pubComplete.add());
820818
pubComplete.waitFor();
821819
assertTrue("Verify publish callback called on completion", pubComplete.pending.isEmpty());
822-
assertTrue("Verify publish callback returns an error", pubComplete.errors.size() == 1);
820+
assertEquals("Verify publish callback returns an error", 1, pubComplete.errors.size());
823821
assertEquals("Verify publish callback error has expected error code", pubComplete.errors.iterator().next().code, 40012);
824822

825823
/* verify no message sent or received on transport */
@@ -838,7 +836,7 @@ public void auth_clientid_publish_implicit() {
838836

839837
/* Get sent message */
840838
messagePublished = protocolListener.sentMessages.get(0).messages[0];
841-
assertEquals("Sent message does not contain clientId", messagePublished.clientId, null);
839+
assertNull("Sent message does not contain clientId", messagePublished.clientId);
842840

843841
/* wait until message received on transport */
844842
protocolListener.waitForRecv(1);
@@ -927,7 +925,7 @@ public void auth_clientid_publish_explicit_before_identified() {
927925

928926
/* Get sent message */
929927
messagePublished = protocolListener.sentMessages.get(0).messages[0];
930-
assertEquals("Sent message does not contain clientId", messagePublished.clientId, null);
928+
assertNull("Sent message does not contain clientId", messagePublished.clientId);
931929

932930
/* wait until message received on transport */
933931
protocolListener.waitForRecv(1);
@@ -996,7 +994,7 @@ public Object getTokenRequest(Auth.TokenParams params) {
996994
ably.connect();
997995
try {
998996
opts.wait();
999-
} catch(InterruptedException ie) {}
997+
} catch(InterruptedException ignored) {}
1000998
ably.auth.renew();
1001999
}
10021000

@@ -1066,7 +1064,7 @@ public Object getTokenRequest(Auth.TokenParams params) {
10661064
ably.connect();
10671065
try {
10681066
opts.wait();
1069-
} catch(InterruptedException ie) {}
1067+
} catch(InterruptedException ignored) {}
10701068

10711069
ably.auth.renewAuth((success, tokenDetails1, errorInfo) -> {
10721070
//Ignore completion handling
@@ -1183,7 +1181,7 @@ public void auth_expired_token_expire_before_connect_renew() {
11831181
assertNotNull("Expected token value", tokenDetails.token);
11841182

11851183
/* allow to expire */
1186-
try { Thread.sleep(200L); } catch(InterruptedException ie) {}
1184+
try { Thread.sleep(200L); } catch(InterruptedException ignored) {}
11871185

11881186
/* create Ably realtime instance with token and authCallback */
11891187
ClientOptions opts = createOptions();

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public void channelhistory_wait_b() {
355355
/* wait for the history to be persisted */
356356
try {
357357
Thread.sleep(16000);
358-
} catch(InterruptedException ie) {}
358+
} catch(InterruptedException ignored) {}
359359

360360
/* get the history for this channel */
361361
PaginatedResult<Message> messages = channel.history(null);
@@ -455,7 +455,7 @@ public void channelhistory_mixed_b() {
455455
/* wait for the history to be persisted */
456456
try {
457457
Thread.sleep(16000);
458-
} catch(InterruptedException ie) {}
458+
} catch(InterruptedException ignored) {}
459459

460460
/* publish to the channel */
461461
msgComplete = new CompletionWaiter();
@@ -517,7 +517,7 @@ public void channelhistory_mixed_f() {
517517
/* wait for the history to be persisted */
518518
try {
519519
Thread.sleep(16000);
520-
} catch(InterruptedException ie) {}
520+
} catch(InterruptedException ignored) {}
521521

522522
/* publish to the channel */
523523
msgComplete = new CompletionWaiter();
@@ -654,7 +654,6 @@ public void channelhistory_limit_b() {
654654
} catch (AblyException e) {
655655
e.printStackTrace();
656656
fail("channelhistory_limit_b: Unexpected exception");
657-
return;
658657
} finally {
659658
if(ably != null)
660659
ably.close();
@@ -720,10 +719,7 @@ public void channelhistory_time_f() {
720719
for(int i = 20; i < 40; i++)
721720
expectedMessageHistory[i - 20] = messageContents.get("history" + i);
722721
Assert.assertArrayEquals("Expect messages in forward order", messages.items(), expectedMessageHistory);
723-
} catch (AblyException e) {
724-
e.printStackTrace();
725-
fail("channelhistory_time_f: Unexpected exception");
726-
} catch (InterruptedException e) {
722+
} catch (AblyException | InterruptedException e) {
727723
e.printStackTrace();
728724
fail("channelhistory_time_f: Unexpected exception");
729725
} finally {
@@ -791,10 +787,7 @@ public void channelhistory_time_b() {
791787
for(int i = 20; i < 40; i++)
792788
expectedMessageHistory[i - 20] = messageContents.get("history" + (59 - i));
793789
Assert.assertArrayEquals("Expect messages in backwards order", messages.items(), expectedMessageHistory);
794-
} catch (AblyException e) {
795-
e.printStackTrace();
796-
fail("channelhistory_time_b: Unexpected exception");
797-
} catch (InterruptedException e) {
790+
} catch (AblyException | InterruptedException e) {
798791
e.printStackTrace();
799792
fail("channelhistory_time_b: Unexpected exception");
800793
} finally {
@@ -1205,7 +1198,7 @@ public void run() {
12051198
/* wait 2 seconds */
12061199
try {
12071200
Thread.sleep(2000L);
1208-
} catch(InterruptedException ie) {}
1201+
} catch(InterruptedException ignored) {}
12091202

12101203
/* subscribe; this will trigger the attach */
12111204
MessageWaiter messageWaiter = new MessageWaiter(rxChannel);

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -583,19 +583,16 @@ public void resume_publish_queue() {
583583
final Channel senderChannel = sender.channels.get(channelName);
584584
senderChannel.attach();
585585
(new ChannelWaiter(senderChannel)).waitFor(ChannelState.attached);
586-
assertEquals(
587-
"The sender's channel should be attached",
588-
senderChannel.state, ChannelState.attached
589-
);
586+
assertEquals("The sender's channel should be attached",
587+
senderChannel.state, ChannelState.attached);
590588

591589
/* create and attach channel to recv on */
592590
final Channel receiverChannel = receiver.channels.get(channelName);
593591
receiverChannel.attach();
594592
(new ChannelWaiter(receiverChannel)).waitFor(ChannelState.attached);
595-
assertEquals(
596-
"The receiver's channel should be attached",
597-
receiverChannel.state, ChannelState.attached
598-
);
593+
assertEquals("The receiver's channel should be attached",
594+
receiverChannel.state, ChannelState.attached);
595+
599596
/* subscribe */
600597
MessageWaiter messageWaiter = new MessageWaiter(receiverChannel);
601598

@@ -612,10 +609,8 @@ public void resume_publish_queue() {
612609

613610
/* wait for the subscription callback to be called */
614611
messageWaiter.waitFor(messageCount);
615-
assertEquals(
616-
"Did not receive the entire first round of messages",
617-
messageWaiter.receivedMessages.size(), messageCount
618-
);
612+
assertEquals("Did not receive the entire first round of messages",
613+
messageWaiter.receivedMessages.size(), messageCount);
619614
messageWaiter.reset();
620615

621616
/* disconnect the sender, without closing;
@@ -641,7 +636,6 @@ public void resume_publish_queue() {
641636
sender.connection.connect();
642637
(new ConnectionWaiter(sender.connection)).waitFor(ConnectionState.connected);
643638

644-
645639
/* wait for the publish callback to be called.*/
646640
errors = msgComplete2.waitFor();
647641
assertEquals("Second round of messages (queued) has errors", 0, errors.length);
@@ -655,10 +649,8 @@ public void resume_publish_queue() {
655649
received.size(), messageCount
656650
);
657651
for(int i=0; i<received.size(); i++) {
658-
assertEquals(
659-
"Received unexpected queued message",
660-
received.get(i).name, "queued_message_" + i
661-
);
652+
assertEquals("Received unexpected queued message", received.get(i).name,
653+
"queued_message_" + i);
662654
}
663655
} catch (AblyException e) {
664656
e.printStackTrace();
@@ -694,10 +686,8 @@ public void resume_publish_resend_pending_messages_when_resume_is_successful() {
694686
final Channel senderChannel = sender.channels.get(channelName);
695687
senderChannel.attach();
696688
(new ChannelWaiter(senderChannel)).waitFor(ChannelState.attached);
697-
assertEquals(
698-
"The sender's channel should be attached",
699-
senderChannel.state, ChannelState.attached
700-
);
689+
assertEquals("The sender's channel should be attached",
690+
senderChannel.state, ChannelState.attached);
701691

702692
MockWebsocketFactory.MockWebsocketTransport transport = mockWebsocketFactory.getCreatedTransport();
703693

@@ -713,7 +703,9 @@ public void resume_publish_resend_pending_messages_when_resume_is_successful() {
713703
assertEquals("First completion has errors", 0, errors.length);
714704

715705
//assert that messages sent till now are sent with correct size and serials
716-
assertEquals("First round of messages has incorrect size", 3, transport.getPublishedMessages().size());
706+
assertEquals("First round of messages has incorrect size", 3,
707+
transport.getPublishedMessages().size());
708+
717709
for (int i = 0; i < transport.getPublishedMessages().size(); i++) {
718710
ProtocolMessage protocolMessage = transport.getPublishedMessages().get(i);
719711
assertEquals("Sent serial incorrect", Long.valueOf(i), protocolMessage.msgSerial);
@@ -724,7 +716,6 @@ public void resume_publish_resend_pending_messages_when_resume_is_successful() {
724716

725717
//block ack/nack messages to simulate pending message
726718
//note that this will only block ack/nack messages received by connection manager
727-
728719
mockWebsocketFactory.blockReceiveProcessing(message -> message.action == ProtocolMessage.Action.ack ||
729720
message.action == ProtocolMessage.Action.nack);
730721

0 commit comments

Comments
 (0)