From bac486e36aedb4a05bcd68dd12516da4d7b6e99c Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Wed, 31 Jan 2024 17:45:19 +0530 Subject: [PATCH] Updating complex tests where reflection is used --- .../java/io/ably/lib/test/common/Helpers.java | 12 +++++++ .../test/realtime/ConnectionManagerTest.java | 36 ++++++++----------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/src/test/java/io/ably/lib/test/common/Helpers.java b/lib/src/test/java/io/ably/lib/test/common/Helpers.java index dd3adf10e..5f5534f68 100644 --- a/lib/src/test/java/io/ably/lib/test/common/Helpers.java +++ b/lib/src/test/java/io/ably/lib/test/common/Helpers.java @@ -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 */ diff --git a/lib/src/test/java/io/ably/lib/test/realtime/ConnectionManagerTest.java b/lib/src/test/java/io/ably/lib/test/realtime/ConnectionManagerTest.java index 97910f0e8..c1183b69d 100644 --- a/lib/src/test/java/io/ably/lib/test/realtime/ConnectionManagerTest.java +++ b/lib/src/test/java/io/ably/lib/test/realtime/ConnectionManagerTest.java @@ -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); } }