Search in sources :

Example 1 with Request

use of com.yahoo.jdisc.Request 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 2 with Request

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

the class MbusServerTestCase method requireThatRequestIsMbus.

@Test
public void requireThatRequestIsMbus() {
    MyRequestHandler requestHandler = MyRequestHandler.newInstance();
    ServerTestDriver driver = ServerTestDriver.newInstance(requestHandler);
    assertTrue(driver.sendMessage(new SimpleMessage("foo")));
    Request request = requestHandler.awaitRequest();
    assertTrue(request instanceof MbusRequest);
    Message msg = ((MbusRequest) request).getMessage();
    assertTrue(msg instanceof SimpleMessage);
    assertEquals("foo", ((SimpleMessage) msg).getValue());
    assertTrue(requestHandler.sendResponse(new Response(Response.Status.OK)));
    assertNotNull(driver.awaitSuccess());
    assertTrue(driver.close());
}
Also used : Response(com.yahoo.jdisc.Response) SimpleMessage(com.yahoo.messagebus.test.SimpleMessage) SimpleMessage(com.yahoo.messagebus.test.SimpleMessage) Request(com.yahoo.jdisc.Request) ServerTestDriver(com.yahoo.messagebus.jdisc.test.ServerTestDriver) Test(org.junit.Test)

Example 3 with Request

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

the class TimeoutManagerImplTestCase method requireThatNoEntryIsMadeIfTimeoutHandlerHasBeenSet.

@Test
public void requireThatNoEntryIsMadeIfTimeoutHandlerHasBeenSet() {
    final Context ctx = new Context(MyRequestHandler.newInstance());
    new RequestDispatch() {

        @Override
        protected Request newRequest() {
            Request request = new Request(ctx.driver, URI.create(REQUEST_URI));
            request.setTimeout(10, TimeUnit.MILLISECONDS);
            request.setTimeoutManager(new TimeoutManager() {

                @Override
                public void scheduleTimeout(Request request) {
                }
            });
            return request;
        }
    }.dispatch();
    assertFalse(ctx.awaitQueueSize(1, 100, TimeUnit.MILLISECONDS));
    assertTrue(ctx.awaitQueueSize(0, 600, TimeUnit.SECONDS));
    ctx.requestHandler.respond();
    assertTrue(ctx.close());
}
Also used : RequestDispatch(com.yahoo.jdisc.handler.RequestDispatch) TimeoutManager(com.yahoo.jdisc.TimeoutManager) NonWorkingRequest(com.yahoo.jdisc.test.NonWorkingRequest) Request(com.yahoo.jdisc.Request) Test(org.junit.Test)

Example 4 with Request

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

the class TimeoutManagerImplTestCase method requireThatTimeoutOccursAtExpectedTime.

@Test
public void requireThatTimeoutOccursAtExpectedTime() throws InterruptedException {
    final Context ctx = new Context(MyRequestHandler.newInstance());
    final MyResponseHandler responseHandler = MyResponseHandler.newInstance();
    ctx.forwardToTime(100);
    new RequestDispatch() {

        @Override
        protected Request newRequest() {
            Request request = new Request(ctx.driver, URI.create(REQUEST_URI));
            request.setTimeout(300, TimeUnit.MILLISECONDS);
            return request;
        }

        @Override
        public ContentChannel handleResponse(Response response) {
            return responseHandler.handleResponse(response);
        }
    }.dispatch();
    ctx.forwardToTime(300);
    assertFalse(responseHandler.await(100, TimeUnit.MILLISECONDS));
    ctx.forwardToTime(400);
    assertTrue(responseHandler.await(600, TimeUnit.SECONDS));
    Response response = responseHandler.response.get();
    assertNotNull(response);
    assertEquals(Response.Status.GATEWAY_TIMEOUT, response.getStatus());
    assertTrue(ctx.close());
}
Also used : Response(com.yahoo.jdisc.Response) RequestDispatch(com.yahoo.jdisc.handler.RequestDispatch) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) NonWorkingRequest(com.yahoo.jdisc.test.NonWorkingRequest) Request(com.yahoo.jdisc.Request) Test(org.junit.Test)

Example 5 with Request

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

the class TimeoutManagerImplTestCase method requireThatManagedHandlerForwardsAllCalls.

@Test
public void requireThatManagedHandlerForwardsAllCalls() throws InterruptedException {
    Request request = NonWorkingRequest.newInstance(REQUEST_URI);
    MyRequestHandler requestHandler = MyRequestHandler.newInstance();
    TimeoutManagerImpl timeoutManager = new TimeoutManagerImpl(Executors.defaultThreadFactory(), new SystemTimer());
    RequestHandler managedHandler = timeoutManager.manageHandler(requestHandler);
    MyResponseHandler responseHandler = MyResponseHandler.newInstance();
    ContentChannel requestContent = managedHandler.handleRequest(request, responseHandler);
    assertNotNull(requestContent);
    ByteBuffer buf = ByteBuffer.allocate(69);
    requestContent.write(buf, null);
    assertSame(buf, requestHandler.content.buf);
    MyCompletion writeCompletion = new MyCompletion();
    requestContent.write(buf = ByteBuffer.allocate(69), writeCompletion);
    assertSame(buf, requestHandler.content.buf);
    requestHandler.content.writeCompletion.completed();
    assertTrue(writeCompletion.completed.await(600, TimeUnit.SECONDS));
    MyCompletion closeCompletion = new MyCompletion();
    requestContent.close(closeCompletion);
    requestHandler.content.closeCompletion.completed();
    assertTrue(closeCompletion.completed.await(600, TimeUnit.SECONDS));
    managedHandler.release();
    assertTrue(requestHandler.destroyed);
    Response response = new Response(Response.Status.OK);
    ContentChannel responseContent = requestHandler.responseHandler.handleResponse(response);
    assertNotNull(responseContent);
    responseContent.write(buf = ByteBuffer.allocate(69), null);
    assertSame(buf, responseHandler.content.buf);
    responseContent.write(buf = ByteBuffer.allocate(69), writeCompletion = new MyCompletion());
    assertSame(buf, responseHandler.content.buf);
    responseHandler.content.writeCompletion.completed();
    assertTrue(writeCompletion.completed.await(600, TimeUnit.SECONDS));
    responseContent.close(closeCompletion = new MyCompletion());
    responseHandler.content.closeCompletion.completed();
    assertTrue(closeCompletion.completed.await(600, TimeUnit.SECONDS));
    assertSame(response, responseHandler.response.get());
}
Also used : Response(com.yahoo.jdisc.Response) RequestHandler(com.yahoo.jdisc.handler.RequestHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) NonWorkingRequest(com.yahoo.jdisc.test.NonWorkingRequest) Request(com.yahoo.jdisc.Request) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

Request (com.yahoo.jdisc.Request)44 Test (org.junit.Test)31 TestDriver (com.yahoo.jdisc.test.TestDriver)22 Response (com.yahoo.jdisc.Response)17 ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)16 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)10 ByteBuffer (java.nio.ByteBuffer)7 NonWorkingRequest (com.yahoo.jdisc.test.NonWorkingRequest)4 RequestDispatch (com.yahoo.jdisc.handler.RequestDispatch)3 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)3 ClientTestDriver (com.yahoo.messagebus.jdisc.test.ClientTestDriver)3 SimpleMessage (com.yahoo.messagebus.test.SimpleMessage)3 Callable (java.util.concurrent.Callable)3 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)2 HttpRequest (com.yahoo.jdisc.http.HttpRequest)2 URI (java.net.URI)2 Executor (java.util.concurrent.Executor)2 Test (org.testng.annotations.Test)2 AbstractModule (com.google.inject.AbstractModule)1 ResourceReference (com.yahoo.jdisc.ResourceReference)1