You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/06/07 11:41:13 UTC

svn commit: r1132926 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/net/ native/os/unix/ test/org/apache/commons/runtime/

Author: mturk
Date: Tue Jun  7 09:41:13 2011
New Revision: 1132926

URL: http://svn.apache.org/viewvc?rev=1132926&view=rev
Log:
Add local endpoint test suite and make sure we unlink the sock on close

Added:
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSocketUtils.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java?rev=1132926&r1=1132925&r2=1132926&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalServerEndpoint.java Tue Jun  7 09:41:13 2011
@@ -45,11 +45,12 @@ public class LocalServerEndpoint extends
     private boolean                     bound    = false;
 
     private static native int           nonblock0(int fd, boolean block);
+    private static native void          unlink0(byte[] addr);
 
     /**
      * Creates a new unbound local server endpoint.
      */
-    private LocalServerEndpoint()
+    public LocalServerEndpoint()
     {
         super(EndpointType.LOCAL);
         this.sd = new LocalDescriptor();
@@ -112,6 +113,8 @@ public class LocalServerEndpoint extends
                     // Ignore selector exceptions
                 }
             }
+            if (sa != null)
+                unlink0(sa.sockaddr());
             sd.close();
         }
         if (key != null) {
@@ -164,12 +167,22 @@ public class LocalServerEndpoint extends
         if (bound)
             throw new IOException(Local.sm.get("endpoint.EBOUND"));
         if (sd.closed())
-            throw new ClosedDescriptorException();
+            sd.create(SocketType.STREAM);
         if (backlog < LISTEN_BACKLOG)
             backlog = LISTEN_BACKLOG;
-        int rc = bind0(sd.fd(), sa.sockaddr(), backlog);
-        if (rc != 0)
-            throw new IOException(Status.describe(rc));            
+        int rc = bind0(sd.fd(), endpoint.sockaddr(), backlog);
+        if (rc != 0) {
+            sd.close();
+            throw new IOException(Status.describe(rc));
+        }
+        bound = true;
+        sa    = endpoint;
+    }
+
+    public void bind(LocalEndpointAddress endpoint)
+        throws IOException
+    {
+        bind(endpoint, LISTEN_BACKLOG);
     }
 
     public LocalEndpoint accept()

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c?rev=1132926&r1=1132925&r2=1132926&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/usock.c Tue Jun  7 09:41:13 2011
@@ -142,6 +142,17 @@ ACR_NET_EXPORT(jint, LocalServerEndpoint
     return rc;
 }
 
+ACR_NET_EXPORT(void, LocalServerEndpoint, unlink0)(JNI_STDARGS, jbyteArray ba)
+{
+    acr_sockaddr_t *sa = SOCKADDR_CAST(ba);
+    if (sa->hostname[0] != 0) {
+        /* Unlink the local socket.
+         */
+        unlink(sa->hostname);
+    }
+    SOCKADDR_RELEASE(ba, sa);
+}
+
 ACR_NET_EXPORT(jint, LocalServerEndpoint, accept0)(JNI_STDARGS, jint fd,
                                                    jbyteArray ba)
 {

Added: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java?rev=1132926&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java (added)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java Tue Jun  7 09:41:13 2011
@@ -0,0 +1,96 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.net;
+
+import java.io.IOException;
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.DatagramSocket;
+import java.net.DatagramSocketImpl;
+import java.net.Socket;
+import java.net.SocketImpl;
+import java.net.ServerSocket;
+import java.net.SocketException;
+import org.testng.annotations.*;
+import org.testng.Assert;
+
+public class TestPosixEndpoint extends Assert
+{
+
+    private Object sync = new Object();
+
+    class Acceptor extends Thread
+    {
+        LocalServerEndpoint ss;
+        public volatile boolean running = true;
+
+        public Acceptor(LocalServerEndpoint ss)
+        {
+            this.ss = ss;
+        }
+
+        public void run() {
+            synchronized(sync) {
+                // Notify that we are ready to
+                // accept the connections
+                sync.notifyAll();
+            }
+            while (running) {
+                try {
+                    LocalEndpoint e = ss.accept();
+                    assertNotNull(e.descriptor());
+                } catch (Exception x) {
+                    fail("Accept failed " + x.toString());
+                }
+            }
+        }
+    }
+
+    @Test(groups = { "posix" })
+    public void connectLocalEndpoint()
+        throws IOException
+    {
+        LocalServerEndpoint  ss = new LocalServerEndpoint();
+        LocalEndpointAddress sa = new LocalEndpointAddress("/tmp/acrposixep1.sock");
+        ss.bind(sa);
+        Acceptor w = new Acceptor(ss);
+        w.start();
+        try {
+            synchronized(sync) {
+                // Wait until Acceptor is ready to accept connections
+                //
+                sync.wait();
+            }
+        } catch (InterruptedException x) {
+            // Ignore
+        }
+        w.running = false;
+        // Create unbound socket
+        // FileDescriptor is null until bind call
+        //
+        LocalEndpoint s = new LocalEndpoint();
+        s.connect(sa);
+        assertFalse(s.isBlocking());
+        s.close();
+        ss.close();
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestPosixEndpoint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSocketUtils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSocketUtils.java?rev=1132926&r1=1132925&r2=1132926&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSocketUtils.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestSocketUtils.java Tue Jun  7 09:41:13 2011
@@ -74,11 +74,11 @@ public class TestSocketUtils extends Ass
         assertNull(Sockets.getFileDescriptor(s));
     }
 
-    class Worker extends Thread
+    class Acceptor extends Thread
     {
         ServerSocket ss;
         
-        public Worker(ServerSocket ss)
+        public Acceptor(ServerSocket ss)
         {
             this.ss = ss;
         }
@@ -101,7 +101,7 @@ public class TestSocketUtils extends Ass
         throws IOException
     {
         ServerSocket ss = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"));
-        Worker w = new Worker(ss);
+        Acceptor w = new Acceptor(ss);
         w.start();
         try {
             synchronized(sync) {
@@ -132,7 +132,7 @@ public class TestSocketUtils extends Ass
         throws IOException
     {
         ServerSocket ss = new ServerSocket(0, 1, InetAddress.getByName("::1"));
-        Worker w = new Worker(ss);
+        Acceptor w = new Acceptor(ss);
         w.start();
         try {
             synchronized(sync) {