2006-09-14 21:13:35 +02:00
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.test.SmackTestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the connection and reconnection mechanism
|
|
|
|
*
|
|
|
|
* @author Francisco Vives
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ReconnectionTest extends SmackTestCase {
|
|
|
|
|
|
|
|
public ReconnectionTest(String arg0) {
|
|
|
|
super(arg0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests an automatic reconnection.
|
|
|
|
* Simulates a connection error and then waits until gets reconnected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void testAutomaticReconnection() throws Exception {
|
|
|
|
XMPPConnection connection = getConnection(0);
|
|
|
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
|
|
|
|
connection.addConnectionListener(listener);
|
|
|
|
|
|
|
|
// Simulates an error in the connection
|
|
|
|
connection.packetReader.notifyConnectionError(new Exception("Simulated Error"));
|
|
|
|
Thread.sleep(12000);
|
|
|
|
// After 10 seconds, the reconnection manager must reestablishes the connection
|
|
|
|
assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
|
|
|
|
true, listener.reconnected);
|
|
|
|
assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
|
2006-11-10 20:06:33 +01:00
|
|
|
listener.attemptsNotifications);
|
|
|
|
assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
|
|
|
|
listener.remainingSeconds);
|
|
|
|
|
|
|
|
// Executes some server interaction testing the connection
|
|
|
|
executeSomeServerInteraction(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testAutomaticReconnectionWithCompression() throws Exception {
|
|
|
|
// Create the configuration for this new connection
|
|
|
|
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
|
|
|
|
config.setCompressionEnabled(true);
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
|
|
|
|
XMPPConnection connection = new XMPPConnection(config);
|
|
|
|
// Connect to the server
|
|
|
|
connection.connect();
|
|
|
|
// Log into the server
|
2013-02-07 04:47:49 +01:00
|
|
|
connection.login(getUsername(0), getPassword(0), "MyOtherResource");
|
2006-11-10 20:06:33 +01:00
|
|
|
|
|
|
|
assertTrue("Failed to use compression", connection.isUsingCompression());
|
|
|
|
|
2007-07-23 04:06:30 +02:00
|
|
|
// Executes some server interaction testing the connection
|
|
|
|
executeSomeServerInteraction(connection);
|
|
|
|
|
2006-11-10 20:06:33 +01:00
|
|
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
|
|
|
|
connection.addConnectionListener(listener);
|
|
|
|
|
|
|
|
// Simulates an error in the connection
|
|
|
|
connection.packetReader.notifyConnectionError(new Exception("Simulated Error"));
|
|
|
|
Thread.sleep(12000);
|
|
|
|
// After 10 seconds, the reconnection manager must reestablishes the connection
|
|
|
|
assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
|
|
|
|
true, listener.reconnected);
|
|
|
|
assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
|
2006-09-14 21:13:35 +02:00
|
|
|
listener.attemptsNotifications);
|
|
|
|
assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
|
|
|
|
listener.remainingSeconds);
|
|
|
|
|
|
|
|
// Executes some server interaction testing the connection
|
|
|
|
executeSomeServerInteraction(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a manual reconnection.
|
|
|
|
* Simulates a connection error, disables the reconnection mechanism and then reconnects.
|
|
|
|
*/
|
|
|
|
public void testManualReconnectionWithCancelation() throws Exception {
|
|
|
|
XMPPConnection connection = getConnection(0);
|
|
|
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
|
|
|
|
connection.addConnectionListener(listener);
|
|
|
|
|
|
|
|
// Produces a connection error
|
|
|
|
connection.packetReader.notifyConnectionError(new Exception("Simulated Error"));
|
|
|
|
assertEquals(
|
|
|
|
"An error occurs but the ConnectionListener.connectionClosedOnError(e) was not notified",
|
|
|
|
true, listener.connectionClosedOnError);
|
|
|
|
Thread.sleep(1000);
|
|
|
|
// Cancels the automatic reconnection
|
|
|
|
connection.getConfiguration().setReconnectionAllowed(false);
|
|
|
|
// Waits for a reconnection that must not happened.
|
|
|
|
Thread.sleep(10500);
|
|
|
|
// Cancels the automatic reconnection
|
|
|
|
assertEquals("The connection was stablished but it was not allowed to", false,
|
|
|
|
listener.reconnected);
|
|
|
|
|
|
|
|
// Makes a manual reconnection from an error terminated connection without reconnection
|
|
|
|
connection.connect();
|
|
|
|
|
|
|
|
// Executes some server interaction testing the connection
|
|
|
|
executeSomeServerInteraction(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a manual reconnection after a login.
|
|
|
|
* Closes the connection and then reconnects.
|
|
|
|
*/
|
|
|
|
public void testCloseAndManualReconnection() throws Exception {
|
|
|
|
XMPPConnection connection = getConnection(0);
|
|
|
|
String username = connection.getConfiguration().getUsername();
|
|
|
|
String password = connection.getConfiguration().getPassword();
|
|
|
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
|
|
|
|
connection.addConnectionListener(listener);
|
|
|
|
|
|
|
|
// Produces a normal disconnection
|
|
|
|
connection.disconnect();
|
|
|
|
assertEquals("ConnectionListener.connectionClosed() was not notified",
|
|
|
|
true, listener.connectionClosed);
|
|
|
|
// Waits 10 seconds waiting for a reconnection that must not happened.
|
|
|
|
Thread.sleep(12200);
|
|
|
|
assertEquals("The connection was stablished but it was not allowed to", false,
|
|
|
|
listener.reconnected);
|
|
|
|
|
|
|
|
// Makes a manual reconnection
|
|
|
|
connection.connect();
|
|
|
|
connection.login(username, password);
|
|
|
|
|
|
|
|
// Executes some server interaction testing the connection
|
|
|
|
executeSomeServerInteraction(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests a reconnection in a anonymously logged connection.
|
|
|
|
* Closes the connection and then reconnects.
|
|
|
|
*/
|
|
|
|
public void testAnonymousReconnection() throws Exception {
|
|
|
|
XMPPConnection connection = createConnection();
|
|
|
|
connection.connect();
|
|
|
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
|
|
|
|
connection.addConnectionListener(listener);
|
|
|
|
|
|
|
|
// Makes the anounymous login
|
|
|
|
connection.loginAnonymously();
|
|
|
|
|
|
|
|
// Produces a normal disconnection
|
|
|
|
connection.disconnect();
|
|
|
|
assertEquals("ConnectionListener.connectionClosed() was not notified",
|
|
|
|
true, listener.connectionClosed);
|
|
|
|
// Makes a manual reconnection
|
|
|
|
connection.connect();
|
|
|
|
connection.loginAnonymously();
|
|
|
|
assertEquals("Failed the manual connection", true, connection.isAnonymous());
|
|
|
|
}
|
|
|
|
|
2007-01-05 00:00:58 +01:00
|
|
|
private XMPPConnection createXMPPConnection() throws Exception {
|
2006-09-14 21:13:35 +02:00
|
|
|
XMPPConnection connection;
|
|
|
|
// Create the configuration
|
|
|
|
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
|
|
|
|
config.setCompressionEnabled(Boolean.getBoolean("test.compressionEnabled"));
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
2007-01-05 00:00:58 +01:00
|
|
|
connection = new XMPPConnection(config);
|
2006-09-14 21:13:35 +02:00
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute some server interaction in order to test that the regenerated connection works fine.
|
|
|
|
*/
|
|
|
|
private void executeSomeServerInteraction(XMPPConnection connection) throws XMPPException {
|
|
|
|
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(connection);
|
|
|
|
privacyManager.getPrivacyLists();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int getMaxConnections() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class XMPPConnectionTestListener implements ConnectionListener {
|
|
|
|
|
|
|
|
// Variables to support listener notifications verification
|
|
|
|
private boolean connectionClosed = false;
|
|
|
|
private boolean connectionClosedOnError = false;
|
|
|
|
private boolean reconnected = false;
|
|
|
|
private boolean reconnectionFailed = false;
|
|
|
|
private int remainingSeconds = 0;
|
|
|
|
private int attemptsNotifications = 0;
|
|
|
|
private boolean reconnectionCanceled = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods to test the listener.
|
|
|
|
*/
|
|
|
|
public void connectionClosed() {
|
|
|
|
connectionClosed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void connectionClosedOnError(Exception e) {
|
|
|
|
connectionClosedOnError = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionCanceled() {
|
|
|
|
reconnectionCanceled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectingIn(int seconds) {
|
|
|
|
attemptsNotifications = attemptsNotifications + 1;
|
|
|
|
remainingSeconds = seconds;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-11-14 22:58:03 +01:00
|
|
|
public void reconnectionSuccessful() {
|
2006-09-14 21:13:35 +02:00
|
|
|
reconnected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionFailed(Exception error) {
|
|
|
|
reconnectionFailed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|