use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class FriendlyServersTest method testHttpBin.
@Test
@Ignore
public void testHttpBin() 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 HttpResponseStatus status = client.go(new Request(HttpMethod.GET, new URL("https://httpbin.org/get")), StatusResponseHandler.getInstance()).get().getStatus();
Assert.assertEquals(200, status.getCode());
}
{
final HttpResponseStatus status = client.go(new Request(HttpMethod.POST, new URL("https://httpbin.org/post")).setContent(new byte[] { 'a', 'b', 'c', 1, 2, 3 }), StatusResponseHandler.getInstance()).get().getStatus();
Assert.assertEquals(200, status.getCode());
}
} finally {
lifecycle.stop();
}
}
use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class FriendlyServersTest method testCompressionCodecConfig.
@Test
public void testCompressionCodecConfig() throws Exception {
final ExecutorService exec = Executors.newSingleThreadExecutor();
final ServerSocket serverSocket = new ServerSocket(0);
final AtomicBoolean foundAcceptEncoding = new AtomicBoolean();
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()) {
// Read headers
String header;
while (!(header = in.readLine()).equals("")) {
if ("Accept-Encoding: identity".equals(header)) {
foundAcceptEncoding.set(true);
}
}
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().withCompressionCodec(HttpClientConfig.CompressionCodec.IDENTITY).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());
Assert.assertTrue(foundAcceptEncoding.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 testFriendlySelfSignedHttpsServer.
@Test
public void testFriendlySelfSignedHttpsServer() throws Exception {
final Lifecycle lifecycle = new Lifecycle();
final String keyStorePath = getClass().getClassLoader().getResource("keystore.jks").getFile();
Server server = new Server();
HttpConfiguration https = new HttpConfiguration();
https.addCustomizer(new SecureRequestCustomizer());
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(keyStorePath);
sslContextFactory.setKeyStorePassword("abc123");
sslContextFactory.setKeyManagerPassword("abc123");
ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(https));
sslConnector.setPort(0);
server.setConnectors(new Connector[] { sslConnector });
server.start();
try {
final SSLContext mySsl = HttpClientInit.sslContextWithTrustedKeyStore(keyStorePath, "abc123");
final HttpClientConfig trustingConfig = HttpClientConfig.builder().withSslContext(mySsl).build();
final HttpClient trustingClient = HttpClientInit.createClient(trustingConfig, lifecycle);
final HttpClientConfig skepticalConfig = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
final HttpClient skepticalClient = HttpClientInit.createClient(skepticalConfig, lifecycle);
// Correct name ("localhost")
{
final HttpResponseStatus status = trustingClient.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", sslConnector.getLocalPort()))), StatusResponseHandler.getInstance()).get().getStatus();
Assert.assertEquals(404, status.getCode());
}
// Incorrect name ("127.0.0.1")
{
final ListenableFuture<StatusResponseHolder> response1 = trustingClient.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://127.0.0.1:%d/", sslConnector.getLocalPort()))), StatusResponseHandler.getInstance());
Throwable ea = null;
try {
response1.get();
} catch (ExecutionException e) {
ea = e.getCause();
}
Assert.assertTrue("ChannelException thrown by 'get'", ea instanceof ChannelException);
Assert.assertTrue("Expected error message", ea.getCause().getMessage().contains("Failed to handshake"));
}
{
// Untrusting client
final ListenableFuture<StatusResponseHolder> response2 = skepticalClient.go(new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", sslConnector.getLocalPort()))), StatusResponseHandler.getInstance());
Throwable eb = null;
try {
response2.get();
} catch (ExecutionException e) {
eb = e.getCause();
}
Assert.assertNotNull("ChannelException thrown by 'get'", eb);
Assert.assertTrue("Root cause is SSLHandshakeException", eb.getCause().getCause() instanceof SSLHandshakeException);
}
} finally {
lifecycle.stop();
server.stop();
}
}
use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class EmitterTest method sizeBasedEmitterGeneralizedCreation.
private HttpPostEmitter sizeBasedEmitterGeneralizedCreation(int size) {
Properties props = new Properties();
props.setProperty("org.apache.druid.java.util.emitter.type", "http");
props.setProperty("org.apache.druid.java.util.emitter.recipientBaseUrl", TARGET_URL);
props.setProperty("org.apache.druid.java.util.emitter.flushMillis", String.valueOf(Long.MAX_VALUE));
props.setProperty("org.apache.druid.java.util.emitter.flushCount", String.valueOf(size));
props.setProperty("org.apache.druid.java.util.emitter.flushTimeOut", String.valueOf(BaseHttpEmittingConfig.TEST_FLUSH_TIMEOUT_MILLIS));
Lifecycle lifecycle = new Lifecycle();
Emitter emitter = Emitters.create(props, httpClient, JSON_MAPPER, lifecycle);
Assert.assertTrue(StringUtils.format("HttpPostEmitter emitter should be created, but found %s", emitter.getClass().getName()), emitter instanceof HttpPostEmitter);
emitter.start();
return (HttpPostEmitter) emitter;
}
use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.
the class JankyServersTest method testHttpSilentServerWithGlobalTimeout.
@Test
public void testHttpSilentServerWithGlobalTimeout() throws Throwable {
final Lifecycle lifecycle = new Lifecycle();
try {
final HttpClientConfig config = HttpClientConfig.builder().withReadTimeout(new Duration(100)).build();
final HttpClient client = HttpClientInit.createClient(config, lifecycle);
final ListenableFuture<StatusResponseHolder> future = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", silentServerSocket.getLocalPort()))), StatusResponseHandler.getInstance());
Throwable e = null;
try {
future.get();
} catch (ExecutionException e1) {
e = e1.getCause();
}
Assert.assertTrue("ReadTimeoutException thrown by 'get'", e instanceof ReadTimeoutException);
} finally {
lifecycle.stop();
}
}
Aggregations