Search in sources :

Example 96 with InetSocketAddress

use of java.net.InetSocketAddress 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 97 with InetSocketAddress

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

the class TestIPC method testRTEDuringConnectionSetup.

/**
   * Test that, if a RuntimeException is thrown after creating a socket
   * but before successfully connecting to the IPC server, that the
   * failure is handled properly. This is a regression test for
   * HADOOP-7428.
   */
@Test(timeout = 60000)
public void testRTEDuringConnectionSetup() throws IOException {
    // Set up a socket factory which returns sockets which
    // throw an RTE when setSoTimeout is called.
    SocketFactory spyFactory = spy(NetUtils.getDefaultSocketFactory(conf));
    Mockito.doAnswer(new Answer<Socket>() {

        @Override
        public Socket answer(InvocationOnMock invocation) throws Throwable {
            Socket s = spy((Socket) invocation.callRealMethod());
            doThrow(new RuntimeException("Injected fault")).when(s).setSoTimeout(anyInt());
            return s;
        }
    }).when(spyFactory).createSocket();
    Server server = new TestServer(1, true);
    Client client = new Client(LongWritable.class, conf, spyFactory);
    server.start();
    try {
        // Call should fail due to injected exception.
        InetSocketAddress address = NetUtils.getConnectAddress(server);
        try {
            call(client, RANDOM.nextLong(), address, conf);
            fail("Expected an exception to have been thrown");
        } catch (Exception e) {
            LOG.info("caught expected exception", e);
            assertTrue(StringUtils.stringifyException(e).contains("Injected fault"));
        }
        // Resetting to the normal socket behavior should succeed
        // (i.e. it should not have cached a half-constructed connection)
        Mockito.reset(spyFactory);
        call(client, RANDOM.nextLong(), address, conf);
    } finally {
        client.stop();
        server.stop();
    }
}
Also used : SocketFactory(javax.net.SocketFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException) ConnectTimeoutException(org.apache.hadoop.net.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Example 98 with InetSocketAddress

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

the class TestIPC method testSocketFactoryException.

/**
   * Test that, if the socket factory throws an IOE, it properly propagates
   * to the client.
   */
@Test(timeout = 60000)
public void testSocketFactoryException() throws IOException {
    SocketFactory mockFactory = mock(SocketFactory.class);
    doThrow(new IOException("Injected fault")).when(mockFactory).createSocket();
    Client client = new Client(LongWritable.class, conf, mockFactory);
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 10);
    try {
        call(client, RANDOM.nextLong(), address, conf);
        fail("Expected an exception to have been thrown");
    } catch (IOException e) {
        assertTrue(e.getMessage().contains("Injected fault"));
    } finally {
        client.stop();
    }
}
Also used : SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Test(org.junit.Test)

Example 99 with InetSocketAddress

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

the class RPCCallBenchmark method createRpcClient.

/**
   * Create a client proxy for the specified engine.
   */
private RpcServiceWrapper createRpcClient(MyOptions opts) throws IOException {
    InetSocketAddress addr = NetUtils.createSocketAddr(opts.host, opts.getPort());
    if (opts.rpcEngine == ProtobufRpcEngine.class) {
        final TestRpcService proxy = RPC.getProxy(TestRpcService.class, 0, addr, conf);
        return new RpcServiceWrapper() {

            @Override
            public String doEcho(String msg) throws Exception {
                EchoRequestProto req = EchoRequestProto.newBuilder().setMessage(msg).build();
                EchoResponseProto responseProto = proxy.echo(null, req);
                return responseProto.getMessage();
            }
        };
    } else {
        throw new RuntimeException("unsupported engine: " + opts.rpcEngine);
    }
}
Also used : EchoRequestProto(org.apache.hadoop.ipc.protobuf.TestProtos.EchoRequestProto) InetSocketAddress(java.net.InetSocketAddress) EchoResponseProto(org.apache.hadoop.ipc.protobuf.TestProtos.EchoResponseProto)

Example 100 with InetSocketAddress

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

the class TestAsyncIPC method internalTestAsyncCallLimit.

public void internalTestAsyncCallLimit(int handlerCount, boolean handlerSleep, int clientCount, int callerCount, int callCount) throws IOException, InterruptedException, ExecutionException {
    Configuration conf = new Configuration();
    conf.setInt(CommonConfigurationKeys.IPC_CLIENT_ASYNC_CALLS_MAX_KEY, 100);
    Client.setPingInterval(conf, TestIPC.PING_INTERVAL);
    Server server = new TestIPC.TestServer(handlerCount, handlerSleep, conf);
    InetSocketAddress addr = NetUtils.getConnectAddress(server);
    server.start();
    Client[] clients = new Client[clientCount];
    for (int i = 0; i < clientCount; i++) {
        clients[i] = new Client(LongWritable.class, conf);
    }
    AsyncLimitlCaller[] callers = new AsyncLimitlCaller[callerCount];
    for (int i = 0; i < callerCount; i++) {
        callers[i] = new AsyncLimitlCaller(i, clients[i % clientCount], addr, callCount);
        callers[i].start();
    }
    for (int i = 0; i < callerCount; i++) {
        callers[i].join();
        callers[i].waitForReturnValues(callers[i].getStart(), callers[i].getCount());
        String msg = String.format("Expected not failed for caller-%d: %s.", i, callers[i]);
        assertFalse(msg, callers[i].failed);
    }
    for (int i = 0; i < clientCount; i++) {
        clients[i].stop();
    }
    server.stop();
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) TestServer(org.apache.hadoop.ipc.TestIPC.TestServer) InetSocketAddress(java.net.InetSocketAddress) LongWritable(org.apache.hadoop.io.LongWritable) TestServer(org.apache.hadoop.ipc.TestIPC.TestServer)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2586 Test (org.junit.Test)595 IOException (java.io.IOException)592 Socket (java.net.Socket)345 InetAddress (java.net.InetAddress)242 SocketAddress (java.net.SocketAddress)176 ServerSocket (java.net.ServerSocket)170 ArrayList (java.util.ArrayList)168 Configuration (org.apache.hadoop.conf.Configuration)140 ByteBuffer (java.nio.ByteBuffer)129 UnknownHostException (java.net.UnknownHostException)122 InputStream (java.io.InputStream)102 OutputStream (java.io.OutputStream)101 SocketChannel (java.nio.channels.SocketChannel)101 SocketException (java.net.SocketException)89 File (java.io.File)88 HashMap (java.util.HashMap)78 URI (java.net.URI)72 Proxy (java.net.Proxy)65 SocketTimeoutException (java.net.SocketTimeoutException)65