Search in sources :

Example 36 with Response

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

the class ConnectToHandlerTestCase method requireThatConnectToHandlerWorks.

@Test
public void requireThatConnectToHandlerWorks() {
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    MyRequestHandler requestHandler = new MyRequestHandler(new MyContent());
    ContainerBuilder builder = driver.newContainerBuilder();
    builder.serverBindings().bind("http://host/*", requestHandler);
    driver.activateContainer(builder);
    Request request = new Request(driver, URI.create("http://host/path"));
    MyResponseHandler responseHandler = new MyResponseHandler();
    ContentChannel content = request.connect(responseHandler);
    request.release();
    assertNotNull(content);
    content.close(null);
    assertNotNull(requestHandler.handler);
    assertSame(request, requestHandler.request);
    requestHandler.handler.handleResponse(new Response(Response.Status.OK)).close(null);
    driver.close();
}
Also used : Response(com.yahoo.jdisc.Response) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 37 with Response

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

the class ServerProviderConformanceTest method testRequestNondeterministicExceptionWithAsyncHandleResponse.

private <T extends ServerProvider, U, V> void testRequestNondeterministicExceptionWithAsyncHandleResponse(final Adapter<T, U, V> adapter, final Module... config) throws Throwable {
    runTest(adapter, Modules.combine(config), RequestType.WITHOUT_CONTENT, new TestRequestHandler() {

        @Override
        public ContentChannel handle(final Request request, final ResponseHandler handler) {
            callInOtherThread(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    try {
                        final ContentChannel out = handler.handleResponse(new Response(Response.Status.OK));
                        closeResponse(out);
                    } catch (Throwable ignored) {
                    }
                    return null;
                }
            });
            throw new ConformanceException();
        }
    });
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request) Callable(java.util.concurrent.Callable)

Example 38 with Response

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

the class ServerProviderConformanceTest method testRequestExceptionBeforeResponseWriteWithAsyncHandleResponse.

private <T extends ServerProvider, U, V> void testRequestExceptionBeforeResponseWriteWithAsyncHandleResponse(final Adapter<T, U, V> adapter, final Module... config) throws Throwable {
    runTest(adapter, Modules.combine(config), RequestType.WITHOUT_CONTENT, new TestRequestHandler() {

        @Override
        public ContentChannel handle(final Request request, final ResponseHandler handler) {
            final Event exceptionHandledByFramework = new Event();
            callInOtherThread(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    exceptionHandledByFramework.await();
                    try {
                        final ContentChannel out = handler.handleResponse(new Response(Response.Status.OK));
                        exceptionHandledByFramework.await();
                        writeResponse(out);
                        closeResponse(out);
                    } catch (Throwable ignored) {
                    }
                    return null;
                }
            });
            throw new ConformanceException(exceptionHandledByFramework);
        }
    });
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request) Callable(java.util.concurrent.Callable)

Example 39 with Response

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

the class ThreadedRequestHandlerTestCase method requireThatThreadedRequestHandlerRetainsTheRequestUntilHandlerIsRun.

@Test
public void requireThatThreadedRequestHandlerRetainsTheRequestUntilHandlerIsRun() throws Exception {
    final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    final AtomicInteger baseRetainCount = new AtomicInteger();
    builder.serverBindings().bind("http://localhost/base", new AbstractRequestHandler() {

        @Override
        public ContentChannel handleRequest(Request request, ResponseHandler handler) {
            baseRetainCount.set(request.retainCount());
            handler.handleResponse(new Response(Response.Status.OK)).close(null);
            return null;
        }
    });
    final CountDownLatch entryLatch = new CountDownLatch(1);
    final CountDownLatch exitLatch = new CountDownLatch(1);
    final AtomicInteger testRetainCount = new AtomicInteger();
    builder.serverBindings().bind("http://localhost/test", new ThreadedRequestHandler(newExecutor()) {

        @Override
        public void handleRequest(Request request, ReadableContentChannel requestContent, ResponseHandler responseHandler) {
            try {
                entryLatch.await(600, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                return;
            }
            testRetainCount.set(request.retainCount());
            responseHandler.handleResponse(new Response(Response.Status.OK)).close(null);
            // drain content to call completion handlers
            requestContent.read();
            exitLatch.countDown();
        }
    });
    driver.activateContainer(builder);
    dispatchRequest(driver, "http://localhost/base");
    dispatchRequest(driver, "http://localhost/test");
    entryLatch.countDown();
    exitLatch.await(600, TimeUnit.SECONDS);
    assertEquals(baseRetainCount.get(), testRetainCount.get());
    assertTrue(driver.close());
}
Also used : Request(com.yahoo.jdisc.Request) CountDownLatch(java.util.concurrent.CountDownLatch) TestDriver(com.yahoo.jdisc.test.TestDriver) Response(com.yahoo.jdisc.Response) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 40 with Response

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

the class MbusRequestHandlerTestCase method requireThatHandlerCanRespondInOtherThread.

@Test
public void requireThatHandlerCanRespondInOtherThread() throws Exception {
    TestDriver driver = newTestDriver(ThreadedReplier.INSTANCE);
    Response response = dispatchMessage(driver, new SimpleMessage("msg")).get(60, TimeUnit.SECONDS);
    assertTrue(response instanceof MbusResponse);
    assertEquals(Response.Status.OK, response.getStatus());
    Reply reply = ((MbusResponse) response).getReply();
    assertTrue(reply instanceof EmptyReply);
    assertFalse(reply.hasErrors());
    assertTrue(driver.close());
}
Also used : Response(com.yahoo.jdisc.Response) SimpleMessage(com.yahoo.messagebus.test.SimpleMessage) Reply(com.yahoo.messagebus.Reply) EmptyReply(com.yahoo.messagebus.EmptyReply) TestDriver(com.yahoo.jdisc.test.TestDriver) EmptyReply(com.yahoo.messagebus.EmptyReply) Test(org.junit.Test)

Aggregations

Response (com.yahoo.jdisc.Response)52 Test (org.junit.Test)39 Request (com.yahoo.jdisc.Request)17 SimpleMessage (com.yahoo.messagebus.test.SimpleMessage)14 ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)11 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)11 TestDriver (com.yahoo.jdisc.test.TestDriver)10 ClientTestDriver (com.yahoo.messagebus.jdisc.test.ClientTestDriver)7 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)6 SimpleReply (com.yahoo.messagebus.test.SimpleReply)6 ByteBuffer (java.nio.ByteBuffer)6 ServerTestDriver (com.yahoo.messagebus.jdisc.test.ServerTestDriver)5 Chain (com.yahoo.component.chain.Chain)3 HttpRequest (com.yahoo.jdisc.http.HttpRequest)3 HttpResponse (com.yahoo.jdisc.http.HttpResponse)3 NonWorkingRequest (com.yahoo.jdisc.test.NonWorkingRequest)3 Reply (com.yahoo.messagebus.Reply)3 Processor (com.yahoo.processing.Processor)3 Callable (java.util.concurrent.Callable)3 Test (org.testng.annotations.Test)3