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