Search in sources :

Example 1 with ContainerBuilder

use of com.yahoo.jdisc.application.ContainerBuilder in project vespa by vespa-engine.

the class ThreadedRequestHandlerTestCase method requireThatResponseIsDispatchedIfHandlerIgnoresIt.

@Test
public void requireThatResponseIsDispatchedIfHandlerIgnoresIt() throws InterruptedException {
    Executor executor = Executors.newSingleThreadExecutor();
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    MyRequestHandler requestHandler = MyRequestHandler.newIgnoreResponse(executor);
    builder.serverBindings().bind("http://localhost/", requestHandler);
    driver.activateContainer(builder);
    MyResponseHandler responseHandler = new MyResponseHandler();
    driver.dispatchRequest("http://localhost/", responseHandler);
    requestHandler.entryLatch.countDown();
    assertTrue(requestHandler.exitLatch.await(60, TimeUnit.SECONDS));
    assertNull(requestHandler.content.read());
    assertTrue(responseHandler.latch.await(60, TimeUnit.SECONDS));
    assertEquals(Response.Status.INTERNAL_SERVER_ERROR, responseHandler.response.getStatus());
    assertNull(responseHandler.content.read());
    assertTrue(driver.close());
}
Also used : Executor(java.util.concurrent.Executor) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 2 with ContainerBuilder

use of com.yahoo.jdisc.application.ContainerBuilder in project vespa by vespa-engine.

the class ThreadedRequestHandlerTestCase method requireThatHandlerSetsRequestTimeout.

@Test
public void requireThatHandlerSetsRequestTimeout() throws InterruptedException {
    Executor executor = Executors.newSingleThreadExecutor();
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    MyRequestHandler requestHandler = MyRequestHandler.newInstance(executor);
    builder.serverBindings().bind("http://localhost/", requestHandler);
    driver.activateContainer(builder);
    MyResponseHandler responseHandler = new MyResponseHandler();
    driver.dispatchRequest("http://localhost/", responseHandler);
    requestHandler.entryLatch.countDown();
    assertTrue(requestHandler.exitLatch.await(60, TimeUnit.SECONDS));
    assertNull(requestHandler.content.read());
    assertNotNull(requestHandler.request.getTimeout(TimeUnit.MILLISECONDS));
    assertTrue(responseHandler.latch.await(60, TimeUnit.SECONDS));
    assertNull(responseHandler.content.read());
    assertTrue(driver.close());
}
Also used : Executor(java.util.concurrent.Executor) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 3 with ContainerBuilder

use of com.yahoo.jdisc.application.ContainerBuilder in project vespa by vespa-engine.

the class ThreadedRequestHandlerTestCase method requireThatRejectedExecutionIsHandledGracefully.

@Test
public void requireThatRejectedExecutionIsHandledGracefully() throws Exception {
    // Instrumentation.
    final Executor executor = new Executor() {

        @Override
        public void execute(final Runnable command) {
            throw new RejectedExecutionException("Deliberately thrown; simulating overloaded executor");
        }
    };
    final RequestHandler requestHandler = new ThreadedRequestHandler(executor) {

        @Override
        protected void handleRequest(Request request, BufferedContentChannel requestContent, ResponseHandler responseHandler) {
            throw new AssertionError("Should never get here");
        }
    };
    // Setup.
    final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    final ContainerBuilder builder = driver.newContainerBuilder();
    builder.serverBindings().bind("http://localhost/", requestHandler);
    driver.activateContainer(builder);
    final MyResponseHandler responseHandler = new MyResponseHandler();
    // Execution.
    try {
        driver.dispatchRequest("http://localhost/", responseHandler);
        fail("Above statement should throw exception");
    } catch (OverloadException e) {
    // As expected.
    }
    // Verification.
    assertEquals("Response handler should be invoked synchronously in this case.", 0, responseHandler.latch.getCount());
    assertEquals(Response.Status.SERVICE_UNAVAILABLE, responseHandler.response.getStatus());
    assertNull(responseHandler.content.read());
    assertTrue(driver.close());
}
Also used : Request(com.yahoo.jdisc.Request) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TestDriver(com.yahoo.jdisc.test.TestDriver) Executor(java.util.concurrent.Executor) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) Test(org.junit.Test)

Example 4 with ContainerBuilder

use of com.yahoo.jdisc.application.ContainerBuilder in project vespa by vespa-engine.

the class ThreadedRequestHandlerTestCase method assertThatRequestContentIsClosedAndResponseIsDispatchedIfHandlerIgnoresIt.

private static void assertThatRequestContentIsClosedAndResponseIsDispatchedIfHandlerIgnoresIt(MyRequestHandler requestHandler) throws InterruptedException {
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    builder.serverBindings().bind("http://localhost/", requestHandler);
    driver.activateContainer(builder);
    MyResponseHandler responseHandler = new MyResponseHandler();
    ContentChannel content = driver.connectRequest("http://localhost/", responseHandler);
    MyCompletion writeCompletion = new MyCompletion();
    content.write(ByteBuffer.allocate(69), writeCompletion);
    MyCompletion closeCompletion = new MyCompletion();
    content.close(closeCompletion);
    requestHandler.entryLatch.countDown();
    assertTrue(requestHandler.exitLatch.await(60, TimeUnit.SECONDS));
    assertTrue(writeCompletion.latch.await(60, TimeUnit.SECONDS));
    assertTrue(writeCompletion.completed);
    assertTrue(closeCompletion.latch.await(60, TimeUnit.SECONDS));
    assertTrue(writeCompletion.completed);
    assertTrue(responseHandler.latch.await(60, TimeUnit.SECONDS));
    assertEquals(Response.Status.INTERNAL_SERVER_ERROR, responseHandler.response.getStatus());
    assertNull(responseHandler.content.read());
    assertTrue(driver.close());
}
Also used : ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) TestDriver(com.yahoo.jdisc.test.TestDriver)

Example 5 with ContainerBuilder

use of com.yahoo.jdisc.application.ContainerBuilder in project vespa by vespa-engine.

the class ThreadedRequestHandlerTestCase method requireThatOverriddenRequestTimeoutIsUsed.

@Test
public void requireThatOverriddenRequestTimeoutIsUsed() throws InterruptedException {
    Executor executor = Executors.newSingleThreadExecutor();
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    MyRequestHandler requestHandler = MyRequestHandler.newWithTimeout(executor, Duration.ofSeconds(1));
    builder.serverBindings().bind("http://localhost/", requestHandler);
    driver.activateContainer(builder);
    MyResponseHandler responseHandler = new MyResponseHandler();
    driver.dispatchRequest("http://localhost/", responseHandler);
    requestHandler.entryLatch.countDown();
    assertTrue(requestHandler.exitLatch.await(60, TimeUnit.SECONDS));
    assertEquals(1, (long) requestHandler.request.getTimeout(TimeUnit.SECONDS));
    assertTrue(responseHandler.latch.await(60, TimeUnit.SECONDS));
    assertNull(responseHandler.content.read());
    assertTrue(driver.close());
}
Also used : Executor(java.util.concurrent.Executor) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Aggregations

ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)53 Test (org.junit.Test)41 TestDriver (com.yahoo.jdisc.test.TestDriver)40 Request (com.yahoo.jdisc.Request)16 Response (com.yahoo.jdisc.Response)12 ByteBuffer (java.nio.ByteBuffer)7 Executor (java.util.concurrent.Executor)6 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)4 AbstractModule (com.google.inject.AbstractModule)3 BindingSet (com.yahoo.jdisc.application.BindingSet)3 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)3 NonWorkingRequestHandler (com.yahoo.jdisc.test.NonWorkingRequestHandler)3 Container (com.yahoo.jdisc.Container)2 UriPattern (com.yahoo.jdisc.application.UriPattern)2 Map (java.util.Map)2 ThreadFactory (java.util.concurrent.ThreadFactory)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Module (com.google.inject.Module)1 Pair (com.yahoo.collections.Pair)1