/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * A SelectableChannelCloser that tracks if the implCloseChannel and * implReleaseChannel methods are invoked
*/ staticclass Closer implements SelectableChannelCloser { int closeCount;
SelectableChannel invokedToClose; int releaseCount;
SelectableChannel invokedToRelease;
/** * Basic test of channel registered with Selector
*/ publicvoid testSelect() throws IOException {
Selector sel = Selector.open(); try (Connection connection = Connection.open()) {
// create channel with the file descriptor from one end of the connection
FileDescriptor fd = getFD(connection.channel1());
SelectableChannel ch = Channels.readWriteSelectableChannel(fd, new Closer());
// register for read events, channel should not be selected
ch.configureBlocking(false);
SelectionKey key = ch.register(sel, SelectionKey.OP_READ); int n = sel.selectNow();
assertTrue(n == 0);
// write bytes to other end of connection
SocketChannel peer = connection.channel2();
ByteBuffer msg = ByteBuffer.wrap("hello".getBytes("UTF-8")); int nwrote = peer.write(msg);
assertTrue(nwrote >= 0);
// channel should be selected
n = sel.select();
assertTrue(n == 1);
assertTrue(sel.selectedKeys().contains(key));
assertTrue(key.isReadable());
assertFalse(key.isWritable());
sel.selectedKeys().clear();
// change interest set for writing, channel should be selected
key.interestOps(SelectionKey.OP_WRITE);
n = sel.select();
assertTrue(n == 1);
assertTrue(sel.selectedKeys().contains(key));
assertTrue(key.isWritable());
assertFalse(key.isReadable());
sel.selectedKeys().clear();
// change interest set for reading + writing, channel should be selected
key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
n = sel.select();
assertTrue(n == 1);
assertTrue(sel.selectedKeys().contains(key));
assertTrue(key.isWritable());
assertTrue(key.isReadable());
sel.selectedKeys().clear();
// change interest set to 0 to deregister, channel should not be selected
key.interestOps(0);
n = sel.selectNow();
assertTrue(n == 0);
} finally {
sel.close();
}
}
/** * Test that the SelectableChannelCloser implCloseChannel method is invoked.
*/ publicvoid testImplCloseChannel() throws IOException { try (Connection connection = Connection.open()) {
FileDescriptor fd = getFD(connection.channel1());
Closer closer = new Closer();
SelectableChannel ch = Channels.readWriteSelectableChannel(fd, closer);
// close channel twice, checking that the closer is invoked only once for (int i=0; i<2; i++) {
ch.close();
// implCloseChannel should been invoked once
assertTrue(closer.closeCount == 1);
assertTrue(closer.invokedToClose == ch);
// implReleaseChannel should not have been invoked
assertTrue(closer.releaseCount == 0);
}
}
}
/** * Test that the SelectableChannelCloser implReleaseChannel method is invoked.
*/ publicvoid testImplReleaseChannel() throws IOException {
Selector sel = Selector.open(); try (Connection connection = Connection.open()) {
FileDescriptor fd = getFD(connection.channel1());
Closer closer = new Closer();
SelectableChannel ch = Channels.readWriteSelectableChannel(fd, closer);
// register with Selector, invoking selectNow to ensure registered
ch.configureBlocking(false);
ch.register(sel, SelectionKey.OP_WRITE);
sel.selectNow();
// close channel
ch.close();
// implCloseChannel should have been invoked
assertTrue(closer.closeCount == 1);
assertTrue(closer.invokedToClose == ch);
// implReleaseChannel should not have been invoked
assertTrue(closer.releaseCount == 0);
// flush the selector
sel.selectNow();
// implReleaseChannel should have been invoked
assertTrue(closer.releaseCount == 1);
assertTrue(closer.invokedToRelease == ch);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.