Search in sources :

Example 46 with ContainerBuilder

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

the class RequestDispatchTestCase method requireThatRequestCanBeDispatched.

@Test
public void requireThatRequestCanBeDispatched() throws Exception {
    final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    final List<ByteBuffer> writtenContent = Arrays.asList(ByteBuffer.allocate(6), ByteBuffer.allocate(9));
    ReadableContentChannel receivedContent = new ReadableContentChannel();
    ContainerBuilder builder = driver.newContainerBuilder();
    Response response = new Response(Response.Status.OK);
    builder.serverBindings().bind("http://localhost/", new MyRequestHandler(receivedContent, response));
    driver.activateContainer(builder);
    RequestDispatch dispatch = new RequestDispatch() {

        @Override
        protected Request newRequest() {
            return new Request(driver, URI.create("http://localhost/"));
        }

        @Override
        protected Iterable<ByteBuffer> requestContent() {
            return writtenContent;
        }
    };
    dispatch.dispatch();
    assertFalse(dispatch.isDone());
    assertSame(writtenContent.get(0), receivedContent.read());
    assertFalse(dispatch.isDone());
    assertSame(writtenContent.get(1), receivedContent.read());
    assertFalse(dispatch.isDone());
    assertNull(receivedContent.read());
    assertTrue(dispatch.isDone());
    assertSame(response, dispatch.get(600, TimeUnit.SECONDS));
    assertTrue(driver.close());
}
Also used : Response(com.yahoo.jdisc.Response) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) Request(com.yahoo.jdisc.Request) ByteBuffer(java.nio.ByteBuffer) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 47 with ContainerBuilder

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

the class RequestDispatchTestCase method requireThatDispatchHandlesCloseException.

@Test
public void requireThatDispatchHandlesCloseException() {
    final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    Response response = new Response(Response.Status.OK);
    builder.serverBindings().bind("http://localhost/", new MyRequestHandler(new ContentChannel() {

        @Override
        public void write(ByteBuffer buf, CompletionHandler handler) {
            handler.completed();
        }

        @Override
        public void close(CompletionHandler handler) {
            throw new RuntimeException();
        }
    }, response));
    driver.activateContainer(builder);
    try {
        new RequestDispatch() {

            @Override
            protected Request newRequest() {
                return new Request(driver, URI.create("http://localhost/"));
            }

            @Override
            protected Iterable<ByteBuffer> requestContent() {
                return Arrays.asList(ByteBuffer.allocate(69));
            }
        }.dispatch();
        fail();
    } catch (RuntimeException e) {
    }
    assertTrue(driver.close());
}
Also used : Response(com.yahoo.jdisc.Response) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) Request(com.yahoo.jdisc.Request) ByteBuffer(java.nio.ByteBuffer) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 48 with ContainerBuilder

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

the class RequestDispatchTestCase method requireThatDispatchCanBeListenedTo.

@Test
public void requireThatDispatchCanBeListenedTo() throws InterruptedException {
    final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    ReadableContentChannel requestContent = new ReadableContentChannel();
    MyRequestHandler requestHandler = new MyRequestHandler(requestContent, null);
    builder.serverBindings().bind("http://localhost/", requestHandler);
    driver.activateContainer(builder);
    RunnableLatch listener = new RunnableLatch();
    new RequestDispatch() {

        @Override
        protected Request newRequest() {
            return new Request(driver, URI.create("http://localhost/"));
        }
    }.dispatch().addListener(listener, MoreExecutors.directExecutor());
    assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
    ContentChannel responseContent = ResponseDispatch.newInstance(Response.Status.OK).connect(requestHandler.responseHandler);
    assertFalse(listener.await(100, TimeUnit.MILLISECONDS));
    assertNull(requestContent.read());
    assertTrue(listener.await(600, TimeUnit.SECONDS));
    responseContent.close(null);
    assertTrue(driver.close());
}
Also used : ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) Request(com.yahoo.jdisc.Request) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 49 with ContainerBuilder

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

the class RequestDispatchTestCase method requireThatDispatchHandlesWriteException.

@Test
public void requireThatDispatchHandlesWriteException() {
    final TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    Response response = new Response(Response.Status.OK);
    builder.serverBindings().bind("http://localhost/", new MyRequestHandler(new ContentChannel() {

        @Override
        public void write(ByteBuffer buf, CompletionHandler handler) {
            throw new RuntimeException();
        }

        @Override
        public void close(CompletionHandler handler) {
            handler.completed();
        }
    }, response));
    driver.activateContainer(builder);
    try {
        new RequestDispatch() {

            @Override
            protected Request newRequest() {
                return new Request(driver, URI.create("http://localhost/"));
            }

            @Override
            protected Iterable<ByteBuffer> requestContent() {
                return Arrays.asList(ByteBuffer.allocate(69));
            }
        }.dispatch();
        fail();
    } catch (RuntimeException e) {
    }
    assertTrue(driver.close());
}
Also used : Response(com.yahoo.jdisc.Response) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) Request(com.yahoo.jdisc.Request) ByteBuffer(java.nio.ByteBuffer) TestDriver(com.yahoo.jdisc.test.TestDriver) Test(org.junit.Test)

Example 50 with ContainerBuilder

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

the class ApplicationLoaderTestCase method requireThatThreadFactoryCanBeBound.

@Test
public void requireThatThreadFactoryCanBeBound() {
    final ThreadFactory factory = Executors.defaultThreadFactory();
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(new AbstractModule() {

        @Override
        protected void configure() {
            bind(ThreadFactory.class).toInstance(factory);
        }
    });
    ContainerBuilder builder = driver.newContainerBuilder();
    assertSame(factory, builder.getInstance(ThreadFactory.class));
    assertTrue(driver.close());
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) TestDriver(com.yahoo.jdisc.test.TestDriver) AbstractModule(com.google.inject.AbstractModule) 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