-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add connection freshness check #390
Changes from 1 commit
664c5e6
41b28a1
2b15846
dbeb79a
aa24aaa
ec4cb43
b16bdec
c75d55e
cc38a40
9efc78a
6a2c3e4
0a1c622
6892ecc
0a14d26
77f3cf8
b1b9085
3a868ee
09d76b2
79d490b
f94e877
6ce56a8
056edff
6ab31f7
0521e14
3e41787
b681cd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
import static org.junit.Assert.fail; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
|
@@ -30,6 +32,7 @@ | |
import io.ably.lib.test.common.Helpers; | ||
import io.ably.lib.test.common.ParameterizedTest; | ||
import io.ably.lib.test.common.Helpers.ConnectionWaiter; | ||
import io.ably.lib.test.common.Helpers.ChannelWaiter; | ||
import io.ably.lib.transport.ConnectionManager; | ||
import io.ably.lib.transport.Defaults; | ||
import io.ably.lib.types.AblyException; | ||
|
@@ -562,37 +565,49 @@ public void onConnectionStateChanged(ConnectionStateChange state) { | |
|
||
/* Attach to channel */ | ||
final Channel channel = ably.channels.get(channelName); | ||
channel.on(new ChannelStateListener() { | ||
@Override | ||
public void onChannelStateChanged(ChannelStateChange stateChange) { | ||
System.out.println("======================= state is " + stateChange.current); | ||
} | ||
}); | ||
channel.once(ChannelState.attached, new ChannelStateListener() { | ||
@Override | ||
public void onChannelStateChanged(ChannelStateChange stateChange) { | ||
System.out.println("Channel attached for the first time"); | ||
assertEquals("Channel is attached", stateChange.current, ChannelState.attached); | ||
} | ||
}); | ||
|
||
/* attach and wait for the channel to be attached */ | ||
channel.attach(); | ||
(new Helpers.ChannelWaiter(channel)).waitFor(ChannelState.attached); | ||
ChannelWaiter channelWaiter = new Helpers.ChannelWaiter(channel); | ||
channelWaiter.waitFor(ChannelState.attached); | ||
|
||
ably.connection.once(ConnectionEvent.disconnected, new ConnectionStateListener() { | ||
@Override | ||
public void onConnectionStateChanged(ConnectionStateChange state) { | ||
synchronized (ably.connection.connectionManager) { | ||
try { | ||
/* The client will try to reconnect right away after disconnection. | ||
* We want it to stay into a disconnected state long enough | ||
* so that the connection becomes stale. | ||
*/ | ||
ably.connection.connectionManager.wait(waitInDisconnectedState); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}); | ||
/* suppress automatic retries by the connection manager */ | ||
try { | ||
Method method = ably.connection.connectionManager.getClass().getDeclaredMethod("disconnectAndSuppressRetries"); | ||
method.setAccessible(true); | ||
method.invoke(ably.connection.connectionManager); | ||
} catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException e) { | ||
fail("Unexpected exception in suppressing retries"); | ||
} | ||
|
||
ably.connection.connectionManager.requestState(ConnectionState.disconnected); | ||
connectionWaiter.waitFor(ConnectionState.disconnected); | ||
assertEquals("Disconnected state was not reached", ConnectionState.disconnected, ably.connection.state); | ||
System.out.println("=============================== DISCONNECTED "); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are these for lines for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That were meant for debugging but have been deleted in this commit 6ab31f7#diff-4cfebe2518c34c9f3975cde19f3ced08L598 |
||
|
||
try { Thread.sleep(waitInDisconnectedState); } catch(InterruptedException e) {} | ||
|
||
channel.on(ChannelEvent.update, new ChannelStateListener() { | ||
ably.connection.connect(); | ||
connectionWaiter.waitFor(ConnectionState.connected); | ||
|
||
/* verify a new connection was assigned */ | ||
assertNotEquals("A new connection was created", firstConnectionId, ably.connection.id); | ||
System.out.println("=============================== connection is new " + firstConnectionId + " " + ably.connection.id); | ||
System.out.println("channel state is " + channel.state); | ||
|
||
channel.once(ChannelEvent.attached, new ChannelStateListener() { | ||
@Override | ||
public void onChannelStateChanged(ChannelStateChange stateChange) { | ||
System.out.println("Channel reattached after a new connection has been established"); | ||
|
@@ -603,7 +618,10 @@ public void onChannelStateChanged(ChannelStateChange stateChange) { | |
ably.close(); | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are not waiting for those to close, this entire section could easily never be run as part of this test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mattheworiordan done 77f3cf8 |
||
(new Helpers.ChannelWaiter(channel)).waitFor(ChannelState.attached); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you please sort out your IDE/editor? It's getting tiring spending time commenting on whitespace I am afraid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this is an old commit and is now fixed. |
||
/* */ | ||
(new Helpers.ConnectionWaiter(ably.connection)).waitFor(ConnectionState.closed); | ||
|
||
} catch (AblyException e) { | ||
e.printStackTrace(); | ||
fail("init0: Unexpected exception instantiating library"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not explicitly awaiting the channel attached, and you cannot be sure the
onChannelStateChanged
handler ran with this code path.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattheworiordan done 0a14d26