Search in sources :

Example 11 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class FriendlyServersTest method testFriendlyProxyHttpServer.

@Test
public void testFriendlyProxyHttpServer() throws Exception {
    final AtomicReference<String> requestContent = new AtomicReference<>();
    final ExecutorService exec = Executors.newSingleThreadExecutor();
    final ServerSocket serverSocket = new ServerSocket(0);
    exec.submit(new Runnable() {

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try (Socket clientSocket = serverSocket.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
                    OutputStream out = clientSocket.getOutputStream()) {
                    StringBuilder request = new StringBuilder();
                    String line;
                    while (!"".equals((line = in.readLine()))) {
                        request.append(line).append("\r\n");
                    }
                    requestContent.set(request.toString());
                    out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes(StandardCharsets.UTF_8));
                    while (!in.readLine().equals("")) {
                    // skip lines
                    }
                    out.write("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nhello!".getBytes(StandardCharsets.UTF_8));
                } catch (Exception e) {
                    Assert.fail(e.toString());
                }
            }
        }
    });
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withHttpProxyConfig(new HttpClientProxyConfig("localhost", serverSocket.getLocalPort(), "bob", "sally")).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final StatusResponseHolder response = client.go(new Request(HttpMethod.GET, new URL("http://anotherHost:8080/")), StatusResponseHandler.getInstance()).get();
        Assert.assertEquals(200, response.getStatus().getCode());
        Assert.assertEquals("hello!", response.getContent());
        Assert.assertEquals("CONNECT anotherHost:8080 HTTP/1.1\r\nProxy-Authorization: Basic Ym9iOnNhbGx5\r\n", requestContent.get());
    } finally {
        exec.shutdownNow();
        serverSocket.close();
        lifecycle.stop();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerSocket(java.net.ServerSocket) ChannelException(org.jboss.netty.channel.ChannelException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) ExecutorService(java.util.concurrent.ExecutorService) BufferedReader(java.io.BufferedReader) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 12 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class FriendlyServersTest method testFriendlyHttpServer.

@Test
public void testFriendlyHttpServer() throws Exception {
    final ExecutorService exec = Executors.newSingleThreadExecutor();
    final ServerSocket serverSocket = new ServerSocket(0);
    exec.submit(new Runnable() {

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try (Socket clientSocket = serverSocket.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
                    OutputStream out = clientSocket.getOutputStream()) {
                    while (!in.readLine().equals("")) {
                    // skip lines
                    }
                    out.write("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nhello!".getBytes(StandardCharsets.UTF_8));
                } catch (Exception e) {
                // Suppress
                }
            }
        }
    });
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final StatusResponseHolder response = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", serverSocket.getLocalPort()))), StatusResponseHandler.getInstance()).get();
        Assert.assertEquals(200, response.getStatus().getCode());
        Assert.assertEquals("hello!", response.getContent());
    } finally {
        exec.shutdownNow();
        serverSocket.close();
        lifecycle.stop();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) ServerSocket(java.net.ServerSocket) ChannelException(org.jboss.netty.channel.ChannelException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) ExecutorService(java.util.concurrent.ExecutorService) BufferedReader(java.io.BufferedReader) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 13 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class ParametrizedUriEmitterTest method parametrizedEmmiter.

private Emitter parametrizedEmmiter(String uriPattern) throws Exception {
    final Properties props = new Properties();
    props.setProperty("org.apache.druid.java.util.emitter.type", "parametrized");
    props.setProperty("org.apache.druid.java.util.emitter.recipientBaseUrlPattern", uriPattern);
    props.setProperty("org.apache.druid.java.util.emitter.httpEmitting.flushTimeOut", String.valueOf(BaseHttpEmittingConfig.TEST_FLUSH_TIMEOUT_MILLIS));
    lifecycle = new Lifecycle();
    Emitter emitter = Emitters.create(props, httpClient, lifecycle);
    Assert.assertEquals(ParametrizedUriEmitter.class, emitter.getClass());
    lifecycle.start();
    return emitter;
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) Properties(java.util.Properties)

Example 14 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JankyServersTest method testHttpsEchoServer.

@Test
public void testHttpsEchoServer() throws Throwable {
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final ListenableFuture<StatusResponseHolder> response = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", echoServerSocket.getLocalPort()))), StatusResponseHandler.getInstance());
        expectedException.expect(ExecutionException.class);
        expectedException.expectMessage("org.jboss.netty.channel.ChannelException: Faulty channel in resource pool");
        response.get();
    } finally {
        lifecycle.stop();
    }
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) URL(java.net.URL) Test(org.junit.Test)

Example 15 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JankyServersTest method testHttpsConnectionClosingServer.

@Test
public void testHttpsConnectionClosingServer() throws Throwable {
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final ListenableFuture<StatusResponseHolder> response = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", closingServerSocket.getLocalPort()))), StatusResponseHandler.getInstance());
        Throwable e = null;
        try {
            response.get();
        } catch (ExecutionException e1) {
            e = e1.getCause();
            e1.printStackTrace();
        }
        Assert.assertTrue("ChannelException thrown by 'get'", isChannelClosedException(e));
    } finally {
        lifecycle.stop();
    }
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Lifecycle (org.apache.druid.java.util.common.lifecycle.Lifecycle)46 Test (org.junit.Test)21 Injector (com.google.inject.Injector)12 URL (java.net.URL)12 ExecutionException (java.util.concurrent.ExecutionException)12 StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)10 Before (org.junit.Before)10 ExecutorService (java.util.concurrent.ExecutorService)6 Binder (com.google.inject.Binder)5 Module (com.google.inject.Module)5 OutputStream (java.io.OutputStream)5 Properties (java.util.Properties)5 ManageLifecycle (org.apache.druid.guice.ManageLifecycle)5 NoopServiceEmitter (org.apache.druid.server.metrics.NoopServiceEmitter)5 ChannelException (org.jboss.netty.channel.ChannelException)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 IOException (java.io.IOException)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Provides (com.google.inject.Provides)3