Search in sources :

Example 1 with TestDriver

use of com.yahoo.jdisc.test.TestDriver 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 TestDriver

use of com.yahoo.jdisc.test.TestDriver 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 TestDriver

use of com.yahoo.jdisc.test.TestDriver 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 TestDriver

use of com.yahoo.jdisc.test.TestDriver 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 TestDriver

use of com.yahoo.jdisc.test.TestDriver 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

TestDriver (com.yahoo.jdisc.test.TestDriver)134 Test (org.junit.Test)128 ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)39 Request (com.yahoo.jdisc.Request)22 Bundle (org.osgi.framework.Bundle)14 AbstractModule (com.google.inject.AbstractModule)13 ByteBuffer (java.nio.ByteBuffer)12 Response (com.yahoo.jdisc.Response)10 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)10 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)6 Executor (java.util.concurrent.Executor)6 LinkedList (java.util.LinkedList)5 BundleContext (org.osgi.framework.BundleContext)4 BindingSet (com.yahoo.jdisc.application.BindingSet)3 ServerProvider (com.yahoo.jdisc.service.ServerProvider)3 NonWorkingRequestHandler (com.yahoo.jdisc.test.NonWorkingRequestHandler)3 SimpleMessage (com.yahoo.messagebus.test.SimpleMessage)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 BundleException (org.osgi.framework.BundleException)3 Module (com.google.inject.Module)2