Search in sources :

Example 1 with WorkerContext

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

the class HttpTest method testInVerticle.

private void testInVerticle(boolean worker) throws Exception {
    client.close();
    server.close();
    class MyVerticle extends AbstractVerticle {

        Context ctx;

        @Override
        public void start() {
            ctx = Vertx.currentContext();
            if (worker) {
                assertTrue(ctx instanceof WorkerContext);
            } else {
                assertTrue(ctx instanceof EventLoopContext);
            }
            Thread thr = Thread.currentThread();
            server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
            server.requestHandler(req -> {
                req.response().end();
                assertSame(ctx, Vertx.currentContext());
                if (!worker) {
                    assertSame(thr, Thread.currentThread());
                }
            });
            server.listen(ar -> {
                assertTrue(ar.succeeded());
                assertSame(ctx, Vertx.currentContext());
                if (!worker) {
                    assertSame(thr, Thread.currentThread());
                }
                client = vertx.createHttpClient(new HttpClientOptions());
                client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> {
                    assertSame(ctx, Vertx.currentContext());
                    if (!worker) {
                        assertSame(thr, Thread.currentThread());
                    }
                    assertEquals(200, res.statusCode());
                    testComplete();
                }).end();
            });
        }
    }
    MyVerticle verticle = new MyVerticle();
    vertx.deployVerticle(verticle, new DeploymentOptions().setWorker(worker));
    await();
}
Also used : Context(io.vertx.core.Context) WorkerContext(io.vertx.core.impl.WorkerContext) EventLoopContext(io.vertx.core.impl.EventLoopContext) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) TimeoutException(java.util.concurrent.TimeoutException) Context(io.vertx.core.Context) InetAddress(java.net.InetAddress) HttpFrame(io.vertx.core.http.HttpFrame) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestLoggerFactory(io.vertx.test.netty.TestLoggerFactory) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) Nullable(io.vertx.codegen.annotations.Nullable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) WorkerContext(io.vertx.core.impl.WorkerContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) IntStream(java.util.stream.IntStream) HeadersAdaptor(io.vertx.core.http.impl.HeadersAdaptor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OutputStreamWriter(java.io.OutputStreamWriter) Assume(org.junit.Assume) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) BufferedWriter(java.io.BufferedWriter) Vertx(io.vertx.core.Vertx) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EventLoopContext(io.vertx.core.impl.EventLoopContext) URLEncoder(java.net.URLEncoder) Rule(org.junit.Rule) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) Handler(io.vertx.core.Handler) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) TemporaryFolder(org.junit.rules.TemporaryFolder) TestUtils.assertIllegalArgumentException(io.vertx.test.core.TestUtils.assertIllegalArgumentException) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) WorkerContext(io.vertx.core.impl.WorkerContext) EventLoopContext(io.vertx.core.impl.EventLoopContext) AbstractVerticle(io.vertx.core.AbstractVerticle) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 2 with WorkerContext

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

the class ConnectionPoolTest method testDefaultContextProviderReusesSameEventLoop.

@Test
public void testDefaultContextProviderReusesSameEventLoop() {
    WorkerContext context = vertx.createWorkerContext();
    ConnectionManager mgr = new ConnectionManager();
    ConnectionPool<Connection> pool = ConnectionPool.pool(mgr, new int[] { 10 }, 10);
    pool.acquire(context.duplicate(), 0, onSuccess(lease -> {
    }));
    assertEquals(1, pool.requests());
    ConnectionRequest request = mgr.assertRequest();
    assertSame(context.nettyEventLoop(), request.context.nettyEventLoop());
}
Also used : Arrays(java.util.Arrays) VertxInternal(io.vertx.core.impl.VertxInternal) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ConnectionPoolTooBusyException(io.vertx.core.http.ConnectionPoolTooBusyException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.vertx.core.Future) VertxTestBase(io.vertx.test.core.VertxTestBase) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) EventLoopContext(io.vertx.core.impl.EventLoopContext) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Ignore(org.junit.Ignore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WorkerContext(io.vertx.core.impl.WorkerContext) AsyncResult(io.vertx.core.AsyncResult) Queue(java.util.Queue) Handler(io.vertx.core.Handler) WorkerContext(io.vertx.core.impl.WorkerContext) Test(org.junit.Test)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)2 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Vertx (io.vertx.core.Vertx)2 EventLoopContext (io.vertx.core.impl.EventLoopContext)2 WorkerContext (io.vertx.core.impl.WorkerContext)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 InternalLoggerFactory (io.netty.util.internal.logging.InternalLoggerFactory)1 Nullable (io.vertx.codegen.annotations.Nullable)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Context (io.vertx.core.Context)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 MultiMap (io.vertx.core.MultiMap)1 Promise (io.vertx.core.Promise)1 VertxException (io.vertx.core.VertxException)1 Buffer (io.vertx.core.buffer.Buffer)1 ConnectionPoolTooBusyException (io.vertx.core.http.ConnectionPoolTooBusyException)1 HttpClient (io.vertx.core.http.HttpClient)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1