1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

SMACK-412 Abstracted the keepalive implementation and set the thread to start and stop on demand.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13610 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
rcollier 2013-04-16 01:39:17 +00:00
parent b4432d7627
commit 3a4b05ac00
6 changed files with 213 additions and 109 deletions

View file

@ -49,6 +49,7 @@ public class DummyConnection extends Connection {
private boolean authenticated = false;
private boolean anonymous = false;
private boolean reconnect = false;
private String user;
private String connectionID;
@ -71,6 +72,12 @@ public class DummyConnection extends Connection {
@Override
public void connect() throws XMPPException {
connectionID = "dummy-" + new Random(new Date().getTime()).nextInt();
if (reconnect) {
for (ConnectionListener listener : getConnectionListeners()) {
listener.reconnectionSuccessful();
}
}
}
@Override
@ -80,6 +87,11 @@ public class DummyConnection extends Connection {
roster = null;
authenticated = false;
anonymous = false;
for (ConnectionListener listener : getConnectionListeners()) {
listener.connectionClosed();
}
reconnect = true;
}
@Override

View file

@ -1,4 +1,4 @@
package org.jivesoftware.smack.ping;
package org.jivesoftware.smack.keepalive;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.junit.Assert.assertEquals;
@ -17,8 +17,10 @@ import org.jivesoftware.smack.TestUtils;
import org.jivesoftware.smack.ThreadedDummyConnection;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.keepalive.KeepAliveManager;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.ping.PingFailedListener;
import org.jivesoftware.smack.ping.packet.Ping;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.junit.After;
@ -40,6 +42,7 @@ public class KeepaliveTest {
@Before
public void resetProperties()
{
SmackConfiguration.setKeepAliveInterval(-1);
originalTimeout = SmackConfiguration.getPacketReplyTimeout();
SmackConfiguration.setPacketReplyTimeout(1000);
}
@ -56,7 +59,7 @@ public class KeepaliveTest {
public void validatePingStanzaXML() throws Exception {
// @formatter:off
String control = "<iq to='juliet@capulet.lit/balcony' id='s2c1' type='get'>"
+ "<ping xmlns='urn:xmpp:ping'/>" + "</iq>";
+ "<ping xmlns='urn:xmpp:ping'/></iq>";
// @formatter:on
Ping ping = new Ping(TO);
@ -65,29 +68,6 @@ public class KeepaliveTest {
assertXMLEqual(control, ping.toXML());
}
@Test
public void checkProvider() throws Exception {
// @formatter:off
String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>"
+ "<ping xmlns='urn:xmpp:ping'/>" + "</iq>";
// @formatter:on
DummyConnection con = new DummyConnection();
IQ pingRequest = PacketParserUtils.parseIQ(TestUtils.getIQParser(control), con);
assertTrue(pingRequest instanceof Ping);
con.processPacket(pingRequest);
Packet pongPacket = con.getSentPacket();
assertTrue(pongPacket instanceof IQ);
IQ pong = (IQ) pongPacket;
assertEquals("juliet@capulet.lit/balcony", pong.getFrom());
assertEquals("capulet.lit", pong.getTo());
assertEquals("s2c1", pong.getPacketID());
assertEquals(IQ.Type.RESULT, pong.getType());
}
@Test
public void serverPingFailSingleConnection() throws Exception {
DummyConnection connection = getConnection();
@ -138,7 +118,7 @@ public class KeepaliveTest {
}
private void addPingFailedListener(DummyConnection con, final CountDownLatch latch) {
ServerPingManager manager = ServerPingManager.getInstanceFor(con);
KeepAliveManager manager = KeepAliveManager.getInstanceFor(con);
manager.addPingFailedListener(new PingFailedListener() {
@Override
public void pingFailed() {
@ -149,7 +129,7 @@ public class KeepaliveTest {
private DummyConnection getConnection() {
DummyConnection con = new DummyConnection();
ServerPingManager mgr = ServerPingManager.getInstanceFor(con);
KeepAliveManager mgr = KeepAliveManager.getInstanceFor(con);
mgr.setPingInterval(PING_MINIMUM);
return con;
@ -157,7 +137,7 @@ public class KeepaliveTest {
private ThreadedDummyConnection getThreadedConnection() {
ThreadedDummyConnection con = new ThreadedDummyConnection();
ServerPingManager mgr = ServerPingManager.getInstanceFor(con);
KeepAliveManager mgr = KeepAliveManager.getInstanceFor(con);
mgr.setPingInterval(PING_MINIMUM);
return con;

View file

@ -38,10 +38,37 @@ public class PingTest {
threadedCon = new ThreadedDummyConnection();
}
@Test
public void checkProvider() throws Exception {
// @formatter:off
String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>"
+ "<ping xmlns='urn:xmpp:ping'/>"
+ "</iq>";
// @formatter:on
DummyConnection con = new DummyConnection();
// Enable ping for this connection
PingManager.getInstanceFor(con);
IQ pingRequest = PacketParserUtils.parseIQ(TestUtils.getIQParser(control), con);
assertTrue(pingRequest instanceof Ping);
con.processPacket(pingRequest);
Packet pongPacket = con.getSentPacket();
assertTrue(pongPacket instanceof IQ);
IQ pong = (IQ) pongPacket;
assertEquals("juliet@capulet.lit/balcony", pong.getFrom());
assertEquals("capulet.lit", pong.getTo());
assertEquals("s2c1", pong.getPacketID());
assertEquals(IQ.Type.RESULT, pong.getType());
}
@Test
public void checkSendingPing() throws Exception {
dummyCon = new DummyConnection();
PingManager pinger = new PingManager(dummyCon);
PingManager pinger = PingManager.getInstanceFor(dummyCon);
pinger.ping("test@myserver.com");
Packet sentPacket = dummyCon.getSentPacket();
@ -54,7 +81,7 @@ public class PingTest {
public void checkSuccessfulPing() throws Exception {
threadedCon = new ThreadedDummyConnection();
PingManager pinger = new PingManager(threadedCon);
PingManager pinger = PingManager.getInstanceFor(threadedCon);
boolean pingSuccess = pinger.ping("test@myserver.com");
@ -69,7 +96,7 @@ public class PingTest {
@Test
public void checkFailedPingOnTimeout() throws Exception {
dummyCon = new DummyConnection();
PingManager pinger = new PingManager(dummyCon);
PingManager pinger = PingManager.getInstanceFor(dummyCon);
boolean pingSuccess = pinger.ping("test@myserver.com");
@ -96,7 +123,7 @@ public class PingTest {
IQ serviceUnavailable = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), threadedCon);
threadedCon.addIQReply(serviceUnavailable);
PingManager pinger = new PingManager(threadedCon);
PingManager pinger = PingManager.getInstanceFor(threadedCon);
boolean pingSuccess = pinger.ping("test@myserver.com");
@ -106,7 +133,7 @@ public class PingTest {
@Test
public void checkPingToServerSuccess() throws Exception {
ThreadedDummyConnection con = new ThreadedDummyConnection();
PingManager pinger = new PingManager(con);
PingManager pinger = PingManager.getInstanceFor(con);
boolean pingSuccess = pinger.pingMyServer();
@ -132,7 +159,7 @@ public class PingTest {
IQ serviceUnavailable = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
con.addIQReply(serviceUnavailable);
PingManager pinger = new PingManager(con);
PingManager pinger = PingManager.getInstanceFor(con);
boolean pingSuccess = pinger.pingMyServer();
@ -142,7 +169,7 @@ public class PingTest {
@Test
public void checkPingToServerTimeout() throws Exception {
DummyConnection con = new DummyConnection();
PingManager pinger = new PingManager(con);
PingManager pinger = PingManager.getInstanceFor(con);
boolean pingSuccess = pinger.pingMyServer();
@ -165,7 +192,7 @@ public class PingTest {
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
con.addIQReply(discoReply);
PingManager pinger = new PingManager(con);
PingManager pinger = PingManager.getInstanceFor(con);
boolean pingSupported = pinger.isPingSupported("test@myserver.com");
assertTrue(pingSupported);
@ -187,7 +214,7 @@ public class PingTest {
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
con.addIQReply(discoReply);
PingManager pinger = new PingManager(con);
PingManager pinger = PingManager.getInstanceFor(con);
boolean pingSupported = pinger.isPingSupported("test@myserver.com");
assertFalse(pingSupported);