Search in sources :

Example 81 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class HttpServerHandlerBenchmark method setup.

@Setup
public void setup() {
    vertx = (VertxInternal) Vertx.vertx();
    HttpServerOptions options = new HttpServerOptions();
    vertxChannel = new EmbeddedChannel(new VertxHttpRequestDecoder(options), // We don't use the VertxHttpResponseDecoder because it will use the PartialPooledByteBufAllocator
    new HttpResponseEncoder() {

        @Override
        protected void encodeHeaders(HttpHeaders headers, ByteBuf buf) {
            ((HeadersMultiMap) headers).encode(buf);
        }
    });
    vertxChannel.config().setAllocator(new Alloc());
    ContextInternal context = vertx.createEventLoopContext(vertxChannel.eventLoop(), null, Thread.currentThread().getContextClassLoader());
    Handler<HttpServerRequest> app = request -> {
        HttpServerResponse response = request.response();
        MultiMap headers = response.headers();
        headers.add(HEADER_CONTENT_TYPE, RESPONSE_TYPE_PLAIN).add(HEADER_SERVER, SERVER).add(HEADER_DATE, DATE_STRING).add(HEADER_CONTENT_LENGTH, HELLO_WORLD_LENGTH);
        response.end(HELLO_WORLD_BUFFER);
    };
    VertxHandler<Http1xServerConnection> handler = VertxHandler.create(chctx -> {
        Http1xServerConnection conn = new Http1xServerConnection(() -> context, null, new HttpServerOptions(), chctx, context, "localhost", null);
        conn.handler(app);
        return conn;
    });
    vertxChannel.pipeline().addLast("handler", handler);
    nettyChannel = new EmbeddedChannel(new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(), options.getMaxChunkSize(), false, options.getDecoderInitialBufferSize()), new HttpResponseEncoder(), new SimpleChannelInboundHandler<HttpRequest>() {

        private final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(CharsetUtil.UTF_8);

        private final int STATIC_PLAINTEXT_LEN = STATIC_PLAINTEXT.length;

        private final ByteBuf PLAINTEXT_CONTENT_BUFFER = Unpooled.unreleasableBuffer(Unpooled.directBuffer().writeBytes(STATIC_PLAINTEXT));

        private final CharSequence PLAINTEXT_CLHEADER_VALUE = new AsciiString(String.valueOf(STATIC_PLAINTEXT_LEN));

        private final CharSequence TYPE_PLAIN = new AsciiString("text/plain");

        private final CharSequence SERVER_NAME = new AsciiString("Netty");

        private final CharSequence CONTENT_TYPE_ENTITY = HttpHeaderNames.CONTENT_TYPE;

        private final CharSequence DATE_ENTITY = HttpHeaderNames.DATE;

        private final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaderNames.CONTENT_LENGTH;

        private final CharSequence SERVER_ENTITY = HttpHeaderNames.SERVER;

        private final DateFormat FORMAT = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");

        private final CharSequence date = new AsciiString(FORMAT.format(new Date()));

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
            writeResponse(ctx, msg, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN, PLAINTEXT_CLHEADER_VALUE);
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
        }

        private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf, CharSequence contentType, CharSequence contentLength) {
            // Build the response object.
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf, false);
            HttpHeaders headers = response.headers();
            headers.set(CONTENT_TYPE_ENTITY, contentType);
            headers.set(SERVER_ENTITY, SERVER_NAME);
            headers.set(DATE_ENTITY, date);
            headers.set(CONTENT_LENGTH_ENTITY, contentLength);
            // Close the non-keep-alive connection after the write operation is done.
            ctx.write(response, ctx.voidPromise());
        }
    });
    nettyChannel.config().setAllocator(new Alloc());
    GET = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(("GET / HTTP/1.1\r\n" + "\r\n").getBytes()));
    readerIndex = GET.readerIndex();
    writeIndex = GET.writerIndex();
}
Also used : HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Date(java.util.Date) MultiMap(io.vertx.core.MultiMap) AsciiString(io.netty.util.AsciiString) ContextInternal(io.vertx.core.impl.ContextInternal) SimpleDateFormat(java.text.SimpleDateFormat) Scope(org.openjdk.jmh.annotations.Scope) Unpooled(io.netty.buffer.Unpooled) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) CharsetUtil(io.netty.util.CharsetUtil) DateFormat(java.text.DateFormat) VertxHandler(io.vertx.core.net.impl.VertxHandler) Setup(org.openjdk.jmh.annotations.Setup) HttpRequest(io.netty.handler.codec.http.HttpRequest) VertxInternal(io.vertx.core.impl.VertxInternal) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Vertx(io.vertx.core.Vertx) VertxHttpRequestDecoder(io.vertx.core.http.impl.VertxHttpRequestDecoder) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) State(org.openjdk.jmh.annotations.State) Benchmark(org.openjdk.jmh.annotations.Benchmark) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Buffer(io.vertx.core.buffer.Buffer) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) HttpServerResponse(io.vertx.core.http.HttpServerResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Fork(org.openjdk.jmh.annotations.Fork) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handler(io.vertx.core.Handler) CompilerControl(org.openjdk.jmh.annotations.CompilerControl) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpServerOptions(io.vertx.core.http.HttpServerOptions) ContextInternal(io.vertx.core.impl.ContextInternal) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) MultiMap(io.vertx.core.MultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) HeadersMultiMap(io.vertx.core.http.impl.headers.HeadersMultiMap) VertxHttpRequestDecoder(io.vertx.core.http.impl.VertxHttpRequestDecoder) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpServerResponse(io.vertx.core.http.HttpServerResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) VertxHttpRequestDecoder(io.vertx.core.http.impl.VertxHttpRequestDecoder) HttpServerRequest(io.vertx.core.http.HttpServerRequest) AsciiString(io.netty.util.AsciiString) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Date(java.util.Date) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Setup(org.openjdk.jmh.annotations.Setup)

Example 82 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class DeploymentTest method testDeployUndeployMultipleInstancesUsingClassName.

@Test
public void testDeployUndeployMultipleInstancesUsingClassName() throws Exception {
    int numInstances = 10;
    DeploymentOptions options = new DeploymentOptions().setInstances(numInstances);
    AtomicInteger deployCount = new AtomicInteger();
    AtomicInteger undeployCount = new AtomicInteger();
    AtomicInteger deployHandlerCount = new AtomicInteger();
    AtomicInteger undeployHandlerCount = new AtomicInteger();
    vertx.eventBus().<String>consumer("tvstarted").handler(msg -> {
        deployCount.incrementAndGet();
    });
    vertx.eventBus().<String>consumer("tvstopped").handler(msg -> {
        undeployCount.incrementAndGet();
        msg.reply("whatever");
    });
    CountDownLatch deployLatch = new CountDownLatch(1);
    vertx.deployVerticle(TestVerticle2.class.getCanonicalName(), options, onSuccess(depID -> {
        assertEquals(1, deployHandlerCount.incrementAndGet());
        deployLatch.countDown();
    }));
    awaitLatch(deployLatch);
    assertWaitUntil(() -> deployCount.get() == numInstances);
    assertEquals(1, vertx.deploymentIDs().size());
    Deployment deployment = ((VertxInternal) vertx).getDeployment(vertx.deploymentIDs().iterator().next());
    Set<Verticle> verticles = deployment.getVerticles();
    assertEquals(numInstances, verticles.size());
    CountDownLatch undeployLatch = new CountDownLatch(1);
    assertEquals(numInstances, deployCount.get());
    vertx.undeploy(deployment.deploymentID(), onSuccess(v -> {
        assertEquals(1, undeployHandlerCount.incrementAndGet());
        undeployLatch.countDown();
    }));
    awaitLatch(undeployLatch);
    assertWaitUntil(() -> deployCount.get() == numInstances);
    assertTrue(vertx.deploymentIDs().isEmpty());
}
Also used : java.util(java.util) URL(java.net.URL) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) URLClassLoader(java.net.URLClassLoader) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(io.vertx.core.json.JsonObject) SourceVerticle(io.vertx.test.verticles.sourceverticle.SourceVerticle) VertxInternal(io.vertx.core.impl.VertxInternal) Files(java.nio.file.Files) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Message(io.vertx.core.eventbus.Message) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Deployment(io.vertx.core.impl.Deployment) CompilingClassLoader(io.vertx.core.impl.verticle.CompilingClassLoader) io.vertx.test.verticles(io.vertx.test.verticles) SourceVerticle(io.vertx.test.verticles.sourceverticle.SourceVerticle) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Deployment(io.vertx.core.impl.Deployment) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 83 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class DeploymentTest method testGetInstanceCountMultipleVerticles.

@Test
public void testGetInstanceCountMultipleVerticles() throws Exception {
    AtomicInteger messageCount = new AtomicInteger(0);
    AtomicInteger totalReportedInstances = new AtomicInteger(0);
    vertx.eventBus().consumer("instanceCount", event -> {
        messageCount.incrementAndGet();
        totalReportedInstances.addAndGet((int) event.body());
        if (messageCount.intValue() == 3) {
            assertEquals(9, totalReportedInstances.get());
            testComplete();
        }
    });
    vertx.deployVerticle(TestVerticle3.class.getCanonicalName(), new DeploymentOptions().setInstances(3), ar -> {
        assertTrue(ar.succeeded());
    });
    await();
    Deployment deployment = ((VertxInternal) vertx).getDeployment(vertx.deploymentIDs().iterator().next());
    CountDownLatch latch = new CountDownLatch(1);
    vertx.undeploy(deployment.deploymentID(), ar -> latch.countDown());
    awaitLatch(latch);
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Deployment(io.vertx.core.impl.Deployment) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 84 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class VertxTest method testCloseHookFailure1.

@Test
public void testCloseHookFailure1() {
    AtomicInteger closedCount = new AtomicInteger();
    class Hook implements Closeable {

        @Override
        public void close(Promise<Void> completion) {
            if (closedCount.incrementAndGet() == 1) {
                throw new RuntimeException();
            } else {
                completion.handle(Future.succeededFuture());
            }
        }
    }
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    vertx.addCloseHook(new Hook());
    vertx.addCloseHook(new Hook());
    // Now undeploy
    vertx.close(ar -> {
        assertTrue(ar.succeeded());
        assertEquals(2, closedCount.get());
        testComplete();
    });
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 85 with VertxInternal

use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.

the class VertxTest method testCloseHooksCalled.

@Test
public void testCloseHooksCalled() {
    AtomicInteger closedCount = new AtomicInteger();
    Closeable myCloseable1 = completionHandler -> {
        closedCount.incrementAndGet();
        completionHandler.handle(Future.succeededFuture());
    };
    Closeable myCloseable2 = completionHandler -> {
        closedCount.incrementAndGet();
        completionHandler.handle(Future.succeededFuture());
    };
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    vertx.addCloseHook(myCloseable1);
    vertx.addCloseHook(myCloseable2);
    // Now undeploy
    vertx.close(ar -> {
        assertTrue(ar.succeeded());
        assertEquals(2, closedCount.get());
        testComplete();
    });
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) URL(java.net.URL) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) RepeatRule(io.vertx.test.core.RepeatRule) OptionsBuilder(org.openjdk.jmh.runner.options.OptionsBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) URLClassLoader(java.net.URLClassLoader) Rule(org.junit.Rule) AsyncTestBase(io.vertx.test.core.AsyncTestBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) CloseFuture(io.vertx.core.impl.CloseFuture) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) Runner(org.openjdk.jmh.runner.Runner) WeakReference(java.lang.ref.WeakReference) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

VertxInternal (io.vertx.core.impl.VertxInternal)102 Test (org.junit.Test)73 CountDownLatch (java.util.concurrent.CountDownLatch)46 VertxOptions (io.vertx.core.VertxOptions)30 Buffer (io.vertx.core.buffer.Buffer)29 JsonObject (io.vertx.core.json.JsonObject)29 File (java.io.File)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)27 VertxException (io.vertx.core.VertxException)24 HttpClient (io.vertx.core.http.HttpClient)24 NetClient (io.vertx.core.net.NetClient)24 TimeUnit (java.util.concurrent.TimeUnit)24 NetServerOptions (io.vertx.core.net.NetServerOptions)23 InetAddress (java.net.InetAddress)23 Channel (io.netty.channel.Channel)22 InetSocketAddress (java.net.InetSocketAddress)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 NetServer (io.vertx.core.net.NetServer)21 ChannelFuture (io.netty.channel.ChannelFuture)20 Bootstrap (io.netty.bootstrap.Bootstrap)19