1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-23 06:42:05 +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:
rcollier 2013-04-22 01:03:43 +00:00
parent fe2e9cdb76
commit ce0cb7d491
3 changed files with 283 additions and 272 deletions

View file

@ -17,7 +17,11 @@
*/
package org.jivesoftware.smack;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.ping.PingManager;
/**
* Tests the connection and reconnection mechanism
@ -27,6 +31,8 @@ import org.jivesoftware.smack.test.SmackTestCase;
public class ReconnectionTest extends SmackTestCase {
private static final long MIN_RECONNECT_WAIT = 17; // Seconds
public ReconnectionTest(String arg0) {
super(arg0);
}
@ -38,19 +44,17 @@ public class ReconnectionTest extends SmackTestCase {
public void testAutomaticReconnection() throws Exception {
XMPPConnection connection = getConnection(0);
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
CountDownLatch latch = new CountDownLatch(1);
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
connection.addConnectionListener(listener);
// Simulates an error in the connection
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
assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
true, listener.reconnected);
assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
listener.attemptsNotifications);
assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
listener.remainingSeconds);
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);
@ -73,19 +77,17 @@ public class ReconnectionTest extends SmackTestCase {
// Executes some server interaction testing the connection
executeSomeServerInteraction(connection);
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
CountDownLatch latch = new CountDownLatch(1);
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
connection.addConnectionListener(listener);
// Simulates an error in the connection
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
assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
true, listener.reconnected);
assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
listener.attemptsNotifications);
assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
listener.remainingSeconds);
assertEquals("The ConnectionListener.connectionEstablished() 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);
@ -97,7 +99,8 @@ public class ReconnectionTest extends SmackTestCase {
*/
public void testManualReconnectionWithCancelation() throws Exception {
XMPPConnection connection = getConnection(0);
XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
CountDownLatch latch = new CountDownLatch(1);
XMPPConnectionTestListener listener = new XMPPConnectionTestListener(latch);
connection.addConnectionListener(listener);
// Produces a connection error
@ -105,14 +108,14 @@ public class ReconnectionTest extends SmackTestCase {
assertEquals(
"An error occurs but the ConnectionListener.connectionClosedOnError(e) was not notified",
true, listener.connectionClosedOnError);
Thread.sleep(1000);
// Thread.sleep(1000);
// Cancels the automatic reconnection
connection.getConfiguration().setReconnectionAllowed(false);
// Waits for a reconnection that must not happened.
Thread.sleep(10500);
Thread.sleep(MIN_RECONNECT_WAIT * 1000);
// Cancels the automatic reconnection
assertEquals("The connection was stablished but it was not allowed to", false,
listener.reconnected);
assertEquals(false, listener.reconnected);
// Makes a manual reconnection from an error terminated connection without reconnection
connection.connect();
@ -137,7 +140,7 @@ public class ReconnectionTest extends SmackTestCase {
assertEquals("ConnectionListener.connectionClosed() was not notified",
true, listener.connectionClosed);
// 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,
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.
*/
private void executeSomeServerInteraction(XMPPConnection connection) throws XMPPException {
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(connection);
privacyManager.getPrivacyLists();
PingManager pingManager = PingManager.getInstanceFor(connection);
pingManager.pingMyServer();
}
protected int getMaxConnections() {
@ -198,19 +201,29 @@ public class ReconnectionTest extends SmackTestCase {
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;
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;
private volatile boolean reconnectionCanceled = false;
private CountDownLatch countDownLatch;
private XMPPConnectionTestListener(CountDownLatch latch) {
countDownLatch = latch;
}
private XMPPConnectionTestListener() {
}
/**
* Methods to test the listener.
*/
public void connectionClosed() {
connectionClosed = true;
if (countDownLatch != null)
countDownLatch.countDown();
}
public void connectionClosedOnError(Exception e) {
@ -219,20 +232,28 @@ public class ReconnectionTest extends SmackTestCase {
public void reconnectionCanceled() {
reconnectionCanceled = true;
if (countDownLatch != null)
countDownLatch.countDown();
}
public void reconnectingIn(int seconds) {
attemptsNotifications = attemptsNotifications + 1;
remainingSeconds = seconds;
}
public void reconnectionSuccessful() {
reconnected = true;
if (countDownLatch != null)
countDownLatch.countDown();
}
public void reconnectionFailed(Exception error) {
reconnectionFailed = true;
if (countDownLatch != null)
countDownLatch.countDown();
}
}

View file

@ -20,6 +20,7 @@ package org.jivesoftware.smack;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.jivesoftware.smack.packet.Presence;
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
inviterRoster.createEntry(getBareJID(inviteeIndex), getUsername(inviteeIndex), null);
Thread.sleep(500); // wait for XMPP server
XMPPConnection inviteeConnection = getConnection(inviteeIndex);
assertFalse("Invitee is already online", inviteeConnection.isConnected());
@ -80,7 +79,7 @@ public class RosterListenerTest extends SmackTestCase {
// connect after adding the listener
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)));

View file

@ -21,12 +21,16 @@
package org.jivesoftware.smack;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.util.StringUtils;
import org.mockito.internal.util.RemoveFirstLine;
/**
* Tests the Roster functionality by creating and removing roster entries.
@ -43,6 +47,7 @@ public class RosterSmackTest extends SmackTestCase {
super(name);
}
/**
* 1. Create entries in roster groups
* 2. Iterate on the groups and remove the entry from each group
@ -52,63 +57,77 @@ public class RosterSmackTest extends SmackTestCase {
try {
// Add a new roster entry
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(2), "gato12", new String[] { "Family" });
// Wait until the server confirms the new entries
long initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 && (
!roster.getPresence(getBareJID(1)).isAvailable() ||
!roster.getPresence(getBareJID(2)).isAvailable())) {
Thread.sleep(100);
waitForCountdown(latch, roster, 2);
final CountDownLatch removeLatch = new CountDownLatch(3);
RosterListener latchCounter = new RosterListener() {
@Override
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 (RosterGroup rosterGroup : entry.getGroups()) {
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(
"The number of entries in connection 1 should be 1",
1,
getConnection(1).getRoster().getEntryCount());
assertEquals(
"The number of groups in connection 1 should be 0",
0,
getConnection(1).getRoster().getGroupCount());
removeLatch.await(5, TimeUnit.SECONDS);
roster.removeRosterListener(latchCounter);
assertEquals(
"The number of entries in connection 2 should be 1",
1,
getConnection(2).getRoster().getEntryCount());
assertEquals(
"The number of groups in connection 2 should be 0",
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());
assertEquals("The number of entries in connection 1 should be 1", 1, getConnection(1).getRoster().getEntryCount());
assertEquals("The number of groups in connection 1 should be 0", 0, getConnection(1).getRoster().getGroupCount());
assertEquals("The number of entries in connection 2 should be 1", 1, getConnection(2).getRoster().getEntryCount());
assertEquals("The number of groups in connection 2 should be 0", 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) {
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 {
// Add a new roster entry
Roster roster = getConnection(0).getRoster();
CountDownLatch latch = new CountDownLatch(2);
setupCountdown(latch, roster);
roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends" });
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
// Wait up to 2 seconds to receive new roster contacts
long initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 2) {
Thread.sleep(100);
}
waitForCountdown(latch, roster, 2);
assertEquals("Wrong number of entries in connection 0", 2, roster.getEntryCount());
// Wait up to 2 seconds to receive presences of the new roster contacts
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());
CountDownLatch removeLatch = new CountDownLatch(2);
RosterListener latchCounter = new RemovalListener(removeLatch);
roster.addRosterListener(latchCounter);
for (RosterEntry entry : roster.getEntries()) {
roster.removeEntry(entry);
Thread.sleep(250);
}
// Wait up to 2 seconds to receive roster removal notifications
initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 0) {
Thread.sleep(100);
}
removeLatch.await(5, TimeUnit.SECONDS);
roster.removeRosterListener(latchCounter);
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 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 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 {
// Add a new roster entry
Roster roster = getConnection(0).getRoster();
CountDownLatch latch = new CountDownLatch(2);
setupCountdown(latch, roster);
roster.createEntry(getBareJID(1), "gato11", null);
roster.createEntry(getBareJID(2), "gato12", null);
// Wait up to 2 seconds to let the server process presence subscriptions
long initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 && (
!roster.getPresence(getBareJID(1)).isAvailable() ||
!roster.getPresence(getBareJID(2)).isAvailable())) {
Thread.sleep(100);
}
Thread.sleep(200);
waitForCountdown(latch, roster, 2);
CountDownLatch removeLatch = new CountDownLatch(2);
RosterListener latchCounter = new RemovalListener(removeLatch);
roster.addRosterListener(latchCounter);
for (RosterEntry entry : roster.getEntries()) {
roster.removeEntry(entry);
Thread.sleep(100);
}
// Wait up to 2 seconds to receive roster removal notifications
initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 && roster.getEntryCount() != 0) {
Thread.sleep(100);
}
removeLatch.await(5, TimeUnit.SECONDS);
roster.removeRosterListener(latchCounter);
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 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 1", 0, getConnection(1).getRoster().getEntryCount());
assertEquals("Wrong number of groups in connection 1", 0, getConnection(1).getRoster().getGroupCount());
}
catch (Exception e) {
fail(e.getMessage());
@ -226,16 +213,31 @@ public class RosterSmackTest extends SmackTestCase {
try {
// Add a new roster entry
Roster roster = getConnection(0).getRoster();
CountDownLatch latch = new CountDownLatch(1);
setupCountdown(latch, roster);
roster.createEntry(getBareJID(1), null, null);
// Wait up to 2 seconds to let the server process presence subscriptions
long initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 &&
!roster.getPresence(getBareJID(1)).isAvailable())
{
Thread.sleep(100);
waitForCountdown(latch, roster, 1);
final CountDownLatch updateLatch = new CountDownLatch(2);
RosterListener latchCounter = new RosterListener() {
@Override
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
for (RosterEntry entry : roster.getEntries()) {
entry.setName("gato11");
@ -243,7 +245,9 @@ public class RosterSmackTest extends SmackTestCase {
}
// Reload the roster and check the name again
roster.reload();
Thread.sleep(2000);
updateLatch.await(5, TimeUnit.SECONDS);
for (RosterEntry entry : roster.getEntries()) {
assertEquals("gato11", entry.getName());
}
@ -251,9 +255,6 @@ public class RosterSmackTest extends SmackTestCase {
catch (Exception e) {
fail(e.getMessage());
}
finally {
cleanUpRoster();
}
}
/**
@ -303,9 +304,6 @@ public class RosterSmackTest extends SmackTestCase {
} catch (Exception e) {
fail(e.getMessage());
}
finally {
cleanUpRoster();
}
}
/**
@ -361,9 +359,6 @@ public class RosterSmackTest extends SmackTestCase {
catch (Exception e) {
fail(e.getMessage());
}
finally {
cleanUpRoster();
}
}
/**
@ -426,9 +421,6 @@ public class RosterSmackTest extends SmackTestCase {
catch (Exception e) {
fail(e.getMessage());
}
finally {
cleanUpRoster();
}
}
/**
@ -441,8 +433,6 @@ public class RosterSmackTest extends SmackTestCase {
* 5. Check that presence for each connected resource is correct
*/
public void testRosterPresences() throws Exception {
Thread.sleep(200);
try {
Presence presence;
// Create another connection for the same user of connection 1
@ -504,12 +494,6 @@ public class RosterSmackTest extends SmackTestCase {
presences.next();
}
assertEquals("Wrong number of returned presences", count, 1);
Thread.sleep(200);
}
finally {
cleanUpRoster();
}
}
/**
@ -605,6 +589,24 @@ public class RosterSmackTest extends SmackTestCase {
for (int i=0; i<getMaxConnections(); i++) {
// Delete all the entries from the roster
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()) {
try {
roster.removeEntry(entry);
@ -616,49 +618,16 @@ public class RosterSmackTest extends SmackTestCase {
}
try {
Thread.sleep(700);
removalLatch.await(5, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
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(
"Wrong number of entries in connection 0",
0,
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());
assertEquals("Wrong number of entries in connection 0", 0, getConnection(0).getRoster().getEntryCount());
assertEquals("Wrong number of entries in connection 1", 0, getConnection(1).getRoster().getEntryCount());
assertEquals("Wrong number of entries in connection 2", 0, getConnection(2).getRoster().getEntryCount());
}
/**
@ -708,15 +677,37 @@ public class RosterSmackTest extends SmackTestCase {
}
protected void setUp() throws Exception {
//XMPPConnection.DEBUG_ENABLED = false;
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
fail(e.getMessage());
}
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) {}
};
}