use of java.nio.channels.Selector in project robovm by robovm.
the class AbstractSelectorTest method test_isOpen.
/**
* @tests AbstractSelector#isOpen()
*/
public void test_isOpen() throws Exception {
Selector acceptSelector = SelectorProvider.provider().openSelector();
assertTrue(acceptSelector.isOpen());
acceptSelector.close();
assertFalse(acceptSelector.isOpen());
}
use of java.nio.channels.Selector in project robovm by robovm.
the class AbstractSelectorTest method test_register_LSelectorI_error.
/**
* @tests AbstractSelector#register(Selector,int)
*/
public void test_register_LSelectorI_error() throws IOException {
Selector acceptSelector = SelectorProvider.provider().openSelector();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
acceptSelector.close();
assertFalse(acceptSelector.isOpen());
try {
ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
fail("should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
assertFalse(ssc.isRegistered());
acceptSelector = Selector.open();
ssc.configureBlocking(true);
try {
ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
fail("should throw IllegalBlockingModeException");
} catch (IllegalBlockingModeException e) {
// expected
}
assertFalse(ssc.isRegistered());
ssc.configureBlocking(false);
SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
assertNotNull(acceptKey);
assertTrue(acceptSelector.keys().contains(acceptKey));
assertTrue(ssc.isRegistered());
}
use of java.nio.channels.Selector in project robovm by robovm.
the class SelectorTest method test_nonBlockingConnect.
public void test_nonBlockingConnect() throws IOException {
SocketChannel channel = null;
try {
channel = SocketChannel.open();
channel.configureBlocking(false);
Selector selector = Selector.open();
channel.register(selector, SelectionKey.OP_CONNECT);
channel.connect(localAddress);
channel.finishConnect();
selector.select();
assertEquals(0, selector.selectedKeys().size());
} finally {
channel.close();
}
}
use of java.nio.channels.Selector in project robovm by robovm.
the class UnixSelectorTest method testSelectorAcceptAndRead.
public void testSelectorAcceptAndRead() throws Exception {
Selector sel0 = Selector.open();
Selector sel1 = Selector.open();
Server server = new Server();
SelectionKey mkey0 = server.serverChannel.register(sel0, SelectionKey.OP_ACCEPT);
server.serverChannel.register(sel1, SelectionKey.OP_ACCEPT);
// HUP is treating as acceptable
assertEquals(1, sel0.select(100));
assertEquals(true, sel0.selectedKeys().contains(mkey0));
server.initialize();
// after bind can not accept
assertEquals(0, sel1.select(100));
server.accept();
Thread.sleep(1000);
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
Selector sel2 = Selector.open();
socketChannel.register(sel2, SelectionKey.OP_WRITE);
boolean isConnected = socketChannel.connect(server.socket.getLocalSocketAddress());
if (!isConnected) {
socketChannel.finishConnect();
}
assertEquals(true, socketChannel.isConnected());
server.close();
Thread.sleep(3000);
assertEquals(true, socketChannel.isConnected());
assertEquals(1, sel2.select(100));
}
use of java.nio.channels.Selector in project netty by netty.
the class NioEventLoop method rebuildSelector0.
private void rebuildSelector0() {
final Selector oldSelector = selector;
final Selector newSelector;
if (oldSelector == null) {
return;
}
try {
newSelector = openSelector();
} catch (Exception e) {
logger.warn("Failed to create a new Selector.", e);
return;
}
// Register all channels to the new Selector.
int nChannels = 0;
for (SelectionKey key : oldSelector.keys()) {
Object a = key.attachment();
try {
if (!key.isValid() || key.channel().keyFor(newSelector) != null) {
continue;
}
int interestOps = key.interestOps();
key.cancel();
SelectionKey newKey = key.channel().register(newSelector, interestOps, a);
if (a instanceof AbstractNioChannel) {
// Update SelectionKey
((AbstractNioChannel) a).selectionKey = newKey;
}
nChannels++;
} catch (Exception e) {
logger.warn("Failed to re-register a Channel to the new Selector.", e);
if (a instanceof AbstractNioChannel) {
AbstractNioChannel ch = (AbstractNioChannel) a;
ch.unsafe().close(ch.unsafe().voidPromise());
} else {
@SuppressWarnings("unchecked") NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
invokeChannelUnregistered(task, key, e);
}
}
}
selector = newSelector;
try {
// time to close the old selector as everything else is registered to the new one
oldSelector.close();
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to close the old Selector.", t);
}
}
logger.info("Migrated " + nChannels + " channel(s) to the new Selector.");
}
Aggregations