Skip to content

Commit bbbcb4d

Browse files
authored
Merge pull request #941 from ably/flakey-test
flakey tests
2 parents 52318c1 + 888c143 commit bbbcb4d

File tree

4 files changed

+29
-38
lines changed

4 files changed

+29
-38
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,9 @@ public void onConnectionStateChanged(ConnectionStateChange state) {
615615
System.out.println("higher range: " + higherRange + " - lower range: " + lowerRange + " | checked value: " + retryTime);
616616

617617
assertTrue("retry time higher range for count " + i + " is not in valid: " + retryTime + " expected: " + higherRange,
618-
retryTime < higherRange);
618+
retryTime <= higherRange);
619619
assertTrue("retry time lower range for count " + i + " is not in valid: " + retryTime + " expected: " + lowerRange,
620-
retryTime > lowerRange);
620+
retryTime >= lowerRange);
621621
}
622622
System.out.println("------------------------------------------------------------");
623623
} catch (AblyException e) {

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ public Object getTokenRequest(TokenParams params) throws AblyException {
305305
@Override
306306
public Object handleResponse(HttpCore.Response response, ErrorInfo error) throws AblyException {
307307
try {
308-
callbackCalled.add(true);
308+
synchronized (tokens) {
309+
callbackCalled.add(true);
310+
}
309311
resultToken[0] = new String(response.body, "UTF-8");
310312
} catch (UnsupportedEncodingException e) {
311313
e.printStackTrace();
@@ -331,21 +333,24 @@ public void onRawConnect(String url) { }
331333
public void onRawMessageSend(ProtocolMessage message) { }
332334
@Override
333335
public void onRawMessageRecv(ProtocolMessage message) {
334-
if (message.action == ProtocolMessage.Action.auth) {
335-
authMessages[0] = true;
336+
synchronized (tokens) {
337+
if (message.action == ProtocolMessage.Action.auth) {
338+
authMessages[0] = true;
339+
}
336340
}
337341
}
338342
};
343+
339344
final AblyRealtime ablyRealtime = new AblyRealtime(options);
340345

341346
/* Once connected for the first time capture the assigned token and
342347
* verify the callback has been called once */
343348
ablyRealtime.connection.once(ConnectionEvent.connected, new ConnectionStateListener() {
344349
@Override
345350
public void onConnectionStateChanged(ConnectionStateChange stateChange) {
346-
assertTrue("Callback not called the first time", callbackCalled.get(0));
347-
assertEquals("State is not connected", ConnectionState.connected, stateChange.current);
348351
synchronized (tokens) {
352+
assertTrue("Callback not called the first time", callbackCalled.get(0));
353+
assertEquals("State is not connected", ConnectionState.connected, stateChange.current);
349354
tokens[0] = ablyRealtime.auth.getTokenDetails().token;
350355
}
351356
}
@@ -365,12 +370,13 @@ public void onConnectionStateChanged(ConnectionStateChange stateChange) {
365370
ablyRealtime.connection.on(ConnectionEvent.update, new ConnectionStateListener() {
366371
@Override
367372
public void onConnectionStateChanged(ConnectionStateChange state) {
368-
assertTrue("Callback not called the second time", callbackCalled.get(1));
369-
assertEquals("Callback not called 2 times", callbackCalled.size(), 2);
370-
assertNotEquals("Token should not be the same", tokens[0], ablyRealtime.auth.getTokenDetails().token);
371-
assertTrue("Auth protocol message has not been received", authMessages[0]);
372-
updateEvents[0] = true;
373-
ablyRealtime.close();
373+
synchronized (tokens) {
374+
assertTrue("Callback not called the second time", callbackCalled.get(1));
375+
assertNotEquals("Token should not be the same", ablyRealtime.auth.getTokenDetails().token, tokens[0]);
376+
assertTrue("Auth protocol message has not been received", authMessages[0]);
377+
updateEvents[0] = true;
378+
ablyRealtime.close();
379+
}
374380
}
375381
});
376382

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -796,34 +796,18 @@ static class MessagesEncodingDataItem {
796796
}
797797

798798
@Test
799+
799800
public void reject_invalid_message_data() throws AblyException {
800801
HashMap<String, Integer> data = new HashMap<String, Integer>();
801802
Message message = new Message("event", data);
802-
Log.LogHandler originalLogHandler = Log.handler;
803-
int originalLogLevel = Log.level;
804-
Log.setLevel(Log.DEBUG);
805-
final ArrayList<LogLine> capturedLog = new ArrayList<>();
806-
Log.setHandler(new Log.LogHandler() {
807-
@Override
808-
public void println(int severity, String tag, String msg, Throwable tr) {
809-
capturedLog.add(new LogLine(severity, tag, msg, tr));
810-
}
811-
});
812-
813803
try {
814804
message.encode(null);
805+
fail("reject_invalid_message_data: Expected AblyException to be thrown.");
815806
} catch(AblyException e) {
816807
assertEquals(null, message.encoding);
817808
assertEquals(data, message.data);
818-
assertEquals(1, capturedLog.size());
819-
LogLine capturedLine = capturedLog.get(0);
820-
assertTrue(capturedLine.tag.contains("ably"));
821-
assertTrue(capturedLine.msg.contains("Message data must be either `byte[]`, `String` or `JSONElement`; implicit coercion of other types to String is deprecated"));
822809
} catch(Throwable t) {
823810
fail("reject_invalid_message_data: Unexpected exception");
824-
} finally {
825-
Log.setHandler(originalLogHandler);
826-
Log.setLevel(originalLogLevel);
827811
}
828812
}
829813

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ public void resume_publish_reenter_when_resume_failed() throws AblyException {
962962
options.realtimeRequestTimeout = 2000L;
963963

964964
/* We want this greater than newTtl + newIdleInterval */
965-
final long waitInDisconnectedState = 3000L;
965+
final long waitInDisconnectedState = 5000L;
966966
options.transportFactory = mockWebsocketFactory;
967967
try(AblyRealtime ably = new AblyRealtime(options)) {
968968
final long newTtl = 1000L;
@@ -1017,12 +1017,6 @@ public void onConnectionStateChanged(ConnectionStateChange state) {
10171017
mockWebsocketFactory.blockReceiveProcessing(message -> message.action == ProtocolMessage.Action.ack ||
10181018
message.action == ProtocolMessage.Action.nack);
10191019

1020-
/* Wait for the connection to go stale, then reconnect */
1021-
try {
1022-
Thread.sleep(waitInDisconnectedState);
1023-
} catch (InterruptedException e) {
1024-
}
1025-
10261020
//enter next 3 clients
10271021
for (int i = 0; i < 3; i++) {
10281022
senderChannel.presence.enterClient(clients[i+3],null,presenceCompletion.add());
@@ -1045,6 +1039,13 @@ public void onConnectionStateChanged(ConnectionStateChange state) {
10451039
for (int i = 0; i < 3; i++) {
10461040
senderChannel.presence.enterClient(clients[i+6],null,presenceCompletion.add());
10471041
}
1042+
1043+
/* Wait for the connection to go stale, then reconnect */
1044+
try {
1045+
Thread.sleep(waitInDisconnectedState);
1046+
} catch (InterruptedException e) {
1047+
}
1048+
10481049
//now let's unblock the ack nacks and reconnect
10491050
mockWebsocketFactory.blockReceiveProcessing(message -> false);
10501051
/* Wait for the connection to go stale, then reconnect */

0 commit comments

Comments
 (0)