io.druid.server.initialization.JettyTest.java Source code

Java tutorial

Introduction

Here is the source code for io.druid.server.initialization.JettyTest.java

Source

/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  Metamarkets licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.druid.server.initialization;

import com.google.common.base.Throwables;
import com.google.common.util.concurrent.ListenableFuture;
import com.metamx.http.client.Request;
import com.metamx.http.client.response.InputStreamResponseHandler;
import com.metamx.http.client.response.StatusResponseHandler;
import com.metamx.http.client.response.StatusResponseHolder;
import org.apache.commons.io.IOUtils;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import javax.servlet.http.HttpServletResponse;

public class JettyTest extends BaseJettyTest {

    @Test
    @Ignore // this test will deadlock if it hits an issue, so ignored by default
    public void testTimeouts() throws Exception {
        // test for request timeouts properly not locking up all threads
        final Executor executor = Executors.newFixedThreadPool(100);
        final AtomicLong count = new AtomicLong(0);
        final CountDownLatch latch = new CountDownLatch(1000);
        for (int i = 0; i < 10000; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    executor.execute(new Runnable() {
                        @Override
                        public void run() {
                            long startTime = System.currentTimeMillis();
                            long startTime2 = 0;
                            try {
                                ListenableFuture<StatusResponseHolder> go = client.go(
                                        new Request(HttpMethod.GET,
                                                new URL("http://localhost:" + port + "/slow/hello")),
                                        new StatusResponseHandler(Charset.defaultCharset()));
                                startTime2 = System.currentTimeMillis();
                                go.get();
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                System.out.println("Response time client" + (System.currentTimeMillis() - startTime)
                                        + "time taken for getting future"
                                        + (System.currentTimeMillis() - startTime2) + "Counter "
                                        + count.incrementAndGet());
                                latch.countDown();

                            }
                        }
                    });
                }
            });
        }

        latch.await();
    }

    @Test
    public void testGzipCompression() throws Exception {
        final URL url = new URL("http://localhost:" + port + "/default");
        final HttpURLConnection get = (HttpURLConnection) url.openConnection();
        get.setRequestProperty("Accept-Encoding", "gzip");
        Assert.assertEquals("gzip", get.getContentEncoding());

        final HttpURLConnection post = (HttpURLConnection) url.openConnection();
        post.setRequestProperty("Accept-Encoding", "gzip");
        post.setRequestMethod("POST");
        Assert.assertEquals("gzip", post.getContentEncoding());

        final HttpURLConnection getNoGzip = (HttpURLConnection) url.openConnection();
        Assert.assertNotEquals("gzip", getNoGzip.getContentEncoding());

        final HttpURLConnection postNoGzip = (HttpURLConnection) url.openConnection();
        postNoGzip.setRequestMethod("POST");
        Assert.assertNotEquals("gzip", postNoGzip.getContentEncoding());
    }

    // Tests that threads are not stuck when partial chunk is not finalized
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=424107
    @Test
    @Ignore
    // above bug is not fixed in jetty for gzip encoding, and the chunk is still finalized instead of throwing exception.
    public void testChunkNotFinalized() throws Exception {
        ListenableFuture<InputStream> go = client.go(
                new Request(HttpMethod.GET, new URL("http://localhost:" + port + "/exception/exception")),
                new InputStreamResponseHandler());
        try {
            StringWriter writer = new StringWriter();
            IOUtils.copy(go.get(), writer, "utf-8");
            Assert.fail("Should have thrown Exception");
        } catch (IOException e) {
            // Expected.
        }

    }

    @Test
    public void testThreadNotStuckOnException() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    ListenableFuture<InputStream> go = client.go(
                            new Request(HttpMethod.GET,
                                    new URL("http://localhost:" + port + "/exception/exception")),
                            new InputStreamResponseHandler());
                    StringWriter writer = new StringWriter();
                    IOUtils.copy(go.get(), writer, "utf-8");
                } catch (IOException e) {
                    // Expected.
                } catch (Throwable t) {
                    Throwables.propagate(t);
                }
                latch.countDown();
            }
        });

        latch.await(5, TimeUnit.SECONDS);
    }

    @Test
    public void testExtensionAuthFilter() throws Exception {
        URL url = new URL("http://localhost:" + port + "/default");
        HttpURLConnection get = (HttpURLConnection) url.openConnection();
        get.setRequestProperty(DummyAuthFilter.AUTH_HDR, DummyAuthFilter.SECRET_USER);
        Assert.assertEquals(HttpServletResponse.SC_OK, get.getResponseCode());

        get = (HttpURLConnection) url.openConnection();
        get.setRequestProperty(DummyAuthFilter.AUTH_HDR, "hacker");
        Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, get.getResponseCode());
    }
}