Search in sources :

Example 31 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project robovm by robovm.

the class SinkChannelTest method test_write_$LByteBuffer_SinkClosed.

/**
	 * @tests java.nio.channels.Pipe.SinkChannel#write(ByteBuffer[])
	 */
public void test_write_$LByteBuffer_SinkClosed() throws IOException {
    ByteBuffer[] bufArray = { buffer };
    sink.close();
    try {
        sink.write(bufArray);
        fail("should throw ClosedChannelException");
    } catch (ClosedChannelException e) {
    // expected
    }
    ByteBuffer[] nullBufArrayRef = null;
    try {
        sink.write(nullBufArrayRef);
        fail("should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    ByteBuffer nullBuf = null;
    ByteBuffer[] nullBufArray = { nullBuf };
    // write ByteBuffer[] contains null element
    try {
        sink.write(nullBufArray);
        fail("should throw ClosedChannelException");
    } catch (ClosedChannelException e) {
    // expected
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ByteBuffer(java.nio.ByteBuffer)

Example 32 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project robovm by robovm.

the class SelectorTest method assert_select_OP_CONNECT.

private void assert_select_OP_CONNECT(SelectType type, int timeout) throws IOException, ClosedChannelException {
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    sc.register(selector, SelectionKey.OP_CONNECT);
    try {
        sc.connect(localAddress);
        int count = blockingSelect(type, timeout);
        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(type, timeout);
        assertEquals(0, count);
        // but selectedKeys remains the same as previous
        assertSame(selectedKeys, selector.selectedKeys());
        sc.finishConnect();
        selectedKeys.clear();
    } finally {
        try {
            ssc.accept().close();
        } catch (Exception e) {
        // do nothing
        }
        try {
            sc.close();
        } catch (IOException e) {
        // do nothing
        }
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 33 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project robovm by robovm.

the class SocketChannelTest method testWriteByteBuffer_Direct.

public void testWriteByteBuffer_Direct() throws IOException {
    assertTrue(this.server1.isBound());
    java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL);
    assertFalse(this.channel1.isRegistered());
    assertTrue(this.channel1.isBlocking());
    assertFalse(this.channel1.isConnected());
    assertFalse(this.channel1.isConnectionPending());
    assertTrue(this.channel1.isOpen());
    try {
        channel1.write(writeBuf);
        fail("Should throw NotYetConnectedException");
    } catch (NotYetConnectedException e) {
    // correct
    }
    this.channel1.connect(localAddr1);
    assertTrue(this.channel1.isBlocking());
    assertTrue(this.channel1.isConnected());
    assertFalse(this.channel1.isConnectionPending());
    assertTrue(this.channel1.isOpen());
    assertEquals(CAPACITY_NORMAL, this.channel1.write(writeBuf));
    this.channel1.close();
    try {
        channel1.write(writeBuf);
        fail("Should throw ClosedChannelException");
    } catch (ClosedChannelException e) {
    // correct
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) NotYetConnectedException(java.nio.channels.NotYetConnectedException) ByteBuffer(java.nio.ByteBuffer)

Example 34 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project robovm by robovm.

the class SocketChannelTest method test_56684.

public void test_56684() throws Exception {
    mockOs.enqueueFault("connect", ENETUNREACH);
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    Selector selector = Selector.open();
    SelectionKey selectionKey = sc.register(selector, SelectionKey.OP_CONNECT);
    try {
        sc.connect(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }), 0));
        fail();
    } catch (ConnectException ex) {
    }
    try {
        sc.finishConnect();
    } catch (ClosedChannelException expected) {
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException) InetSocketAddress(java.net.InetSocketAddress) Selector(java.nio.channels.Selector) ConnectException(java.net.ConnectException)

Example 35 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project vert.x by eclipse.

the class Http2ServerTest method testIdleTimeout.

@Test
public void testIdleTimeout() throws Exception {
    waitFor(5);
    server.close();
    server = vertx.createHttpServer(serverOptions.setIdleTimeout(2));
    server.requestHandler(req -> {
        req.exceptionHandler(err -> {
            assertTrue(err instanceof ClosedChannelException);
            complete();
        });
        req.response().closeHandler(v -> {
            complete();
        });
        req.response().endHandler(v -> {
            complete();
        });
        req.connection().closeHandler(v -> {
            complete();
        });
    });
    startServer();
    TestClient client = new TestClient();
    ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
        int id = request.nextStreamId();
        request.decoder.frameListener(new Http2EventAdapter() {
        });
        request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise());
        request.context.flush();
    });
    fut.sync();
    fut.channel().closeFuture().addListener(v1 -> {
        vertx.runOnContext(v2 -> {
            complete();
        });
    });
    await();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ClosedChannelException(java.nio.channels.ClosedChannelException) Http2EventAdapter(io.netty.handler.codec.http2.Http2EventAdapter) Test(org.junit.Test)

Aggregations

ClosedChannelException (java.nio.channels.ClosedChannelException)211 ByteBuffer (java.nio.ByteBuffer)67 IOException (java.io.IOException)60 Test (org.junit.Test)23 InetSocketAddress (java.net.InetSocketAddress)19 SelectionKey (java.nio.channels.SelectionKey)18 SocketChannel (java.nio.channels.SocketChannel)15 ArrayList (java.util.ArrayList)13 NotYetConnectedException (java.nio.channels.NotYetConnectedException)11 InterruptedIOException (java.io.InterruptedIOException)10 CancelledKeyException (java.nio.channels.CancelledKeyException)10 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)9 File (java.io.File)9 ServerSocketChannel (java.nio.channels.ServerSocketChannel)9 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)8 FileChannel (java.nio.channels.FileChannel)8 ConnectException (java.net.ConnectException)7 FsVolumeReference (org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeReference)6 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)5 Command (com.cloud.agent.api.Command)5