Search in sources :

Example 21 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestServer method testBindSimple.

@Test
public void testBindSimple() throws Exception {
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    Server.bind(socket, address, 10);
    try {
        assertTrue(socket.isBound());
    } finally {
        socket.close();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 22 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestServer method testEmptyConfig.

@Test
public void testEmptyConfig() throws Exception {
    Configuration conf = new Configuration();
    conf.set("TestRange", "");
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    try {
        Server.bind(socket, address, 10, conf, "TestRange");
        assertTrue(socket.isBound());
    } finally {
        socket.close();
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 23 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestServer method testBindError.

@Test
public void testBindError() throws Exception {
    Configuration conf = new Configuration();
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    socket.bind(address);
    try {
        int min = socket.getLocalPort();
        conf.set("TestRange", min + "-" + min);
        ServerSocket socket2 = new ServerSocket();
        InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
        boolean caught = false;
        try {
            Server.bind(socket2, address2, 10, conf, "TestRange");
        } catch (BindException e) {
            caught = true;
        } finally {
            socket2.close();
        }
        assertTrue("Failed to catch the expected bind exception", caught);
    } finally {
        socket.close();
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 24 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestWebHdfsContentLength method setup.

@BeforeClass
public static void setup() throws IOException {
    listenSocket = new ServerSocket();
    listenSocket.bind(null);
    bindAddr = NetUtils.getHostPortString((InetSocketAddress) listenSocket.getLocalSocketAddress());
    redirectResponse = "HTTP/1.1 307 Redirect\r\n" + "Location: http://" + bindAddr + "/path\r\n" + "Connection: close\r\n\r\n";
    p = new Path("webhdfs://" + bindAddr + "/path");
    fs = p.getFileSystem(new Configuration());
    executor = Executors.newSingleThreadExecutor();
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) BeforeClass(org.junit.BeforeClass)

Example 25 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestSaslDataTransfer method TestPeerFromSocketAndKeyReadTimeout.

/**
   * Verifies that peerFromSocketAndKey honors socket read timeouts.
   */
@Test(timeout = 60000)
public void TestPeerFromSocketAndKeyReadTimeout() throws Exception {
    HdfsConfiguration conf = createSecureConfig("authentication,integrity,privacy");
    AtomicBoolean fallbackToSimpleAuth = new AtomicBoolean(false);
    SaslDataTransferClient saslClient = new SaslDataTransferClient(conf, DataTransferSaslUtil.getSaslPropertiesResolver(conf), TrustedChannelResolver.getInstance(conf), fallbackToSimpleAuth);
    DatanodeID fakeDatanodeId = new DatanodeID("127.0.0.1", "localhost", "beefbeef-beef-beef-beef-beefbeefbeef", 1, 2, 3, 4);
    DataEncryptionKeyFactory dataEncKeyFactory = new DataEncryptionKeyFactory() {

        @Override
        public DataEncryptionKey newDataEncryptionKey() {
            return new DataEncryptionKey(123, "456", new byte[8], new byte[8], 1234567, "fakeAlgorithm");
        }
    };
    ServerSocket serverSocket = null;
    Socket socket = null;
    try {
        serverSocket = new ServerSocket(0, -1);
        socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
        Peer peer = DFSUtilClient.peerFromSocketAndKey(saslClient, socket, dataEncKeyFactory, new Token(), fakeDatanodeId, 1);
        peer.close();
        Assert.fail("Expected DFSClient#peerFromSocketAndKey to time out.");
    } catch (SocketTimeoutException e) {
        GenericTestUtils.assertExceptionContains("Read timed out", e);
    } finally {
        IOUtils.cleanup(null, socket, serverSocket);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DatanodeID(org.apache.hadoop.hdfs.protocol.DatanodeID) DataEncryptionKey(org.apache.hadoop.hdfs.security.token.block.DataEncryptionKey) SocketTimeoutException(java.net.SocketTimeoutException) Peer(org.apache.hadoop.hdfs.net.Peer) ServerSocket(java.net.ServerSocket) Token(org.apache.hadoop.security.token.Token) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

ServerSocket (java.net.ServerSocket)655 IOException (java.io.IOException)306 Socket (java.net.Socket)254 InetSocketAddress (java.net.InetSocketAddress)126 Test (org.junit.Test)94 SocketException (java.net.SocketException)54 InputStream (java.io.InputStream)48 SocketTimeoutException (java.net.SocketTimeoutException)42 OutputStream (java.io.OutputStream)40 InetAddress (java.net.InetAddress)39 BindException (java.net.BindException)27 URL (java.net.URL)26 InputStreamReader (java.io.InputStreamReader)23 File (java.io.File)22 UnknownHostException (java.net.UnknownHostException)22 SSLSocket (javax.net.ssl.SSLSocket)21 BufferedReader (java.io.BufferedReader)20 SSLServerSocket (javax.net.ssl.SSLServerSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)15 SocketChannel (java.nio.channels.SocketChannel)14