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.
git-svn-id: https://svn.apache.org/repos/asf/harmony/enhanced/java/trunk@1002476 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c b/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
index 7e36b7c..0110469 100644
--- a/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
+++ b/classlib/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
@@ -346,14 +346,14 @@
/* 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;
}
diff --git a/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java b/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java
index 016ff5b..246eb6b 100644
--- a/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java
+++ b/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectionKeyImpl.java
@@ -39,12 +39,15 @@
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 @@
throw new IllegalArgumentException();
}
synchronized (selector.keysLock) {
+ if (interestOps != operations) {
+ stateChange = true;
+ }
interestOps = operations;
selector.modKey(this);
}
@@ -105,6 +111,14 @@
this.index = index;
}
+ boolean getStateChange() {
+ return stateChange;
+ }
+
+ void setStateChange(boolean flag) {
+ this.stateChange = flag;
+ }
+
private void checkValid() {
if (!isValid()) {
throw new CancelledKeyException();
diff --git a/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java b/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java
index 4a03ccd..4e162a9 100644
--- a/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java
+++ b/classlib/modules/nio/src/main/java/common/org/apache/harmony/nio/internal/SelectorImpl.java
@@ -563,20 +563,20 @@
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);
}
}
}
diff --git a/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java b/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java
index 0303d5b..2492bf5 100644
--- a/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java
+++ b/classlib/modules/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectorTest.java
@@ -19,21 +19,18 @@
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 @@
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
diff --git a/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java b/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java
new file mode 100644
index 0000000..1648c0f
--- /dev/null
+++ b/classlib/modules/nio/src/test/java/unix/org/apache/harmony/nio/tests/java/nio/channels/UnixSelectorTest.java
@@ -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));
+ }
+}