From 3d1538865a8c516049dabe464d64ee62d103d80a Mon Sep 17 00:00:00 2001 From: emcho Date: Sun, 31 Jan 2010 20:35:58 +0000 Subject: [PATCH] Fixes ice4j test suite and speeds up test execution (tests now execute for less than 20s --- test/org/ice4j/StunTestSuite.java | 4 +- test/org/ice4j/TransactionSupportTests.java | 123 ++++++++++++++---- test/org/ice4j/stack/DatagramCollector.java | 6 +- .../stunclient/ResponseSequenceServer.java | 1 + .../stunclient/StunAddressDiscovererTest.java | 13 +- .../StunAddressDiscovererTest_v4v6.java | 13 +- .../StunAddressDiscovererTest_v6.java | 12 +- 7 files changed, 137 insertions(+), 35 deletions(-) diff --git a/test/org/ice4j/StunTestSuite.java b/test/org/ice4j/StunTestSuite.java index 278df51..29c6921 100644 --- a/test/org/ice4j/StunTestSuite.java +++ b/test/org/ice4j/StunTestSuite.java @@ -65,7 +65,7 @@ public class StunTestSuite //event dispatching suite.addTestSuite(org.ice4j.MessageEventDispatchingTest.class); - /* + //transactions suite.addTestSuite(org.ice4j.TransactionSupportTests.class); @@ -73,7 +73,7 @@ public class StunTestSuite suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest.class); suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v6.class); suite.addTestSuite(org.ice4j.stunclient.StunAddressDiscovererTest_v4v6.class); -*/ + return suite; } } diff --git a/test/org/ice4j/TransactionSupportTests.java b/test/org/ice4j/TransactionSupportTests.java index 5f52858..a4086b4 100644 --- a/test/org/ice4j/TransactionSupportTests.java +++ b/test/org/ice4j/TransactionSupportTests.java @@ -8,6 +8,7 @@ package org.ice4j; import java.net.*; import java.util.*; +import java.util.logging.*; import junit.framework.*; @@ -33,7 +34,7 @@ public class TransactionSupportTests extends TestCase * The client address we use for this test. */ TransportAddress serverAddress - = new TransportAddress("127.0.0.2", 5255, Transport.UDP); + = new TransportAddress("127.0.0.1", 5255, Transport.UDP); /** * The socket the client uses in this test. @@ -91,6 +92,9 @@ public class TransactionSupportTests extends TestCase protected void tearDown() throws Exception { + StunStack.getInstance().removeSocket(clientAddress); + StunStack.getInstance().removeSocket(serverAddress); + clientSock.close(); serverSock.close(); @@ -118,6 +122,14 @@ public class TransactionSupportTests extends TestCase */ public void testClientRetransmissions() throws Exception { + String oldRetransValue = System.getProperty( + "org.ice4j.MAX_RETRANSMISSIONS"); + String oldMaxWaitValue = System.getProperty( + "org.ice4j.MAX_WAIT_INTERVAL"); + + System.setProperty("org.ice4j.MAX_WAIT_INTERVAL", "100"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); + //prepare to listen System.setProperty("org.ice4j.PROPAGATE_RECEIVED_RETRANSMISSIONS", "true"); @@ -128,7 +140,7 @@ public class TransactionSupportTests extends TestCase StunStack.getInstance().sendRequest(bindingRequest, serverAddress, clientAddress, responseCollector); //wait for retransmissions - Thread.sleep(12000); + Thread.sleep(1000); //verify Vector reqs @@ -137,17 +149,36 @@ public class TransactionSupportTests extends TestCase assertTrue("No retransmissions of the request have been received", reqs.size() > 1); assertTrue("The binding request has not been retransmitted enough!", - reqs.size() >= 7); + reqs.size() >= 3); + + //restore the retransmissions prop in case others are counting on + //defaults. + if(oldRetransValue != null) + System.getProperty( "org.ice4j.MAX_RETRANSMISSIONS", + oldRetransValue); + else + System.clearProperty("org.ice4j.MAX_RETRANSMISSIONS"); + + if(oldMaxWaitValue != null) + System.getProperty( "org.ice4j.MAX_WAIT_INTERVAL", + oldRetransValue); + else + System.clearProperty("org.ice4j.MAX_WAIT_INTERVAL"); + } /** * Make sure that retransmissions are not seen by the server user and that * it only gets a single request. + * * @throws Exception if anything goes wrong. */ public void testServerRetransmissionHiding() throws Exception { + String oldRetransValue = System.getProperty( + "org.ice4j.MAX_RETRANSMISSIONS"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); //prepare to listen StunStack.getInstance().addRequestListener( serverAddress, requestCollector); @@ -156,7 +187,7 @@ public class TransactionSupportTests extends TestCase serverAddress, clientAddress, responseCollector); //wait for retransmissions - Thread.sleep(12000); + Thread.sleep(1000); //verify Vector reqs @@ -166,6 +197,14 @@ public class TransactionSupportTests extends TestCase assertTrue( "Retransmissions of a binding request were propagated " + "to the server", reqs.size() <= 1 ); + + //restore the retransmissions prop in case others are counting on + //defaults. + if(oldRetransValue != null) + System.getProperty( "org.ice4j.MAX_RETRANSMISSIONS", + oldRetransValue); + else + System.clearProperty("org.ice4j.MAX_RETRANSMISSIONS"); } /** @@ -177,6 +216,11 @@ public class TransactionSupportTests extends TestCase */ public void testServerResponseRetransmissions() throws Exception { + String oldRetransValue = System.getProperty( + "org.ice4j.MAX_RETRANSMISSIONS"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); + System.setProperty("org.ice4j.MAX_WAIT_INTERVAL", "100"); + //prepare to listen System.setProperty("org.ice4j.KEEP_CLIENT_TRANS_AFTER_A_RESPONSE", "true"); @@ -188,7 +232,7 @@ public class TransactionSupportTests extends TestCase clientAddress, responseCollector); //wait for the message to arrive - Thread.sleep(500); + requestCollector.waitForRequest(); Vector reqs = requestCollector .getRequestsForTransaction( bindingRequest.getTransactionID()); @@ -200,12 +244,23 @@ public class TransactionSupportTests extends TestCase tid, bindingResponse, serverAddress, clientAddress); //wait for retransmissions - Thread.currentThread().sleep(12000); + Thread.sleep(500); - //verify that at least half of the request received a retransmitted resp. + //verify that we received a fair number of retransmitted responses. assertTrue( - "There were no retransmissions of a binding response", - responseCollector.receivedResponses.size() < 5 ); + "There were too few retransmissions of a binding response: " + +responseCollector.receivedResponses.size(), + responseCollector.receivedResponses.size() < 3 ); + + //restore the retransmissions prop in case others are counting on + //defaults. + if(oldRetransValue != null) + System.getProperty( "org.ice4j.MAX_RETRANSMISSIONS", + oldRetransValue); + else + System.clearProperty("org.ice4j.MAX_RETRANSMISSIONS"); + + System.clearProperty("org.ice4j.MAX_WAIT_INTERVAL"); } /** @@ -221,7 +276,7 @@ public class TransactionSupportTests extends TestCase serverAddress, clientAddress, responseCollector); //wait for retransmissions - Thread.sleep(500); + requestCollector.waitForRequest(); Vector reqs1 = requestCollector .getRequestsForTransaction( bindingRequest.getTransactionID()); @@ -238,7 +293,7 @@ public class TransactionSupportTests extends TestCase serverAddress, clientAddress, responseCollector); //wait for retransmissions - Thread.sleep(12000); + Thread.sleep(1000); Vector reqs2 = requestCollector.getRequestsForTransaction( @@ -288,7 +343,7 @@ public class TransactionSupportTests extends TestCase { //MAX_RETRANSMISSIONS System.setProperty("org.ice4j.ORIGINAL_WAIT_INTERVAL", - "1000"); + "50"); //make sure we see retransmissions so that we may count them System.setProperty("org.ice4j.PROPAGATE_RECEIVED_RETRANSMISSIONS", "true"); @@ -299,7 +354,7 @@ public class TransactionSupportTests extends TestCase serverAddress, clientAddress, responseCollector); //wait a while - Thread.currentThread().sleep(500); + requestCollector.waitForRequest(); //verify Vector reqs = requestCollector.getRequestsForTransaction( @@ -308,15 +363,14 @@ public class TransactionSupportTests extends TestCase reqs.size() < 2); //wait for a send - Thread.currentThread().sleep(700); + Thread.sleep(110); reqs = requestCollector.getRequestsForTransaction( bindingRequest.getTransactionID()); //verify assertEquals("A retransmissions of the request was not sent", - 2, - reqs.size()); + 2, reqs.size()); } public void testMaxWaitIntervalConfigurationParameter() @@ -337,18 +391,19 @@ public class TransactionSupportTests extends TestCase serverAddress, clientAddress, responseCollector); //wait a while - Thread.sleep(1095); + Thread.sleep(1200); //verify - Vector reqs = requestCollector.getRequestsForTransaction( + Vector reqs + = requestCollector.getRequestsForTransaction( bindingRequest.getTransactionID()); assertEquals("Not all retransmissions were made for the expected period " +"of time", - 11, + 12, reqs.size()); //wait for a send - Thread.currentThread().sleep(1800); + Thread.sleep(1800); //verify reqs = requestCollector.getRequestsForTransaction( @@ -360,10 +415,16 @@ public class TransactionSupportTests extends TestCase } private class PlainRequestCollector implements RequestListener{ - private Vector receivedRequestsVector = new Vector(); + private Vector receivedRequestsVector + = new Vector(); - public void requestReceived(StunMessageEvent evt){ - receivedRequestsVector.add(evt); + public void requestReceived(StunMessageEvent evt) + { + synchronized(this) + { + receivedRequestsVector.add(evt); + notifyAll(); + } } /** @@ -379,7 +440,8 @@ public class TransactionSupportTests extends TestCase { Vector newVec = new Vector(); - Iterator reqsIter = receivedRequestsVector.iterator(); + Iterator reqsIter + = receivedRequestsVector.iterator(); while(reqsIter.hasNext()) { @@ -391,6 +453,19 @@ public class TransactionSupportTests extends TestCase return newVec; } + + public void waitForRequest() + { + synchronized(this) + { + try + { + wait(50); + } + catch (InterruptedException e) + {} + } + } } private class PlainResponseCollector implements ResponseCollector{ diff --git a/test/org/ice4j/stack/DatagramCollector.java b/test/org/ice4j/stack/DatagramCollector.java index 3e37161..bfee339 100644 --- a/test/org/ice4j/stack/DatagramCollector.java +++ b/test/org/ice4j/stack/DatagramCollector.java @@ -15,6 +15,8 @@ public class DatagramCollector DatagramPacket receivedPacket = null; DatagramSocket sock = null; + boolean packetReceived = false; + public DatagramCollector() { } @@ -28,6 +30,7 @@ public class DatagramCollector synchronized (this) { + packetReceived = true; notifyAll(); } @@ -51,7 +54,7 @@ public class DatagramCollector { synchronized(this) { - if(receivedPacket != null) + if(packetReceived) return; try @@ -72,6 +75,7 @@ public class DatagramCollector DatagramPacket returnValue = receivedPacket; receivedPacket = null; sock = null; + packetReceived = false; //return return returnValue; diff --git a/test/org/ice4j/stunclient/ResponseSequenceServer.java b/test/org/ice4j/stunclient/ResponseSequenceServer.java index fbd34bf..89fdf7e 100644 --- a/test/org/ice4j/stunclient/ResponseSequenceServer.java +++ b/test/org/ice4j/stunclient/ResponseSequenceServer.java @@ -68,6 +68,7 @@ public class ResponseSequenceServer */ public void shutDown() { + stunStack.removeSocket(serverAddress); messageSequence.removeAllElements(); localSocket.close(); diff --git a/test/org/ice4j/stunclient/StunAddressDiscovererTest.java b/test/org/ice4j/stunclient/StunAddressDiscovererTest.java index 5dd20ac..be0bdb3 100644 --- a/test/org/ice4j/stunclient/StunAddressDiscovererTest.java +++ b/test/org/ice4j/stunclient/StunAddressDiscovererTest.java @@ -44,8 +44,13 @@ public class StunAddressDiscovererTest super(name); } - protected void setUp() throws Exception { + protected void setUp() throws Exception + { super.setUp(); + + System.setProperty("org.ice4j.MAX_WAIT_INTERVAL", "100"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); + responseServer = new ResponseSequenceServer(responseServerAddress); stunAddressDiscoverer = new NetworkConfigurationDiscoveryProcess(discovererAddress, @@ -55,8 +60,10 @@ public class StunAddressDiscovererTest responseServer.start(); } - protected void tearDown() throws Exception { - + protected void tearDown() throws Exception + { + System.clearProperty("org.ice4j.MAX_WAIT_INTERVAL"); + System.clearProperty("org.ice4j.MAX_RETRANSMISSIONS"); responseServer.shutDown(); stunAddressDiscoverer.shutDown(); stunAddressDiscoverer = null; diff --git a/test/org/ice4j/stunclient/StunAddressDiscovererTest_v4v6.java b/test/org/ice4j/stunclient/StunAddressDiscovererTest_v4v6.java index 150af54..409b39c 100644 --- a/test/org/ice4j/stunclient/StunAddressDiscovererTest_v4v6.java +++ b/test/org/ice4j/stunclient/StunAddressDiscovererTest_v4v6.java @@ -62,7 +62,9 @@ public class StunAddressDiscovererTest_v4v6 extends TestCase { super(name); } - protected void setUp() throws Exception { + protected void setUp() + throws Exception + { super.setUp(); responseServer_v6 = new ResponseSequenceServer(responseServerAddress_v6); @@ -77,9 +79,16 @@ public class StunAddressDiscovererTest_v4v6 extends TestCase { stunAddressDiscoverer_v4.start(); responseServer_v6.start(); responseServer_v4.start(); + + System.setProperty("org.ice4j.MAX_WAIT_INTERVAL", "100"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); } - protected void tearDown() throws Exception { + protected void tearDown() + throws Exception + { + System.clearProperty("org.ice4j.MAX_WAIT_INTERVAL"); + System.clearProperty("org.ice4j.MAX_RETRANSMISSIONS"); responseServer_v6.shutDown(); responseServer_v4.shutDown(); diff --git a/test/org/ice4j/stunclient/StunAddressDiscovererTest_v6.java b/test/org/ice4j/stunclient/StunAddressDiscovererTest_v6.java index b466535..efb96ea 100644 --- a/test/org/ice4j/stunclient/StunAddressDiscovererTest_v6.java +++ b/test/org/ice4j/stunclient/StunAddressDiscovererTest_v6.java @@ -41,7 +41,8 @@ public class StunAddressDiscovererTest_v6 extends TestCase { super(name); } - protected void setUp() throws Exception { + protected void setUp() throws Exception + { super.setUp(); responseServer = new ResponseSequenceServer(responseServerAddress); stunAddressDiscoverer @@ -51,10 +52,15 @@ public class StunAddressDiscovererTest_v6 extends TestCase { stunAddressDiscoverer.start(); responseServer.start(); + + System.setProperty("org.ice4j.MAX_WAIT_INTERVAL", "100"); + System.setProperty("org.ice4j.MAX_RETRANSMISSIONS", "2"); } - protected void tearDown() throws Exception { - + protected void tearDown() throws Exception + { + System.clearProperty("org.ice4j.MAX_WAIT_INTERVAL"); + System.clearProperty("org.ice4j.MAX_RETRANSMISSIONS"); responseServer.shutDown(); stunAddressDiscoverer.shutDown(); stunAddressDiscoverer = null;