Skip to content

Commit

Permalink
Updating complex tests where reflection is used
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Jan 31, 2024
1 parent e93f0ca commit bac486e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
12 changes: 12 additions & 0 deletions lib/src/test/java/io/ably/lib/test/common/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,18 @@ public void setField(String fieldName, long value) {
}
}

public long getField(String fieldName) {
Field connectionStateField = null;
try {
connectionStateField = ConnectionManager.class.getDeclaredField(fieldName);
connectionStateField.setAccessible(true);
return connectionStateField.getLong(connectionManager);
} catch (NoSuchFieldException | IllegalAccessException e) {
fail("Unexpected exception in checking connectionStateTtl");
}
return 0;
}

/**
* Suppress automatic retries by the connection manager and disconnect
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,29 +529,23 @@ public void run() {
@Test
public void connection_details_has_ttl() throws AblyException {
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
opts.autoConnect = false;
try (AblyRealtime ably = new AblyRealtime(opts)) {
final boolean[] callbackWasRun = new boolean[1];
ably.connection.on(ConnectionEvent.connected, new ConnectionStateListener() {
@Override
public void onConnectionStateChanged(ConnectionStateChange state) {
synchronized(callbackWasRun) {
callbackWasRun[0] = true;
try {
Field field = ably.connection.connectionManager.getClass().getDeclaredField("connectionStateTtl");
field.setAccessible(true);
assertEquals("Verify connectionStateTtl has the default value", field.get(ably.connection.connectionManager), 120000L);
} catch (NoSuchFieldException|IllegalAccessException e) {
fail("Unexpected exception in checking connectionStateTtl");
}
callbackWasRun.notify();
}
}
});
Helpers.MutableConnectionManager connectionManager = new Helpers.MutableConnectionManager(ably);

synchronized (callbackWasRun) {
try { callbackWasRun.wait(); } catch(InterruptedException ie) {}
assertTrue("Connected callback was not run", callbackWasRun[0]);
}
// connStateTtl set to default value
long connStateTtl = connectionManager.getField("connectionStateTtl");
assertEquals(Defaults.connectionStateTtl, connStateTtl);

connectionManager.setField("connectionStateTtl", 8000L);
long oldConnStateTtl = connectionManager.getField("connectionStateTtl");
assertEquals(8000L, oldConnStateTtl);

ably.connect();
new ConnectionWaiter(ably.connection).waitFor(ConnectionState.connected);
long newConnStateTtl = connectionManager.getField("connectionStateTtl");
// connStateTtl set by server to 120s
assertEquals(120000L, newConnStateTtl);
}
}

Expand Down

0 comments on commit bac486e

Please sign in to comment.