directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r329152 - /directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
Date Fri, 28 Oct 2005 08:22:55 GMT
Author: trustin
Date: Fri Oct 28 01:22:49 2005
New Revision: 329152

URL: http://svn.apache.org/viewcvs?rev=329152&view=rev
Log:
Fixed failing ConnectorTest due to BindException.

Modified:
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java

Modified: directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java?rev=329152&r1=329151&r2=329152&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
(original)
+++ directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
Fri Oct 28 01:22:49 2005
@@ -18,9 +18,9 @@
  */
 package org.apache.mina.examples.echoserver;
 
+import java.net.BindException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-import java.net.SocketAddress;
 
 import junit.framework.Assert;
 
@@ -48,8 +48,6 @@
  */
 public class ConnectorTest extends AbstractTest
 {
-    private int clientPort;
-    
     public ConnectorTest()
     {
     }
@@ -57,9 +55,6 @@
     public void setUp() throws Exception
     {
         super.setUp();
-        clientPort = port;
-        clientPort = AvailablePortFinder.getNextAvailable( clientPort + 1 );
-        System.out.println( "Using port " + clientPort + " as local address" );
     }
     
     public void testTCP() throws Exception
@@ -99,44 +94,56 @@
     
     private void testConnector( IoConnector connector ) throws Exception
     {
-        InetSocketAddress localAddress = new InetSocketAddress( clientPort );
-
-        System.out.println("* Without localAddress and initializer");
-        testConnector( connector, null );
-        
-        System.out.println("* Without localAddress and with initializer");
-        testConnector( connector, null );
-
-        // Tests below fail in Windows platform.
-        if( System.getProperty("os.name").toLowerCase().indexOf( "windows" ) >= 0 )
-        {
-            // skip further tests
-            System.out.println( "** Skipping some tests that fails in Windows platform."
);
-            return;
-        }
+        System.out.println("* Without localAddress");
+        testConnector( connector, false );
         
-        System.out.println("* With localAddress and without initializer");
-        testConnector( connector, localAddress );
-        
-        // It takes some time for local address to be cleared by OS,
-        // so let's just get a new one rather than waiting for it.
-        clientPort = AvailablePortFinder.getNextAvailable( clientPort + 1 );
-        localAddress = new InetSocketAddress( clientPort );
-
-        System.out.println("* With localAddress and initializer");
-        testConnector( connector, localAddress );
+        System.out.println("* With localAddress");
+        testConnector( connector, true );
     }
     
-    private void testConnector( IoConnector connector, SocketAddress localAddress ) throws
Exception
+    private void testConnector( IoConnector connector, boolean useLocalAddress ) throws Exception
     {
         EchoConnectorHandler handler = new EchoConnectorHandler();
-        ConnectFuture future = connector.connect(
-                new InetSocketAddress( InetAddress.getLocalHost(), port ),
-                localAddress,
-                handler );
         
-        future.join();
-        IoSession session = future.getSession();
+        IoSession session = null;
+        if( !useLocalAddress )
+        {
+            ConnectFuture future = connector.connect(
+                    new InetSocketAddress( InetAddress.getLocalHost(), port ),
+                    handler );
+            future.join();
+            session = future.getSession();
+        }
+        else
+        {
+            int clientPort = port;
+            for( int i = 0; i < 65536; i ++ )
+            {
+                clientPort = AvailablePortFinder.getNextAvailable( clientPort + 1 );
+                ConnectFuture future = connector.connect(
+                        new InetSocketAddress( InetAddress.getLocalHost(), port ),
+                        new InetSocketAddress( clientPort ),
+                        handler );
+                future.join();
+                
+                try
+                {
+                    session = future.getSession();
+                    break;
+                }
+                catch( BindException e )
+                {
+                    // Try again until we succeed to bind.
+                }
+            }
+
+            if( session == null )
+            {
+                Assert.fail( "Failed to find out an appropriate local address." );
+            }
+        }
+        
+        // Run a basic connector test.
         testConnector0( session );
         
         // Send closeNotify to test TLS closure if it is TLS connection.
@@ -180,7 +187,7 @@
             connector.getFilterChain().remove( "log" );
         }
         
-        session.close();
+        session.close().join();
     }
     
     private void testConnector0( IoSession session ) throws InterruptedException



Mime
View raw message