2013-02-07 15:19:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-02-17 23:58:40 +01:00
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2013-04-22 03:03:43 +02:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.test.SmackTestCase;
|
2013-04-22 03:03:43 +02:00
|
|
|
import org.jivesoftware.smackx.ping.PingManager;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the connection and reconnection mechanism
|
|
|
|
*
|
|
|
|
* @author Francisco Vives
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ReconnectionTest extends SmackTestCase {
|
2013-04-22 03:03:43 +02:00
|
|
|
|
|
|
|
private static final long MIN_RECONNECT_WAIT = 17; // Seconds
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
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 {
|
2013-04-22 03:03:43 +02:00
|
|
|
XMPPConnection connection = getConnection(0);
|
2014-02-17 23:58:40 +01:00
|
|
|
CountDownLatch latch = new CountDownLatch(1);
|
2013-04-22 03:03:43 +02:00
|
|
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
|
2014-02-17 23:58:40 +01:00
|
|
|
connection.addConnectionListener(listener);
|
|
|
|
|
|
|
|
// Simulates an error in the connection
|
|
|
|
connection.notifyConnectionError(new Exception("Simulated Error"));
|
2013-04-22 03:03:43 +02:00
|
|
|
latch.await(MIN_RECONNECT_WAIT, TimeUnit.SECONDS);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// After 10 seconds, the reconnection manager must reestablishes the connection
|
|
|
|
assertEquals("The ConnectionListener.connectionStablished() notification was not fired", true, listener.reconnected);
|
|
|
|
assertTrue("The ReconnectionManager algorithm has reconnected without waiting at least 5 seconds", listener.attemptsNotifications > 0);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
connection.login(getUsername(0), getPassword(0), "MyOtherResource");
|
|
|
|
|
|
|
|
assertTrue("Failed to use compression", connection.isUsingCompression());
|
|
|
|
|
|
|
|
// Executes some server interaction testing the connection
|
|
|
|
executeSomeServerInteraction(connection);
|
|
|
|
|
2013-04-22 03:03:43 +02:00
|
|
|
CountDownLatch latch = new CountDownLatch(1);
|
2014-02-17 23:58:40 +01:00
|
|
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
|
|
|
|
connection.addConnectionListener(listener);
|
|
|
|
|
|
|
|
// Simulates an error in the connection
|
|
|
|
connection.notifyConnectionError(new Exception("Simulated Error"));
|
2013-04-22 03:03:43 +02:00
|
|
|
latch.await(MIN_RECONNECT_WAIT, TimeUnit.SECONDS);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// After 10 seconds, the reconnection manager must reestablishes the connection
|
|
|
|
assertEquals("The ConnectionListener.connectionEstablished() notification was not fired", true, listener.reconnected);
|
2013-04-22 03:03:43 +02:00
|
|
|
assertTrue("The ReconnectionManager algorithm has reconnected without waiting at least 5 seconds", listener.attemptsNotifications > 0);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// 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);
|
2013-04-22 03:03:43 +02:00
|
|
|
CountDownLatch latch = new CountDownLatch(1);
|
|
|
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
|
2014-02-17 23:58:40 +01:00
|
|
|
connection.addConnectionListener(listener);
|
|
|
|
|
|
|
|
// Produces a connection error
|
|
|
|
connection.notifyConnectionError(new Exception("Simulated Error"));
|
|
|
|
assertEquals(
|
|
|
|
"An error occurs but the ConnectionListener.connectionClosedOnError(e) was not notified",
|
|
|
|
true, listener.connectionClosedOnError);
|
2013-04-22 03:03:43 +02:00
|
|
|
// Thread.sleep(1000);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// Cancels the automatic reconnection
|
|
|
|
connection.getConfiguration().setReconnectionAllowed(false);
|
|
|
|
// Waits for a reconnection that must not happened.
|
|
|
|
Thread.sleep(MIN_RECONNECT_WAIT * 1000);
|
|
|
|
// Cancels the automatic reconnection
|
|
|
|
assertEquals(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(MIN_RECONNECT_WAIT * 1000);
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
private XMPPConnection createXMPPConnection() throws Exception {
|
|
|
|
XMPPConnection connection;
|
|
|
|
// Create the configuration
|
|
|
|
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
|
|
|
|
config.setCompressionEnabled(Boolean.getBoolean("test.compressionEnabled"));
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
connection = new XMPPConnection(config);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute some server interaction in order to test that the regenerated connection works fine.
|
|
|
|
*/
|
|
|
|
private void executeSomeServerInteraction(XMPPConnection connection) throws XMPPException {
|
|
|
|
PingManager pingManager = PingManager.getInstanceFor(connection);
|
|
|
|
pingManager.pingMyServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int getMaxConnections() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class XMPPConnectionTestListener implements ConnectionListener {
|
|
|
|
|
|
|
|
// Variables to support listener notifications verification
|
|
|
|
private volatile boolean connectionClosed = false;
|
|
|
|
private volatile boolean connectionClosedOnError = false;
|
|
|
|
private volatile boolean reconnected = false;
|
|
|
|
private volatile boolean reconnectionFailed = false;
|
|
|
|
private volatile int remainingSeconds = 0;
|
|
|
|
private volatile int attemptsNotifications = 0;
|
2013-04-22 03:03:43 +02:00
|
|
|
private volatile boolean reconnectionCanceled = false;
|
2014-02-17 23:58:40 +01:00
|
|
|
private CountDownLatch countDownLatch;
|
2013-04-22 03:03:43 +02:00
|
|
|
|
|
|
|
private XMPPConnectionTestListener(CountDownLatch latch) {
|
|
|
|
countDownLatch = latch;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2013-04-22 03:03:43 +02:00
|
|
|
|
|
|
|
private XMPPConnectionTestListener() {
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
* Methods to test the listener.
|
|
|
|
*/
|
2013-04-22 03:03:43 +02:00
|
|
|
public void connectionClosed() {
|
|
|
|
connectionClosed = true;
|
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
if (countDownLatch != null)
|
2013-04-22 03:03:43 +02:00
|
|
|
countDownLatch.countDown();
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void connectionClosedOnError(Exception e) {
|
2013-04-22 03:03:43 +02:00
|
|
|
connectionClosedOnError = true;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionCanceled() {
|
|
|
|
reconnectionCanceled = true;
|
2013-04-22 03:03:43 +02:00
|
|
|
|
|
|
|
if (countDownLatch != null)
|
|
|
|
countDownLatch.countDown();
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectingIn(int seconds) {
|
|
|
|
attemptsNotifications = attemptsNotifications + 1;
|
2013-04-22 03:03:43 +02:00
|
|
|
remainingSeconds = seconds;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionSuccessful() {
|
|
|
|
reconnected = true;
|
2013-04-22 03:03:43 +02:00
|
|
|
|
|
|
|
if (countDownLatch != null)
|
|
|
|
countDownLatch.countDown();
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionFailed(Exception error) {
|
|
|
|
reconnectionFailed = true;
|
2013-04-22 03:03:43 +02:00
|
|
|
|
|
|
|
if (countDownLatch != null)
|
|
|
|
countDownLatch.countDown();
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|