1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-05 20:51:07 +01:00

SMACK-412 Split the ping implementation to a server ping to replace keepalive and a simplified ping manager for manual pings of other entities.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13569 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
rcollier 2013-03-19 02:37:36 +00:00
parent a55b54f20b
commit aab1dcdabe
19 changed files with 749 additions and 651 deletions

View file

@ -15,24 +15,127 @@
*/
package org.jivesoftware.smackx.ping;
import static org.junit.Assert.assertEquals;
import org.jivesoftware.smackx.ping.packet.Ping;
import org.jivesoftware.smackx.ping.packet.Pong;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.TestUtils;
import org.jivesoftware.smack.ThreadedDummyConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.ping.packet.Ping;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.junit.Test;
import static org.junit.Assert.*;
public class PingPongTest {
@Test
public void createPongfromPingTest() {
Ping ping = new Ping("from@sender.local/resourceFrom", "to@receiver.local/resourceTo");
public void checkSendingPing() throws Exception {
DummyConnection con = new DummyConnection();
PingManager pinger = new PingManager(con);
pinger.ping("test@myserver.com");
// create a pong from a ping
Pong pong = new Pong(ping);
assertEquals(pong.getFrom(), ping.getTo());
assertEquals(pong.getTo(), ping.getFrom());
assertEquals(pong.getPacketID(), ping.getPacketID());
Packet sentPacket = con.getSentPacket();
assertTrue(sentPacket instanceof Ping);
}
@Test
public void checkSuccessfulPing() throws Exception {
ThreadedDummyConnection con = new ThreadedDummyConnection();
PingManager pinger = new PingManager(con);
boolean pingSuccess = pinger.ping("test@myserver.com");
assertTrue(pingSuccess);
}
/**
* DummyConnection will not reply so it will timeout.
* @throws Exception
*/
@Test
public void checkFailedPingOnTimeout() throws Exception {
DummyConnection con = new DummyConnection();
PingManager pinger = new PingManager(con);
boolean pingSuccess = pinger.ping("test@myserver.com");
assertFalse(pingSuccess);
}
/**
* DummyConnection will not reply so it will timeout.
* @throws Exception
*/
@Test
public void checkFailedPingError() throws Exception {
ThreadedDummyConnection con = new ThreadedDummyConnection();
//@formatter:off
String reply =
"<iq type='error' id='qrzSp-16' to='test@myserver.com'>" +
"<ping xmlns='urn:xmpp:ping'/>" +
"<error type='cancel'>" +
"<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
"</error>" +
"</iq>";
//@formatter:on
IQ serviceUnavailable = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
con.addIQReply(serviceUnavailable);
PingManager pinger = new PingManager(con);
boolean pingSuccess = pinger.ping("test@myserver.com");
assertFalse(pingSuccess);
}
@Test
public void checkSuccessfulDiscoRequest() throws Exception {
ThreadedDummyConnection con = new ThreadedDummyConnection();
DiscoverInfo info = new DiscoverInfo();
info.addFeature(Ping.NAMESPACE);
//@formatter:off
String reply =
"<iq type='result' id='qrzSp-16' to='test@myserver.com'>" +
"<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc' name='Pidgin'/>" +
"<feature var='urn:xmpp:ping'/>" +
"</query></iq>";
//@formatter:on
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
con.addIQReply(discoReply);
PingManager pinger = new PingManager(con);
boolean pingSupported = pinger.isPingSupported("test@myserver.com");
assertTrue(pingSupported);
}
@Test
public void checkUnuccessfulDiscoRequest() throws Exception {
ThreadedDummyConnection con = new ThreadedDummyConnection();
DiscoverInfo info = new DiscoverInfo();
info.addFeature(Ping.NAMESPACE);
//@formatter:off
String reply =
"<iq type='result' id='qrzSp-16' to='test@myserver.com'>" +
"<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc' name='Pidgin'/>" +
"<feature var='urn:xmpp:noping'/>" +
"</query></iq>";
//@formatter:on
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
con.addIQReply(discoReply);
PingManager pinger = new PingManager(con);
boolean pingSupported = pinger.isPingSupported("test@myserver.com");
assertFalse(pingSupported);
}
}