harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From litt...@apache.org
Subject svn commit: r1002476 - in /harmony/enhanced/java/trunk/classlib/modules: luni/src/main/native/luni/unix/ nio/src/main/java/common/org/apache/harmony/nio/internal/ nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/ nio/src/test/jav...
Date Wed, 29 Sep 2010 05:26:49 GMT
Author: littlee
Date: Wed Sep 29 05:26:48 2010
New Revision: 1002476

URL: http://svn.apache.org/viewvc?rev=1002476&view=rev
Log:
Applying patch from HARMONY-6638:
1. In Linux, ServerSocketChannel should be acceptable if its server socket bind any address.
2. In Linux, SocketChannel should be connectable if it does not connect anything.
3. The Selector should also return if an error occurred.
4. If user change the interested operations of a selector, this
selector should treated as a new one (but with the same
SelectionKey). On the other hand, if the selector was return once and
no interested operation changed, it should not  be selected next time.

Added:
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java
  (with props)
Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
    harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java
    harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java
    harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c?rev=1002476&r1=1002475&r2=1002476&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
Wed Sep 29 05:26:48 2010
@@ -346,14 +346,14 @@ Java_org_apache_harmony_luni_platform_OS
 	  /* output result to int array */
 	  flagArray = (*env)->GetIntArrayElements(env, outFlags, &isCopy);
 	  for (val=0; val<countReadC; val++) {
-          if (my_pollfds[val].revents & (POLLIN | POLLPRI)) {
+          if (my_pollfds[val].revents & (POLLIN | POLLPRI | POLLHUP | POLLERR)) {
               flagArray[val] = SOCKET_OP_READ;
               changed=1;
           }
       }
 
 	  for (val=0; val<countWriteC; val++) {
-          if (my_pollfds[val+countReadC].revents & POLLOUT) {
+            if (my_pollfds[val+countReadC].revents & (POLLOUT | POLLHUP | POLLERR)) {
               flagArray[val+countReadC] = SOCKET_OP_WRITE;
               changed=1;
           }

Modified: harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java?rev=1002476&r1=1002475&r2=1002476&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java
Wed Sep 29 05:26:48 2010
@@ -39,12 +39,15 @@ final class SelectionKeyImpl extends Abs
 
     private int index;
 
+    private boolean stateChange = false;
+
     public SelectionKeyImpl(AbstractSelectableChannel channel, int operations,
             Object attachment, SelectorImpl selector) {
         this.channel = channel;
         interestOps = operations;
         this.selector = selector;
         attach(attachment);
+        this.stateChange = true;
     }
 
     @Override
@@ -73,6 +76,9 @@ final class SelectionKeyImpl extends Abs
             throw new IllegalArgumentException();
         }
         synchronized (selector.keysLock) {
+            if (interestOps != operations) {
+                stateChange = true;
+            }
             interestOps = operations;
             selector.modKey(this);
         }
@@ -105,6 +111,14 @@ final class SelectionKeyImpl extends Abs
         this.index = index;
     }
 
+    boolean getStateChange() {
+        return stateChange;
+    }
+
+    void setStateChange(boolean flag) {
+        this.stateChange = flag;
+    }
+
     private void checkValid() {
         if (!isValid()) {
             throw new CancelledKeyException();

Modified: harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java?rev=1002476&r1=1002475&r2=1002476&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java
Wed Sep 29 05:26:48 2010
@@ -563,20 +563,20 @@ final class SelectorImpl extends Abstrac
                     if (key.isConnected()) {
                         selectedOp = OP_WRITE & ops;
                     } else {
-                        selectedOp = OP_CONNECT & ops;
+                        selectedOp = (OP_CONNECT | OP_WRITE) & ops;
                     }
                     break;
             }
 
+            key.setReadyOps(selectedOp);
             if (0 != selectedOp) {
                 boolean wasSelected = mutableSelectedKeys.contains(key);
-                if (wasSelected && key.readyOps() != selectedOp) {
-                    key.setReadyOps(key.readyOps() | selectedOp);
-                    selected++;
-                } else if (!wasSelected) {
-                    key.setReadyOps(selectedOp);
+                if (!wasSelected) {
                     mutableSelectedKeys.add(key);
+                }
+                if (key.getStateChange()) {
                     selected++;
+                    key.setStateChange(false);
                 }
             }
         }

Modified: harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java?rev=1002476&r1=1002475&r2=1002476&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java
Wed Sep 29 05:26:48 2010
@@ -19,21 +19,18 @@ package org.apache.harmony.nio.tests.jav
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.ClosedSelectorException;
+import java.nio.channels.Pipe;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
-import java.nio.channels.Pipe;
 import java.nio.channels.spi.SelectorProvider;
 import java.util.Set;
-import java.util.Collections;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
 
 import junit.framework.TestCase;
 import tests.support.Support_PortManager;
@@ -406,6 +403,52 @@ public class SelectorTest extends TestCa
         assertNull(failure.get());
     }
 
+    public void testOpChange() throws Exception {
+        SocketChannel sc = SocketChannel.open();
+        sc.configureBlocking(false);
+        sc.register(selector, SelectionKey.OP_CONNECT);
+        try {
+            sc.connect(LOCAL_ADDRESS);
+            int count = blockingSelect(SelectType.TIMEOUT, 100);
+            assertEquals(1, count);
+            Set<SelectionKey> selectedKeys = selector.selectedKeys();
+            assertEquals(1, selectedKeys.size());
+            SelectionKey key = selectedKeys.iterator().next();
+            assertEquals(sc.keyFor(selector), key);
+            assertEquals(SelectionKey.OP_CONNECT, key.readyOps());
+            // select again, it should return 0
+            count = selectOnce(SelectType.TIMEOUT, 100);
+            assertEquals(0, count);
+            // but selectedKeys remains the same as previous
+            assertSame(selectedKeys, selector.selectedKeys());
+            sc.finishConnect();
+
+            // same selector, but op is changed
+            SelectionKey key1 = sc.register(selector, SelectionKey.OP_WRITE);
+            assertEquals(key, key1);
+            count = blockingSelect(SelectType.TIMEOUT, 100);
+            assertEquals(1, count);
+            selectedKeys = selector.selectedKeys();
+            assertEquals(1, selectedKeys.size());
+            key = selectedKeys.iterator().next();
+            assertEquals(key, key1);
+            assertEquals(SelectionKey.OP_WRITE, key.readyOps());
+
+            selectedKeys.clear();
+        } finally {
+            try {
+                ssc.accept().close();
+            } catch (Exception e) {
+                // do nothing
+            }
+            try {
+                sc.close();
+            } catch (IOException e) {
+                // do nothing
+            }
+        }
+    }
+
     private void assert_select_SelectorClosed(SelectType type, int timeout)
             throws IOException {
         // selector is closed

Added: harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java?rev=1002476&view=auto
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java
(added)
+++ harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java
Wed Sep 29 05:26:48 2010
@@ -0,0 +1,133 @@
+/* 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.harmony.nio.tests.java.nio.channels;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+
+import junit.framework.TestCase;
+
+public class UnixSelectorTest extends TestCase {
+    static class Server {
+        private ServerSocketChannel serverChannel = ServerSocketChannel.open();
+        private ServerSocket socket = null;
+
+        Server() throws Exception {
+            serverChannel.configureBlocking(false);
+        }
+
+        public void initialize() throws Exception {
+            this.socket = serverChannel.socket();
+            socket.bind(new InetSocketAddress("localhost", 0));
+        }
+
+        public int getPort() {
+            return socket.getLocalPort();
+        }
+
+        public boolean isOpen() {
+            return !socket.isClosed();
+        }
+
+        public ServerSocketChannel getServerChannel() {
+            return serverChannel;
+        }
+
+        public void accept() {
+            Thread serverThread = new Thread(new Runnable() {
+                public void run() {
+                    try {
+                        while (serverChannel.accept() == null) {
+                            Thread.sleep(1000);
+                        }
+                    } catch (Exception e) {}
+                }
+            });
+            serverThread.start();
+        }
+
+        public void close() throws Exception{
+            serverChannel.close();
+        }
+    }
+
+    public void testSelectorAcceptAndRead() throws Exception {
+        Selector sel0 = Selector.open();
+        Selector sel1 = Selector.open();
+        Server server = new Server();
+        SelectableChannel serverChannel = server.getServerChannel();
+        SelectionKey mkey0 = serverChannel.register(sel0, SelectionKey.OP_ACCEPT);
+        serverChannel.register(sel1, SelectionKey.OP_ACCEPT);
+
+        // HUP is treating as acceptable
+        assertThat(sel0.select(100), is(1));
+        assertThat(sel0.selectedKeys().contains(mkey0), is(true));
+        server.initialize();
+        // after bind can not accept
+        assertThat(sel1.select(100), is(0));
+        server.accept();
+        Thread.sleep(1000);
+        int port = server.getPort();
+        SocketChannel socketChannel = SocketChannel.open();
+        socketChannel.configureBlocking(false);
+        Selector sel2 = Selector.open();
+        socketChannel.register(sel2, SelectionKey.OP_WRITE);
+        boolean isConnected = socketChannel.connect(new InetSocketAddress(port));
+        if (!isConnected) {
+            socketChannel.finishConnect();
+        }
+
+        assertThat(socketChannel.isConnected(), is(true));
+        server.close();
+        Thread.sleep(3000);
+        assertThat(socketChannel.isConnected(), is(true));
+        assertThat(sel2.select(100), is(1));
+    }
+
+    public void testSelectUnConnectedChannel() throws Exception {
+        SocketChannel socketChannel2 = SocketChannel.open();
+        socketChannel2.configureBlocking(false);
+        Selector sel3 = Selector.open();
+        SelectionKey mkey3 = socketChannel2.register(sel3, SelectionKey.OP_WRITE);
+        // HUP is also treating as writable
+        assertThat(sel3.select(100), is(1));
+        assertThat(mkey3.isConnectable(), is(false));
+        // even the channel is not connected, the selector could be writable
+        assertThat(socketChannel2.isConnected(), is(false));
+        assertThat(mkey3.isWritable(), is(true));
+
+        Selector sel4 = Selector.open();
+        SelectionKey mkey4 = socketChannel2.register(sel4, SelectionKey.OP_CONNECT);
+        assertThat(sel4.select(100), is(1));
+        assertThat(mkey4.isWritable(), is(false));
+        assertThat(mkey4.isConnectable(), is(true));
+
+        Selector sel5 = Selector.open();
+        SelectionKey mkey5 = socketChannel2.register(sel5, SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE);
+        assertThat(sel5.select(100), is(1));
+        assertThat(mkey5.isWritable(), is(true));
+        assertThat(mkey5.isConnectable(), is(true));
+    }
+}

Propchange: harmony/enhanced/java/trunk/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message