Skip to content

Commit

Permalink
Merge pull request #941 from ably/flakey-test
Browse files Browse the repository at this point in the history
flakey tests
  • Loading branch information
AndyTWF authored May 23, 2023
2 parents 52318c1 + 888c143 commit bbbcb4d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,9 @@ public void onConnectionStateChanged(ConnectionStateChange state) {
System.out.println("higher range: " + higherRange + " - lower range: " + lowerRange + " | checked value: " + retryTime);

assertTrue("retry time higher range for count " + i + " is not in valid: " + retryTime + " expected: " + higherRange,
retryTime < higherRange);
retryTime <= higherRange);
assertTrue("retry time lower range for count " + i + " is not in valid: " + retryTime + " expected: " + lowerRange,
retryTime > lowerRange);
retryTime >= lowerRange);
}
System.out.println("------------------------------------------------------------");
} catch (AblyException e) {
Expand Down
28 changes: 17 additions & 11 deletions lib/src/test/java/io/ably/lib/test/realtime/RealtimeJWTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ public Object getTokenRequest(TokenParams params) throws AblyException {
@Override
public Object handleResponse(HttpCore.Response response, ErrorInfo error) throws AblyException {
try {
callbackCalled.add(true);
synchronized (tokens) {
callbackCalled.add(true);
}
resultToken[0] = new String(response.body, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Expand All @@ -331,21 +333,24 @@ public void onRawConnect(String url) { }
public void onRawMessageSend(ProtocolMessage message) { }
@Override
public void onRawMessageRecv(ProtocolMessage message) {
if (message.action == ProtocolMessage.Action.auth) {
authMessages[0] = true;
synchronized (tokens) {
if (message.action == ProtocolMessage.Action.auth) {
authMessages[0] = true;
}
}
}
};

final AblyRealtime ablyRealtime = new AblyRealtime(options);

/* Once connected for the first time capture the assigned token and
* verify the callback has been called once */
ablyRealtime.connection.once(ConnectionEvent.connected, new ConnectionStateListener() {
@Override
public void onConnectionStateChanged(ConnectionStateChange stateChange) {
assertTrue("Callback not called the first time", callbackCalled.get(0));
assertEquals("State is not connected", ConnectionState.connected, stateChange.current);
synchronized (tokens) {
assertTrue("Callback not called the first time", callbackCalled.get(0));
assertEquals("State is not connected", ConnectionState.connected, stateChange.current);
tokens[0] = ablyRealtime.auth.getTokenDetails().token;
}
}
Expand All @@ -365,12 +370,13 @@ public void onConnectionStateChanged(ConnectionStateChange stateChange) {
ablyRealtime.connection.on(ConnectionEvent.update, new ConnectionStateListener() {
@Override
public void onConnectionStateChanged(ConnectionStateChange state) {
assertTrue("Callback not called the second time", callbackCalled.get(1));
assertEquals("Callback not called 2 times", callbackCalled.size(), 2);
assertNotEquals("Token should not be the same", tokens[0], ablyRealtime.auth.getTokenDetails().token);
assertTrue("Auth protocol message has not been received", authMessages[0]);
updateEvents[0] = true;
ablyRealtime.close();
synchronized (tokens) {
assertTrue("Callback not called the second time", callbackCalled.get(1));
assertNotEquals("Token should not be the same", ablyRealtime.auth.getTokenDetails().token, tokens[0]);
assertTrue("Auth protocol message has not been received", authMessages[0]);
updateEvents[0] = true;
ablyRealtime.close();
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,34 +796,18 @@ static class MessagesEncodingDataItem {
}

@Test

public void reject_invalid_message_data() throws AblyException {
HashMap<String, Integer> data = new HashMap<String, Integer>();
Message message = new Message("event", data);
Log.LogHandler originalLogHandler = Log.handler;
int originalLogLevel = Log.level;
Log.setLevel(Log.DEBUG);
final ArrayList<LogLine> capturedLog = new ArrayList<>();
Log.setHandler(new Log.LogHandler() {
@Override
public void println(int severity, String tag, String msg, Throwable tr) {
capturedLog.add(new LogLine(severity, tag, msg, tr));
}
});

try {
message.encode(null);
fail("reject_invalid_message_data: Expected AblyException to be thrown.");
} catch(AblyException e) {
assertEquals(null, message.encoding);
assertEquals(data, message.data);
assertEquals(1, capturedLog.size());
LogLine capturedLine = capturedLog.get(0);
assertTrue(capturedLine.tag.contains("ably"));
assertTrue(capturedLine.msg.contains("Message data must be either `byte[]`, `String` or `JSONElement`; implicit coercion of other types to String is deprecated"));
} catch(Throwable t) {
fail("reject_invalid_message_data: Unexpected exception");
} finally {
Log.setHandler(originalLogHandler);
Log.setLevel(originalLogLevel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ public void resume_publish_reenter_when_resume_failed() throws AblyException {
options.realtimeRequestTimeout = 2000L;

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

/* Wait for the connection to go stale, then reconnect */
try {
Thread.sleep(waitInDisconnectedState);
} catch (InterruptedException e) {
}

//enter next 3 clients
for (int i = 0; i < 3; i++) {
senderChannel.presence.enterClient(clients[i+3],null,presenceCompletion.add());
Expand All @@ -1045,6 +1039,13 @@ public void onConnectionStateChanged(ConnectionStateChange state) {
for (int i = 0; i < 3; i++) {
senderChannel.presence.enterClient(clients[i+6],null,presenceCompletion.add());
}

/* Wait for the connection to go stale, then reconnect */
try {
Thread.sleep(waitInDisconnectedState);
} catch (InterruptedException e) {
}

//now let's unblock the ack nacks and reconnect
mockWebsocketFactory.blockReceiveProcessing(message -> false);
/* Wait for the connection to go stale, then reconnect */
Expand Down

0 comments on commit bbbcb4d

Please sign in to comment.