Search in sources :

Example 66 with Context

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

the class HttpTestBase method startServer.

protected void startServer(Context context, HttpServer server) throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    context.runOnContext(v -> {
        server.listen(onSuccess(s -> latch.countDown()));
    });
    awaitLatch(latch);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ProxyType(io.vertx.core.net.ProxyType) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) Context(io.vertx.core.Context) HttpClient(io.vertx.core.http.HttpClient) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 67 with Context

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

the class HttpTest method testSendNonExistingFile.

@Test
public void testSendNonExistingFile() throws Exception {
    server.requestHandler(req -> {
        final Context ctx = vertx.getOrCreateContext();
        req.response().sendFile("/not/existing/path", event -> {
            assertEquals(ctx, vertx.getOrCreateContext());
            if (event.failed()) {
                req.response().end("failed");
            }
        });
    });
    server.listen(onSuccess(s -> {
        client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
            resp.bodyHandler(buff -> {
                assertEquals("failed", buff.toString());
                testComplete();
            });
        }).end();
    }));
    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) Test(org.junit.Test)

Example 68 with Context

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

the class ContextTest method testRunOnContext.

@Test
public void testRunOnContext() throws Exception {
    vertx.runOnContext(v -> {
        Thread th = Thread.currentThread();
        Context ctx = Vertx.currentContext();
        ctx.runOnContext(v2 -> {
            assertEquals(th, Thread.currentThread());
            for (int i = 0; i < 10; i++) {
                Context c = Vertx.currentContext();
                assertEquals(ctx, c);
            }
            new Thread() {

                public void run() {
                    ctx.runOnContext(v3 -> {
                        assertEquals(th, Thread.currentThread());
                        assertEquals(ctx, Vertx.currentContext());
                        testComplete();
                    });
                }
            }.start();
        });
    });
    await();
}
Also used : Context(io.vertx.core.Context) CyclicBarrier(java.util.concurrent.CyclicBarrier) Vertx(io.vertx.core.Vertx) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Context(io.vertx.core.Context) Future(io.vertx.core.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) WorkerExecutor(io.vertx.core.WorkerExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) DeploymentOptions(io.vertx.core.DeploymentOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractVerticle(io.vertx.core.AbstractVerticle) TaskQueue(io.vertx.core.impl.TaskQueue) Test(org.junit.Test)

Example 69 with Context

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

the class ContextTest method testExecuteOrderedBlocking.

@Test
public void testExecuteOrderedBlocking() throws Exception {
    Context context = vertx.getOrCreateContext();
    context.executeBlocking(f -> {
        assertTrue(Context.isOnWorkerThread());
        f.complete(1 + 2);
    }, r -> {
        assertTrue(Context.isOnEventLoopThread());
        assertEquals(r.result(), 3);
        testComplete();
    });
    await();
}
Also used : Context(io.vertx.core.Context) Test(org.junit.Test)

Example 70 with Context

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

the class ContextTest method testExceptionHandlerOnAsyncDeploymentAsyncResultHandlerFailure.

@Test
public void testExceptionHandlerOnAsyncDeploymentAsyncResultHandlerFailure() {
    RuntimeException failure = new RuntimeException();
    Context ctx = vertx.getOrCreateContext();
    ctx.exceptionHandler(err -> {
        assertSame(failure, err);
        testComplete();
    });
    ctx.runOnContext(v -> {
        vertx.deployVerticle(new AbstractVerticle() {

            @Override
            public void start(Future<Void> startFuture) throws Exception {
                context.runOnContext(startFuture::complete);
            }
        }, ar -> {
            throw failure;
        });
    });
    await();
}
Also used : Context(io.vertx.core.Context) AbstractVerticle(io.vertx.core.AbstractVerticle) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Aggregations

Context (io.vertx.core.Context)125 Test (org.junit.Test)99 Handler (io.vertx.core.Handler)75 Vertx (io.vertx.core.Vertx)75 Before (org.junit.Before)58 Buffer (io.vertx.core.buffer.Buffer)57 TestContext (io.vertx.ext.unit.TestContext)57 Future (io.vertx.core.Future)53 Async (io.vertx.ext.unit.Async)53 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)53 RunWith (org.junit.runner.RunWith)53 JsonObject (io.vertx.core.json.JsonObject)49 HttpURLConnection (java.net.HttpURLConnection)48 StandardCharsets (java.nio.charset.StandardCharsets)48 Assert.assertThat (org.junit.Assert.assertThat)48 Mockito (org.mockito.Mockito)48 Rule (org.junit.Rule)39 CoreMatchers.is (org.hamcrest.CoreMatchers.is)38 ArgumentCaptor (org.mockito.ArgumentCaptor)38 Timeout (org.junit.rules.Timeout)36