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();
}
}
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();
}
}
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;
}
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();
}
}
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();
}
}
Aggregations