Search in sources :

Example 26 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project hadoop by apache.

the class TestClose method testWriteAfterClose.

@Test
public void testWriteAfterClose() throws IOException {
    Configuration conf = new Configuration();
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build();
    try {
        final byte[] data = "foo".getBytes();
        FileSystem fs = FileSystem.get(conf);
        OutputStream out = fs.create(new Path("/test"));
        out.write(data);
        out.close();
        try {
            // Should fail.
            out.write(data);
            fail("Should not have been able to write more data after file is closed.");
        } catch (ClosedChannelException cce) {
        // We got the correct exception. Ignoring.
        }
        // Should succeed. Double closes are OK.
        out.close();
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ClosedChannelException(java.nio.channels.ClosedChannelException) Configuration(org.apache.hadoop.conf.Configuration) FileSystem(org.apache.hadoop.fs.FileSystem) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 27 with ClosedChannelException

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

the class SocketChannelTest method testWriteByteBuffer.

public void testWriteByteBuffer() throws IOException {
    assertTrue(this.server1.isBound());
    java.nio.ByteBuffer writeBuf = java.nio.ByteBuffer.allocate(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 28 with ClosedChannelException

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

the class SocketChannelTest method testReadByteBuffer.

// -------------------------------------------------
// Test for read/write but no real data expressed
// -------------------------------------------------
public void testReadByteBuffer() throws Exception {
    assertTrue(this.server1.isBound());
    java.nio.ByteBuffer readBuf = java.nio.ByteBuffer.allocate(CAPACITY_NORMAL);
    assertFalse(this.channel1.isRegistered());
    assertTrue(this.channel1.isBlocking());
    assertFalse(this.channel1.isConnected());
    assertFalse(this.channel1.isConnectionPending());
    assertTrue(this.channel1.isOpen());
    // note: blocking-mode will make the read process endless!
    this.channel1.configureBlocking(false);
    try {
        channel1.read(readBuf);
        fail("Should throw NotYetConnectedException");
    } catch (NotYetConnectedException e) {
    // correct
    }
    boolean connected = this.channel1.connect(localAddr1);
    if (!connected) {
        assertFalse(this.channel1.isBlocking());
        assertTrue(this.channel1.isConnectionPending());
        assertFalse(this.channel1.isConnected());
    }
    if (tryFinish()) {
        assertEquals(0, this.channel1.read(readBuf));
    }
    this.channel1.close();
    try {
        channel1.read(readBuf);
        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 29 with ClosedChannelException

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

the class ServerSocketChannelTest method test_socket_accept_Blocking_Bound.

/**
     * @tests ServerSocket#socket().accept()
     */
public void test_socket_accept_Blocking_Bound() throws IOException {
    // regression test for Harmony-748
    serverChannel.configureBlocking(true);
    ServerSocket gotSocket = serverChannel.socket();
    gotSocket.bind(localAddr1);
    serverChannel.close();
    try {
        gotSocket.accept();
        fail("Should throw a ClosedChannelException");
    } catch (ClosedChannelException e) {
    // expected
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ServerSocket(java.net.ServerSocket)

Example 30 with ClosedChannelException

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

the class ServerSocketChannelTest method test_socket_accept_Nonblocking_Bound.

/**
     * @tests ServerSocket#socket().accept()
     */
public void test_socket_accept_Nonblocking_Bound() throws IOException {
    // regression test for Harmony-748
    serverChannel.configureBlocking(false);
    ServerSocket gotSocket = serverChannel.socket();
    gotSocket.bind(localAddr1);
    try {
        gotSocket.accept();
        fail("Should throw an IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    serverChannel.close();
    try {
        gotSocket.accept();
        fail("Should throw a ClosedChannelException");
    } catch (ClosedChannelException e) {
    // expected
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ServerSocket(java.net.ServerSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

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