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