Search in sources :

Example 96 with SocketChannel

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

the class TestWebHdfsTimeouts method setUp.

@Before
public void setUp() throws Exception {
    Configuration conf = WebHdfsTestUtil.createConf();
    serverSocket = new ServerSocket(0, CONNECTION_BACKLOG);
    nnHttpAddress = new InetSocketAddress("localhost", serverSocket.getLocalPort());
    conf.set(DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY, "localhost:" + serverSocket.getLocalPort());
    if (timeoutSource == TimeoutSource.Configuration) {
        String v = Integer.toString(SHORT_SOCKET_TIMEOUT) + "ms";
        conf.set(HdfsClientConfigKeys.DFS_WEBHDFS_SOCKET_CONNECT_TIMEOUT_KEY, v);
        conf.set(HdfsClientConfigKeys.DFS_WEBHDFS_SOCKET_READ_TIMEOUT_KEY, v);
    }
    fs = WebHdfsTestUtil.getWebHdfsFileSystem(conf, WebHdfsConstants.WEBHDFS_SCHEME);
    if (timeoutSource == TimeoutSource.ConnectionFactory) {
        fs.connectionFactory = connectionFactory;
    }
    clients = new ArrayList<SocketChannel>();
    serverThread = null;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Before(org.junit.Before)

Example 97 with SocketChannel

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

the class SocketChannelTest method test_socketChannel_read_ByteBufferII_remoteClosed.

/**
     * @tests SocketChannel#read(ByteBuffer[], int, int) when remote server
     *        closed
     */
public void test_socketChannel_read_ByteBufferII_remoteClosed() throws Exception {
    // regression 1 for HARMONY-549
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    ssc.accept().close();
    ByteBuffer[] buf = { ByteBuffer.allocate(10) };
    assertEquals(-1, sc.read(buf, 0, 1));
    ssc.close();
    sc.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 98 with SocketChannel

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

the class SocketChannelTest method test_write$LByteBuffer_invalid.

/**
     * @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
     */
public void test_write$LByteBuffer_invalid() throws IOException {
    // Set-up
    ServerSocketChannel server = ServerSocketChannel.open();
    server.socket().bind(null);
    SocketChannel client = SocketChannel.open();
    client.connect(server.socket().getLocalSocketAddress());
    SocketChannel worker = server.accept();
    // Do some stuff
    try {
        client.write((ByteBuffer[]) null);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        client.write((ByteBuffer[]) null, 0, 0);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        client.write((ByteBuffer[]) null, 1, 0);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        client.write((ByteBuffer[]) null, 0, 1);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        client.write((ByteBuffer[]) null, 1, 1);
        fail("Should throw a NPE");
    } catch (NullPointerException e) {
    // expected
    }
    ByteBuffer[] buffers = new ByteBuffer[1];
    buffers[0] = ByteBuffer.wrap("Hello ".getBytes("UTF-8"));
    try {
        client.write(buffers, -1, 1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        client.write(buffers, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        client.write(buffers, 0, 2);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        client.write(buffers, 2, 0);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        client.write(null, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    }
    // Tidy-up
    worker.close();
    client.close();
    server.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 99 with SocketChannel

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

the class SocketChannelTest method test_socketChannel_write_ByteBuffer_posNotZero.

/**
     * @tests SocketChannel#write(ByteBuffer) if position is not zero
     */
public void test_socketChannel_write_ByteBuffer_posNotZero() throws Exception {
    // regression 5 for HARMONY-549
    final String testStr = "Hello World";
    ByteBuffer readBuf = ByteBuffer.allocate(11);
    ByteBuffer buf = ByteBuffer.wrap(testStr.getBytes());
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    buf.position(2);
    ssc.accept().write(buf);
    assertEquals(9, sc.read(readBuf));
    buf.flip();
    readBuf.flip();
    byte[] read = new byte[9];
    byte[] write = new byte[11];
    buf.get(write);
    readBuf.get(read);
    for (int i = 0; i < 9; i++) {
        assertEquals(read[i], write[i + 2]);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 100 with SocketChannel

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

the class SocketChannelTest method test_socketChannel_write_ByteBufferII.

/**
     * @tests SocketChannel#write(ByteBuffer[], int, int)
     */
public void test_socketChannel_write_ByteBufferII() throws Exception {
    // regression 2 for HARMONY-549
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr2);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr2);
    SocketChannel sock = ssc.accept();
    ByteBuffer[] buf = { ByteBuffer.allocate(10), null };
    try {
        sc.write(buf, 0, 2);
        fail("should throw NPE");
    } catch (NullPointerException e) {
    // expected
    }
    ssc.close();
    sc.close();
    ByteBuffer target = ByteBuffer.allocate(10);
    assertEquals(-1, sock.read(target));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ByteBuffer(java.nio.ByteBuffer) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

SocketChannel (java.nio.channels.SocketChannel)662 IOException (java.io.IOException)298 ServerSocketChannel (java.nio.channels.ServerSocketChannel)285 InetSocketAddress (java.net.InetSocketAddress)202 ByteBuffer (java.nio.ByteBuffer)163 SelectionKey (java.nio.channels.SelectionKey)105 Socket (java.net.Socket)87 Test (org.junit.Test)81 ClosedChannelException (java.nio.channels.ClosedChannelException)50 Selector (java.nio.channels.Selector)42 SocketAddress (java.net.SocketAddress)35 ServerSocket (java.net.ServerSocket)31 ClosedSelectorException (java.nio.channels.ClosedSelectorException)28 CancelledKeyException (java.nio.channels.CancelledKeyException)27 ConnectException (java.net.ConnectException)26 ArrayList (java.util.ArrayList)26 SocketTimeoutException (java.net.SocketTimeoutException)23 SelectableChannel (java.nio.channels.SelectableChannel)21 SocketException (java.net.SocketException)20 HashMap (java.util.HashMap)20