Make the test work without relying on a sleep statement to get the results needed.

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1177338 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2011-09-29 15:38:15 +00:00
parent b6b69113b1
commit 6d31ed72f0
1 changed files with 25 additions and 23 deletions

View File

@ -26,50 +26,52 @@ import javax.net.ServerSocketFactory;
import junit.framework.TestCase;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.util.Wait;
public class SlowConnectionTest extends TestCase {
public void testSlowConnection() throws Exception {
int timeout = 1000;
URI tcpUri = new URI("tcp://localhost:61616?soTimeout=" + timeout + "&trace=true&connectionTimeout=" + timeout + "&wireFormat.maxInactivityDurationInitalDelay=" + timeout);
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + tcpUri + ")");
final Connection connection = cf.createConnection();
MockBroker broker = new MockBroker();
broker.start();
new Thread(new Runnable() {
public void run() {
try { connection.start(); } catch (Throwable ignored) {}
}
}).start();
Thread.sleep(timeout * 5);
int count = 0;
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (thread.getName().contains("ActiveMQ Transport")) { count++; }
}
assertTrue("Transport count: " + count + ", expected <= 1", Wait.waitFor(new Wait.Condition(){
public boolean isSatisified() throws Exception {
int count = 0;
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (thread.getName().contains("ActiveMQ Transport")) { count++; }
}
return count == 1;
}}));
broker.interrupt();
broker.join();
assertTrue("Transport count: " + count + ", expected <= 1", count <= 1);
}
}
class MockBroker extends Thread {
public void run() {
List<Socket> inProgress = new ArrayList<Socket>();
ServerSocketFactory factory = ServerSocketFactory.getDefault();
ServerSocket ss = null;
try {
ss = factory.createServerSocket(61616);
while (!interrupted()) {
inProgress.add(ss.accept()); // eat socket
}
@ -77,10 +79,10 @@ public class SlowConnectionTest extends TestCase {
e.printStackTrace();
} finally {
try { ss.close(); } catch (IOException ignored) {}
for (Socket s : inProgress) {
for (Socket s : inProgress) {
try { s.close(); } catch (IOException ignored) {}
}
}
}
}
}
}
}