mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-27 00:32:07 +01:00
SMACK-433 Removed Thread.sleep usage for tests
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13621 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
fe2e9cdb76
commit
ce0cb7d491
3 changed files with 283 additions and 272 deletions
|
@ -17,7 +17,11 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smack;
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.jivesoftware.smack.test.SmackTestCase;
|
import org.jivesoftware.smack.test.SmackTestCase;
|
||||||
|
import org.jivesoftware.smackx.ping.PingManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the connection and reconnection mechanism
|
* Tests the connection and reconnection mechanism
|
||||||
|
@ -27,6 +31,8 @@ import org.jivesoftware.smack.test.SmackTestCase;
|
||||||
|
|
||||||
public class ReconnectionTest extends SmackTestCase {
|
public class ReconnectionTest extends SmackTestCase {
|
||||||
|
|
||||||
|
private static final long MIN_RECONNECT_WAIT = 17; // Seconds
|
||||||
|
|
||||||
public ReconnectionTest(String arg0) {
|
public ReconnectionTest(String arg0) {
|
||||||
super(arg0);
|
super(arg0);
|
||||||
}
|
}
|
||||||
|
@ -38,19 +44,17 @@ public class ReconnectionTest extends SmackTestCase {
|
||||||
|
|
||||||
public void testAutomaticReconnection() throws Exception {
|
public void testAutomaticReconnection() throws Exception {
|
||||||
XMPPConnection connection = getConnection(0);
|
XMPPConnection connection = getConnection(0);
|
||||||
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
|
||||||
connection.addConnectionListener(listener);
|
connection.addConnectionListener(listener);
|
||||||
|
|
||||||
// Simulates an error in the connection
|
// Simulates an error in the connection
|
||||||
connection.notifyConnectionError(new Exception("Simulated Error"));
|
connection.notifyConnectionError(new Exception("Simulated Error"));
|
||||||
Thread.sleep(12000);
|
latch.await(MIN_RECONNECT_WAIT, TimeUnit.SECONDS);
|
||||||
|
|
||||||
// After 10 seconds, the reconnection manager must reestablishes the connection
|
// After 10 seconds, the reconnection manager must reestablishes the connection
|
||||||
assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
|
assertEquals("The ConnectionListener.connectionStablished() notification was not fired", true, listener.reconnected);
|
||||||
true, listener.reconnected);
|
assertTrue("The ReconnectionManager algorithm has reconnected without waiting at least 5 seconds", listener.attemptsNotifications > 0);
|
||||||
assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
|
|
||||||
listener.attemptsNotifications);
|
|
||||||
assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
|
|
||||||
listener.remainingSeconds);
|
|
||||||
|
|
||||||
// Executes some server interaction testing the connection
|
// Executes some server interaction testing the connection
|
||||||
executeSomeServerInteraction(connection);
|
executeSomeServerInteraction(connection);
|
||||||
|
@ -73,19 +77,17 @@ public class ReconnectionTest extends SmackTestCase {
|
||||||
// Executes some server interaction testing the connection
|
// Executes some server interaction testing the connection
|
||||||
executeSomeServerInteraction(connection);
|
executeSomeServerInteraction(connection);
|
||||||
|
|
||||||
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
|
||||||
connection.addConnectionListener(listener);
|
connection.addConnectionListener(listener);
|
||||||
|
|
||||||
// Simulates an error in the connection
|
// Simulates an error in the connection
|
||||||
connection.notifyConnectionError(new Exception("Simulated Error"));
|
connection.notifyConnectionError(new Exception("Simulated Error"));
|
||||||
Thread.sleep(12000);
|
latch.await(MIN_RECONNECT_WAIT, TimeUnit.SECONDS);
|
||||||
|
|
||||||
// After 10 seconds, the reconnection manager must reestablishes the connection
|
// After 10 seconds, the reconnection manager must reestablishes the connection
|
||||||
assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
|
assertEquals("The ConnectionListener.connectionEstablished() notification was not fired", true, listener.reconnected);
|
||||||
true, listener.reconnected);
|
assertTrue("The ReconnectionManager algorithm has reconnected without waiting at least 5 seconds", listener.attemptsNotifications > 0);
|
||||||
assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
|
|
||||||
listener.attemptsNotifications);
|
|
||||||
assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
|
|
||||||
listener.remainingSeconds);
|
|
||||||
|
|
||||||
// Executes some server interaction testing the connection
|
// Executes some server interaction testing the connection
|
||||||
executeSomeServerInteraction(connection);
|
executeSomeServerInteraction(connection);
|
||||||
|
@ -97,7 +99,8 @@ public class ReconnectionTest extends SmackTestCase {
|
||||||
*/
|
*/
|
||||||
public void testManualReconnectionWithCancelation() throws Exception {
|
public void testManualReconnectionWithCancelation() throws Exception {
|
||||||
XMPPConnection connection = getConnection(0);
|
XMPPConnection connection = getConnection(0);
|
||||||
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
|
||||||
connection.addConnectionListener(listener);
|
connection.addConnectionListener(listener);
|
||||||
|
|
||||||
// Produces a connection error
|
// Produces a connection error
|
||||||
|
@ -105,14 +108,14 @@ public class ReconnectionTest extends SmackTestCase {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"An error occurs but the ConnectionListener.connectionClosedOnError(e) was not notified",
|
"An error occurs but the ConnectionListener.connectionClosedOnError(e) was not notified",
|
||||||
true, listener.connectionClosedOnError);
|
true, listener.connectionClosedOnError);
|
||||||
Thread.sleep(1000);
|
// Thread.sleep(1000);
|
||||||
|
|
||||||
// Cancels the automatic reconnection
|
// Cancels the automatic reconnection
|
||||||
connection.getConfiguration().setReconnectionAllowed(false);
|
connection.getConfiguration().setReconnectionAllowed(false);
|
||||||
// Waits for a reconnection that must not happened.
|
// Waits for a reconnection that must not happened.
|
||||||
Thread.sleep(10500);
|
Thread.sleep(MIN_RECONNECT_WAIT * 1000);
|
||||||
// Cancels the automatic reconnection
|
// Cancels the automatic reconnection
|
||||||
assertEquals("The connection was stablished but it was not allowed to", false,
|
assertEquals(false, listener.reconnected);
|
||||||
listener.reconnected);
|
|
||||||
|
|
||||||
// Makes a manual reconnection from an error terminated connection without reconnection
|
// Makes a manual reconnection from an error terminated connection without reconnection
|
||||||
connection.connect();
|
connection.connect();
|
||||||
|
@ -137,7 +140,7 @@ public class ReconnectionTest extends SmackTestCase {
|
||||||
assertEquals("ConnectionListener.connectionClosed() was not notified",
|
assertEquals("ConnectionListener.connectionClosed() was not notified",
|
||||||
true, listener.connectionClosed);
|
true, listener.connectionClosed);
|
||||||
// Waits 10 seconds waiting for a reconnection that must not happened.
|
// Waits 10 seconds waiting for a reconnection that must not happened.
|
||||||
Thread.sleep(12200);
|
Thread.sleep(MIN_RECONNECT_WAIT * 1000);
|
||||||
assertEquals("The connection was stablished but it was not allowed to", false,
|
assertEquals("The connection was stablished but it was not allowed to", false,
|
||||||
listener.reconnected);
|
listener.reconnected);
|
||||||
|
|
||||||
|
@ -187,8 +190,8 @@ public class ReconnectionTest extends SmackTestCase {
|
||||||
* Execute some server interaction in order to test that the regenerated connection works fine.
|
* Execute some server interaction in order to test that the regenerated connection works fine.
|
||||||
*/
|
*/
|
||||||
private void executeSomeServerInteraction(XMPPConnection connection) throws XMPPException {
|
private void executeSomeServerInteraction(XMPPConnection connection) throws XMPPException {
|
||||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(connection);
|
PingManager pingManager = PingManager.getInstanceFor(connection);
|
||||||
privacyManager.getPrivacyLists();
|
pingManager.pingMyServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int getMaxConnections() {
|
protected int getMaxConnections() {
|
||||||
|
@ -198,19 +201,29 @@ public class ReconnectionTest extends SmackTestCase {
|
||||||
private class XMPPConnectionTestListener implements ConnectionListener {
|
private class XMPPConnectionTestListener implements ConnectionListener {
|
||||||
|
|
||||||
// Variables to support listener notifications verification
|
// Variables to support listener notifications verification
|
||||||
private boolean connectionClosed = false;
|
private volatile boolean connectionClosed = false;
|
||||||
private boolean connectionClosedOnError = false;
|
private volatile boolean connectionClosedOnError = false;
|
||||||
private boolean reconnected = false;
|
private volatile boolean reconnected = false;
|
||||||
private boolean reconnectionFailed = false;
|
private volatile boolean reconnectionFailed = false;
|
||||||
private int remainingSeconds = 0;
|
private volatile int remainingSeconds = 0;
|
||||||
private int attemptsNotifications = 0;
|
private volatile int attemptsNotifications = 0;
|
||||||
private boolean reconnectionCanceled = false;
|
private volatile boolean reconnectionCanceled = false;
|
||||||
|
private CountDownLatch countDownLatch;
|
||||||
|
|
||||||
|
private XMPPConnectionTestListener(CountDownLatch latch) {
|
||||||
|
countDownLatch = latch;
|
||||||
|
}
|
||||||
|
|
||||||
|
private XMPPConnectionTestListener() {
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Methods to test the listener.
|
* Methods to test the listener.
|
||||||
*/
|
*/
|
||||||
public void connectionClosed() {
|
public void connectionClosed() {
|
||||||
connectionClosed = true;
|
connectionClosed = true;
|
||||||
|
|
||||||
|
if (countDownLatch != null)
|
||||||
|
countDownLatch.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connectionClosedOnError(Exception e) {
|
public void connectionClosedOnError(Exception e) {
|
||||||
|
@ -219,20 +232,28 @@ public class ReconnectionTest extends SmackTestCase {
|
||||||
|
|
||||||
public void reconnectionCanceled() {
|
public void reconnectionCanceled() {
|
||||||
reconnectionCanceled = true;
|
reconnectionCanceled = true;
|
||||||
|
|
||||||
|
if (countDownLatch != null)
|
||||||
|
countDownLatch.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reconnectingIn(int seconds) {
|
public void reconnectingIn(int seconds) {
|
||||||
attemptsNotifications = attemptsNotifications + 1;
|
attemptsNotifications = attemptsNotifications + 1;
|
||||||
remainingSeconds = seconds;
|
remainingSeconds = seconds;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reconnectionSuccessful() {
|
public void reconnectionSuccessful() {
|
||||||
reconnected = true;
|
reconnected = true;
|
||||||
|
|
||||||
|
if (countDownLatch != null)
|
||||||
|
countDownLatch.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reconnectionFailed(Exception error) {
|
public void reconnectionFailed(Exception error) {
|
||||||
reconnectionFailed = true;
|
reconnectionFailed = true;
|
||||||
|
|
||||||
|
if (countDownLatch != null)
|
||||||
|
countDownLatch.countDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ package org.jivesoftware.smack;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Presence;
|
import org.jivesoftware.smack.packet.Presence;
|
||||||
import org.jivesoftware.smack.test.SmackTestCase;
|
import org.jivesoftware.smack.test.SmackTestCase;
|
||||||
|
@ -48,8 +49,6 @@ public class RosterListenerTest extends SmackTestCase {
|
||||||
// add user1 to roster to create roster events stored at XMPP server
|
// add user1 to roster to create roster events stored at XMPP server
|
||||||
inviterRoster.createEntry(getBareJID(inviteeIndex), getUsername(inviteeIndex), null);
|
inviterRoster.createEntry(getBareJID(inviteeIndex), getUsername(inviteeIndex), null);
|
||||||
|
|
||||||
Thread.sleep(500); // wait for XMPP server
|
|
||||||
|
|
||||||
XMPPConnection inviteeConnection = getConnection(inviteeIndex);
|
XMPPConnection inviteeConnection = getConnection(inviteeIndex);
|
||||||
assertFalse("Invitee is already online", inviteeConnection.isConnected());
|
assertFalse("Invitee is already online", inviteeConnection.isConnected());
|
||||||
|
|
||||||
|
@ -80,7 +79,7 @@ public class RosterListenerTest extends SmackTestCase {
|
||||||
// connect after adding the listener
|
// connect after adding the listener
|
||||||
connectAndLogin(inviteeIndex);
|
connectAndLogin(inviteeIndex);
|
||||||
|
|
||||||
Thread.sleep(500); // wait for packets to be processed
|
Thread.sleep(5000); // wait for packets to be processed
|
||||||
|
|
||||||
assertNotNull("inviter is not in roster", inviteeRoster.getEntry(getBareJID(inviterIndex)));
|
assertNotNull("inviter is not in roster", inviteeRoster.getEntry(getBareJID(inviterIndex)));
|
||||||
|
|
||||||
|
|
|
@ -21,12 +21,16 @@
|
||||||
package org.jivesoftware.smack;
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Presence;
|
import org.jivesoftware.smack.packet.Presence;
|
||||||
import org.jivesoftware.smack.test.SmackTestCase;
|
import org.jivesoftware.smack.test.SmackTestCase;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
import org.mockito.internal.util.RemoveFirstLine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the Roster functionality by creating and removing roster entries.
|
* Tests the Roster functionality by creating and removing roster entries.
|
||||||
|
@ -43,6 +47,7 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1. Create entries in roster groups
|
* 1. Create entries in roster groups
|
||||||
* 2. Iterate on the groups and remove the entry from each group
|
* 2. Iterate on the groups and remove the entry from each group
|
||||||
|
@ -52,63 +57,77 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
try {
|
try {
|
||||||
// Add a new roster entry
|
// Add a new roster entry
|
||||||
Roster roster = getConnection(0).getRoster();
|
Roster roster = getConnection(0).getRoster();
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
setupCountdown(latch, roster);
|
||||||
|
|
||||||
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends", "Family" });
|
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends", "Family" });
|
||||||
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
|
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
|
||||||
|
|
||||||
// Wait until the server confirms the new entries
|
waitForCountdown(latch, roster, 2);
|
||||||
long initial = System.currentTimeMillis();
|
|
||||||
while (System.currentTimeMillis() - initial < 2000 && (
|
final CountDownLatch removeLatch = new CountDownLatch(3);
|
||||||
!roster.getPresence(getBareJID(1)).isAvailable() ||
|
RosterListener latchCounter = new RosterListener() {
|
||||||
!roster.getPresence(getBareJID(2)).isAvailable())) {
|
@Override
|
||||||
Thread.sleep(100);
|
public void presenceChanged(Presence presence) {}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public void entriesUpdated(Collection<String> addresses) {
|
||||||
|
removeLatch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesDeleted(Collection<String> addresses) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesAdded(Collection<String> addresses) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
roster.addRosterListener(latchCounter);
|
||||||
|
|
||||||
for (RosterEntry entry : roster.getEntries()) {
|
for (RosterEntry entry : roster.getEntries()) {
|
||||||
for (RosterGroup rosterGroup : entry.getGroups()) {
|
for (RosterGroup rosterGroup : entry.getGroups()) {
|
||||||
rosterGroup.removeEntry(entry);
|
rosterGroup.removeEntry(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Wait up to 2 seconds
|
|
||||||
initial = System.currentTimeMillis();
|
|
||||||
while (System.currentTimeMillis() - initial < 2000 &&
|
|
||||||
(roster.getGroupCount() != 0 &&
|
|
||||||
getConnection(2).getRoster().getEntryCount() != 2)) {
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(
|
removeLatch.await(5, TimeUnit.SECONDS);
|
||||||
"The number of entries in connection 1 should be 1",
|
roster.removeRosterListener(latchCounter);
|
||||||
1,
|
|
||||||
getConnection(1).getRoster().getEntryCount());
|
|
||||||
assertEquals(
|
|
||||||
"The number of groups in connection 1 should be 0",
|
|
||||||
0,
|
|
||||||
getConnection(1).getRoster().getGroupCount());
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("The number of entries in connection 1 should be 1", 1, getConnection(1).getRoster().getEntryCount());
|
||||||
"The number of entries in connection 2 should be 1",
|
assertEquals("The number of groups in connection 1 should be 0", 0, getConnection(1).getRoster().getGroupCount());
|
||||||
1,
|
assertEquals("The number of entries in connection 2 should be 1", 1, getConnection(2).getRoster().getEntryCount());
|
||||||
getConnection(2).getRoster().getEntryCount());
|
assertEquals("The number of groups in connection 2 should be 0", 0, getConnection(2).getRoster().getGroupCount());
|
||||||
assertEquals(
|
assertEquals("The number of entries in connection 0 should be 2", 2, roster.getEntryCount());
|
||||||
"The number of groups in connection 2 should be 0",
|
assertEquals("The number of groups in connection 0 should be 0", 0, roster.getGroupCount());
|
||||||
0,
|
|
||||||
getConnection(2).getRoster().getGroupCount());
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
"The number of entries in connection 0 should be 2",
|
|
||||||
2,
|
|
||||||
roster.getEntryCount());
|
|
||||||
assertEquals(
|
|
||||||
"The number of groups in connection 0 should be 0",
|
|
||||||
0,
|
|
||||||
roster.getGroupCount());
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
finally {
|
}
|
||||||
cleanUpRoster();
|
|
||||||
}
|
private void setupCountdown(final CountDownLatch latch, Roster roster) {
|
||||||
|
roster.addRosterListener(new RosterListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void presenceChanged(Presence presence) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesUpdated(Collection<String> addresses) {
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesDeleted(Collection<String> addresses) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesAdded(Collection<String> addresses) {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitForCountdown(CountDownLatch latch, Roster roster, int entryCount) throws InterruptedException {
|
||||||
|
latch.await(5, TimeUnit.SECONDS);
|
||||||
|
assertEquals(entryCount, roster.getEntryCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -119,50 +138,30 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
public void testDeleteAllRosterEntries() throws Exception {
|
public void testDeleteAllRosterEntries() throws Exception {
|
||||||
// Add a new roster entry
|
// Add a new roster entry
|
||||||
Roster roster = getConnection(0).getRoster();
|
Roster roster = getConnection(0).getRoster();
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
setupCountdown(latch, roster);
|
||||||
|
|
||||||
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends" });
|
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends" });
|
||||||
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
|
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
|
||||||
|
|
||||||
// Wait up to 2 seconds to receive new roster contacts
|
waitForCountdown(latch, roster, 2);
|
||||||
long initial = System.currentTimeMillis();
|
|
||||||
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 2) {
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals("Wrong number of entries in connection 0", 2, roster.getEntryCount());
|
CountDownLatch removeLatch = new CountDownLatch(2);
|
||||||
|
RosterListener latchCounter = new RemovalListener(removeLatch);
|
||||||
// Wait up to 2 seconds to receive presences of the new roster contacts
|
roster.addRosterListener(latchCounter);
|
||||||
initial = System.currentTimeMillis();
|
|
||||||
while (System.currentTimeMillis() - initial < 5000 &&
|
|
||||||
(!roster.getPresence(getBareJID(1)).isAvailable() ||
|
|
||||||
!roster.getPresence(getBareJID(2)).isAvailable()))
|
|
||||||
{
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
assertTrue("Presence not received", roster.getPresence(getBareJID(1)).isAvailable());
|
|
||||||
assertTrue("Presence not received", roster.getPresence(getBareJID(2)).isAvailable());
|
|
||||||
|
|
||||||
for (RosterEntry entry : roster.getEntries()) {
|
for (RosterEntry entry : roster.getEntries()) {
|
||||||
roster.removeEntry(entry);
|
roster.removeEntry(entry);
|
||||||
Thread.sleep(250);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait up to 2 seconds to receive roster removal notifications
|
removeLatch.await(5, TimeUnit.SECONDS);
|
||||||
initial = System.currentTimeMillis();
|
roster.removeRosterListener(latchCounter);
|
||||||
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 0) {
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount());
|
assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount());
|
||||||
assertEquals("Wrong number of groups in connection 0", 0, roster.getGroupCount());
|
assertEquals("Wrong number of groups in connection 0", 0, roster.getGroupCount());
|
||||||
|
assertEquals("Wrong number of entries in connection 1", 0, getConnection(1).getRoster().getEntryCount());
|
||||||
assertEquals(
|
assertEquals("Wrong number of groups in connection 1", 0, getConnection(1).getRoster().getGroupCount());
|
||||||
"Wrong number of entries in connection 1",
|
|
||||||
0,
|
|
||||||
getConnection(1).getRoster().getEntryCount());
|
|
||||||
assertEquals(
|
|
||||||
"Wrong number of groups in connection 1",
|
|
||||||
0,
|
|
||||||
getConnection(1).getRoster().getGroupCount());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -174,41 +173,29 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
try {
|
try {
|
||||||
// Add a new roster entry
|
// Add a new roster entry
|
||||||
Roster roster = getConnection(0).getRoster();
|
Roster roster = getConnection(0).getRoster();
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
setupCountdown(latch, roster);
|
||||||
|
|
||||||
roster.createEntry(getBareJID(1), "gato11", null);
|
roster.createEntry(getBareJID(1), "gato11", null);
|
||||||
roster.createEntry(getBareJID(2), "gato12", null);
|
roster.createEntry(getBareJID(2), "gato12", null);
|
||||||
|
|
||||||
// Wait up to 2 seconds to let the server process presence subscriptions
|
waitForCountdown(latch, roster, 2);
|
||||||
long initial = System.currentTimeMillis();
|
CountDownLatch removeLatch = new CountDownLatch(2);
|
||||||
while (System.currentTimeMillis() - initial < 2000 && (
|
RosterListener latchCounter = new RemovalListener(removeLatch);
|
||||||
!roster.getPresence(getBareJID(1)).isAvailable() ||
|
roster.addRosterListener(latchCounter);
|
||||||
!roster.getPresence(getBareJID(2)).isAvailable())) {
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
Thread.sleep(200);
|
|
||||||
|
|
||||||
for (RosterEntry entry : roster.getEntries()) {
|
for (RosterEntry entry : roster.getEntries()) {
|
||||||
roster.removeEntry(entry);
|
roster.removeEntry(entry);
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait up to 2 seconds to receive roster removal notifications
|
removeLatch.await(5, TimeUnit.SECONDS);
|
||||||
initial = System.currentTimeMillis();
|
roster.removeRosterListener(latchCounter);
|
||||||
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 0) {
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount());
|
assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount());
|
||||||
assertEquals("Wrong number of groups in connection 0", 0, roster.getGroupCount());
|
assertEquals("Wrong number of groups in connection 0", 0, roster.getGroupCount());
|
||||||
|
assertEquals("Wrong number of entries in connection 1", 0, getConnection(1).getRoster().getEntryCount());
|
||||||
assertEquals(
|
assertEquals("Wrong number of groups in connection 1", 0, getConnection(1).getRoster().getGroupCount());
|
||||||
"Wrong number of entries in connection 1",
|
|
||||||
0,
|
|
||||||
getConnection(1).getRoster().getEntryCount());
|
|
||||||
assertEquals(
|
|
||||||
"Wrong number of groups in connection 1",
|
|
||||||
0,
|
|
||||||
getConnection(1).getRoster().getGroupCount());
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
|
@ -226,15 +213,30 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
try {
|
try {
|
||||||
// Add a new roster entry
|
// Add a new roster entry
|
||||||
Roster roster = getConnection(0).getRoster();
|
Roster roster = getConnection(0).getRoster();
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
setupCountdown(latch, roster);
|
||||||
|
|
||||||
roster.createEntry(getBareJID(1), null, null);
|
roster.createEntry(getBareJID(1), null, null);
|
||||||
|
|
||||||
// Wait up to 2 seconds to let the server process presence subscriptions
|
waitForCountdown(latch, roster, 1);
|
||||||
long initial = System.currentTimeMillis();
|
|
||||||
while (System.currentTimeMillis() - initial < 2000 &&
|
final CountDownLatch updateLatch = new CountDownLatch(2);
|
||||||
!roster.getPresence(getBareJID(1)).isAvailable())
|
RosterListener latchCounter = new RosterListener() {
|
||||||
{
|
@Override
|
||||||
Thread.sleep(100);
|
public void entriesAdded(Collection<String> addresses) {}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public void entriesUpdated(Collection<String> addresses) {
|
||||||
|
updateLatch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesDeleted(Collection<String> addresses) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void presenceChanged(Presence presence) {}
|
||||||
|
};
|
||||||
|
roster.addRosterListener(latchCounter);
|
||||||
|
|
||||||
// Change the roster entry name and check if the change was made
|
// Change the roster entry name and check if the change was made
|
||||||
for (RosterEntry entry : roster.getEntries()) {
|
for (RosterEntry entry : roster.getEntries()) {
|
||||||
|
@ -243,7 +245,9 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
}
|
}
|
||||||
// Reload the roster and check the name again
|
// Reload the roster and check the name again
|
||||||
roster.reload();
|
roster.reload();
|
||||||
Thread.sleep(2000);
|
|
||||||
|
updateLatch.await(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
for (RosterEntry entry : roster.getEntries()) {
|
for (RosterEntry entry : roster.getEntries()) {
|
||||||
assertEquals("gato11", entry.getName());
|
assertEquals("gato11", entry.getName());
|
||||||
}
|
}
|
||||||
|
@ -251,9 +255,6 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
cleanUpRoster();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -303,9 +304,6 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
cleanUpRoster();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -361,9 +359,6 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
cleanUpRoster();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -426,9 +421,6 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
cleanUpRoster();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -441,75 +433,67 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
* 5. Check that presence for each connected resource is correct
|
* 5. Check that presence for each connected resource is correct
|
||||||
*/
|
*/
|
||||||
public void testRosterPresences() throws Exception {
|
public void testRosterPresences() throws Exception {
|
||||||
Thread.sleep(200);
|
Presence presence;
|
||||||
try {
|
|
||||||
Presence presence;
|
|
||||||
|
|
||||||
// Create another connection for the same user of connection 1
|
// Create another connection for the same user of connection 1
|
||||||
ConnectionConfiguration connectionConfiguration =
|
ConnectionConfiguration connectionConfiguration =
|
||||||
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
|
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
|
||||||
XMPPConnection conn4 = new XMPPConnection(connectionConfiguration);
|
XMPPConnection conn4 = new XMPPConnection(connectionConfiguration);
|
||||||
conn4.connect();
|
conn4.connect();
|
||||||
conn4.login(getUsername(1), getPassword(1), "Home");
|
conn4.login(getUsername(1), getPassword(1), "Home");
|
||||||
|
|
||||||
// Add a new roster entry
|
// Add a new roster entry
|
||||||
Roster roster = getConnection(0).getRoster();
|
Roster roster = getConnection(0).getRoster();
|
||||||
roster.createEntry(getBareJID(1), "gato11", null);
|
roster.createEntry(getBareJID(1), "gato11", null);
|
||||||
|
|
||||||
// Wait up to 2 seconds
|
// Wait up to 2 seconds
|
||||||
long initial = System.currentTimeMillis();
|
long initial = System.currentTimeMillis();
|
||||||
while (System.currentTimeMillis() - initial < 2000 &&
|
while (System.currentTimeMillis() - initial < 2000 &&
|
||||||
(roster.getPresence(getBareJID(1)).getType() == Presence.Type.unavailable)) {
|
(roster.getPresence(getBareJID(1)).getType() == Presence.Type.unavailable)) {
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
}
|
|
||||||
|
|
||||||
// Check that a presence is returned for a user
|
|
||||||
presence = roster.getPresence(getBareJID(1));
|
|
||||||
assertTrue("Returned a null Presence for an existing user", presence.isAvailable());
|
|
||||||
|
|
||||||
// Check that the right presence is returned for a user+resource
|
|
||||||
presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getServiceName() + "/Home");
|
|
||||||
assertEquals("Returned the wrong Presence", "Home",
|
|
||||||
StringUtils.parseResource(presence.getFrom()));
|
|
||||||
|
|
||||||
// Check that the right presence is returned for a user+resource
|
|
||||||
presence = roster.getPresenceResource(getFullJID(1));
|
|
||||||
assertTrue("Presence not found for user " + getFullJID(1), presence.isAvailable());
|
|
||||||
assertEquals("Returned the wrong Presence", "Smack",
|
|
||||||
StringUtils.parseResource(presence.getFrom()));
|
|
||||||
|
|
||||||
// Check the returned presence for a non-existent user+resource
|
|
||||||
presence = roster.getPresenceResource("noname@" + getServiceName() + "/Smack");
|
|
||||||
assertFalse("Available presence was returned for a non-existing user", presence.isAvailable());
|
|
||||||
assertEquals("Returned Presence for a non-existing user has the incorrect type",
|
|
||||||
Presence.Type.unavailable, presence.getType());
|
|
||||||
|
|
||||||
// Check that the returned presences are correct
|
|
||||||
Iterator<Presence> presences = roster.getPresences(getBareJID(1));
|
|
||||||
int count = 0;
|
|
||||||
while (presences.hasNext()) {
|
|
||||||
count++;
|
|
||||||
presences.next();
|
|
||||||
}
|
|
||||||
assertEquals("Wrong number of returned presences", count, 2);
|
|
||||||
|
|
||||||
// Close the connection so one presence must go
|
|
||||||
conn4.disconnect();
|
|
||||||
|
|
||||||
// Check that the returned presences are correct
|
|
||||||
presences = roster.getPresences(getBareJID(1));
|
|
||||||
count = 0;
|
|
||||||
while (presences.hasNext()) {
|
|
||||||
count++;
|
|
||||||
presences.next();
|
|
||||||
}
|
|
||||||
assertEquals("Wrong number of returned presences", count, 1);
|
|
||||||
|
|
||||||
Thread.sleep(200);
|
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
cleanUpRoster();
|
// Check that a presence is returned for a user
|
||||||
|
presence = roster.getPresence(getBareJID(1));
|
||||||
|
assertTrue("Returned a null Presence for an existing user", presence.isAvailable());
|
||||||
|
|
||||||
|
// Check that the right presence is returned for a user+resource
|
||||||
|
presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getServiceName() + "/Home");
|
||||||
|
assertEquals("Returned the wrong Presence", "Home",
|
||||||
|
StringUtils.parseResource(presence.getFrom()));
|
||||||
|
|
||||||
|
// Check that the right presence is returned for a user+resource
|
||||||
|
presence = roster.getPresenceResource(getFullJID(1));
|
||||||
|
assertTrue("Presence not found for user " + getFullJID(1), presence.isAvailable());
|
||||||
|
assertEquals("Returned the wrong Presence", "Smack",
|
||||||
|
StringUtils.parseResource(presence.getFrom()));
|
||||||
|
|
||||||
|
// Check the returned presence for a non-existent user+resource
|
||||||
|
presence = roster.getPresenceResource("noname@" + getServiceName() + "/Smack");
|
||||||
|
assertFalse("Available presence was returned for a non-existing user", presence.isAvailable());
|
||||||
|
assertEquals("Returned Presence for a non-existing user has the incorrect type",
|
||||||
|
Presence.Type.unavailable, presence.getType());
|
||||||
|
|
||||||
|
// Check that the returned presences are correct
|
||||||
|
Iterator<Presence> presences = roster.getPresences(getBareJID(1));
|
||||||
|
int count = 0;
|
||||||
|
while (presences.hasNext()) {
|
||||||
|
count++;
|
||||||
|
presences.next();
|
||||||
}
|
}
|
||||||
|
assertEquals("Wrong number of returned presences", count, 2);
|
||||||
|
|
||||||
|
// Close the connection so one presence must go
|
||||||
|
conn4.disconnect();
|
||||||
|
|
||||||
|
// Check that the returned presences are correct
|
||||||
|
presences = roster.getPresences(getBareJID(1));
|
||||||
|
count = 0;
|
||||||
|
while (presences.hasNext()) {
|
||||||
|
count++;
|
||||||
|
presences.next();
|
||||||
|
}
|
||||||
|
assertEquals("Wrong number of returned presences", count, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -605,6 +589,24 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
for (int i=0; i<getMaxConnections(); i++) {
|
for (int i=0; i<getMaxConnections(); i++) {
|
||||||
// Delete all the entries from the roster
|
// Delete all the entries from the roster
|
||||||
Roster roster = getConnection(i).getRoster();
|
Roster roster = getConnection(i).getRoster();
|
||||||
|
final CountDownLatch removalLatch = new CountDownLatch(roster.getEntryCount());
|
||||||
|
roster.addRosterListener(new RosterListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void presenceChanged(Presence presence) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesUpdated(Collection<String> addresses) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesDeleted(Collection<String> addresses) {
|
||||||
|
removalLatch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesAdded(Collection<String> addresses) {}
|
||||||
|
});
|
||||||
|
|
||||||
for (RosterEntry entry : roster.getEntries()) {
|
for (RosterEntry entry : roster.getEntries()) {
|
||||||
try {
|
try {
|
||||||
roster.removeEntry(entry);
|
roster.removeEntry(entry);
|
||||||
|
@ -616,49 +618,16 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(700);
|
removalLatch.await(5, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
catch (InterruptedException e) {
|
catch (InterruptedException e) {
|
||||||
fail(e.getMessage());
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Wait up to 6 seconds to receive roster removal notifications
|
|
||||||
long initial = System.currentTimeMillis();
|
|
||||||
while (System.currentTimeMillis() - initial < 6000 && (
|
|
||||||
getConnection(0).getRoster().getEntryCount() != 0 ||
|
|
||||||
getConnection(1).getRoster().getEntryCount() != 0 ||
|
|
||||||
getConnection(2).getRoster().getEntryCount() != 0)) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(100);
|
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals("Wrong number of entries in connection 0", 0, getConnection(0).getRoster().getEntryCount());
|
||||||
"Wrong number of entries in connection 0",
|
assertEquals("Wrong number of entries in connection 1", 0, getConnection(1).getRoster().getEntryCount());
|
||||||
0,
|
assertEquals("Wrong number of entries in connection 2", 0, getConnection(2).getRoster().getEntryCount());
|
||||||
getConnection(0).getRoster().getEntryCount());
|
|
||||||
assertEquals(
|
|
||||||
"Wrong number of groups in connection 0",
|
|
||||||
0,
|
|
||||||
getConnection(0).getRoster().getGroupCount());
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
"Wrong number of entries in connection 1",
|
|
||||||
0,
|
|
||||||
getConnection(1).getRoster().getEntryCount());
|
|
||||||
assertEquals(
|
|
||||||
"Wrong number of groups in connection 1",
|
|
||||||
0,
|
|
||||||
getConnection(1).getRoster().getGroupCount());
|
|
||||||
|
|
||||||
assertEquals(
|
|
||||||
"Wrong number of entries in connection 2",
|
|
||||||
0,
|
|
||||||
getConnection(2).getRoster().getEntryCount());
|
|
||||||
assertEquals(
|
|
||||||
"Wrong number of groups in connection 2",
|
|
||||||
0,
|
|
||||||
getConnection(2).getRoster().getGroupCount());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -708,15 +677,37 @@ public class RosterSmackTest extends SmackTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
//XMPPConnection.DEBUG_ENABLED = false;
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(500);
|
|
||||||
}
|
|
||||||
catch (InterruptedException e) {
|
|
||||||
fail(e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
cleanUpRoster();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void tearDown() throws Exception {
|
||||||
|
cleanUpRoster();
|
||||||
|
super.tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class RemovalListener implements RosterListener {
|
||||||
|
private CountDownLatch latch;
|
||||||
|
|
||||||
|
private RemovalListener(CountDownLatch removalLatch) {
|
||||||
|
latch = removalLatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void presenceChanged(Presence presence) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesUpdated(Collection<String> addresses) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesDeleted(Collection<String> addresses) {
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void entriesAdded(Collection<String> addresses) {}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue