Search in sources :

Example 31 with Context

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

the class Http2Test method testServerStreamPausedWhenConnectionIsPaused.

@Test
public void testServerStreamPausedWhenConnectionIsPaused() throws Exception {
    CountDownLatch fullLatch = new CountDownLatch(1);
    CompletableFuture<Void> resumeLatch = new CompletableFuture<>();
    server.requestHandler(req -> {
        HttpServerResponse resp = req.response();
        switch(req.path()) {
            case "/0":
                {
                    vertx.setPeriodic(1, timerID -> {
                        if (resp.writeQueueFull()) {
                            vertx.cancelTimer(timerID);
                            fullLatch.countDown();
                        } else {
                            resp.write(Buffer.buffer(TestUtils.randomAlphaString(512)));
                        }
                    });
                    break;
                }
            case "/1":
                {
                    assertTrue(resp.writeQueueFull());
                    resp.drainHandler(v -> {
                        resp.end();
                    });
                    resumeLatch.complete(null);
                    break;
                }
        }
    });
    startServer();
    client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/0", resp -> {
        resp.pause();
        Context ctx = vertx.getOrCreateContext();
        resumeLatch.thenAccept(v1 -> {
            ctx.runOnContext(v2 -> {
                resp.endHandler(v -> {
                    testComplete();
                });
                resp.resume();
            });
        });
    });
    awaitLatch(fullLatch);
    client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/1", resp -> {
        resp.endHandler(v -> {
            complete();
        });
    });
    // Make sure it completes
    resumeLatch.get(20, TimeUnit.SECONDS);
    await();
}
Also used : Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Http2Settings(io.vertx.core.http.Http2Settings) Cert(io.vertx.test.core.tls.Cert) Context(io.vertx.core.Context) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) Buffer(io.vertx.core.buffer.Buffer) NetServer(io.vertx.core.net.NetServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpServerResponse(io.vertx.core.http.HttpServerResponse) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) StreamResetException(io.vertx.core.http.StreamResetException) Context(io.vertx.core.Context) CompletableFuture(java.util.concurrent.CompletableFuture) HttpServerResponse(io.vertx.core.http.HttpServerResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 32 with Context

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

the class Http2TestBase method assertOnIOContext.

protected void assertOnIOContext(Context context) {
    Context current = Vertx.currentContext();
    assertNotNull(current);
    assertEquals(context, current);
    for (StackTraceElement elt : Thread.currentThread().getStackTrace()) {
        if (elt.getMethodName().equals("executeFromIO")) {
            return;
        }
    }
    fail("Not from IO");
}
Also used : Context(io.vertx.core.Context)

Example 33 with Context

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

the class WebsocketTest method testRaceConditionWithWebsocketClientWorker.

@Test
public void testRaceConditionWithWebsocketClientWorker() throws Exception {
    CompletableFuture<Context> fut = new CompletableFuture<>();
    vertx.deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            fut.complete(context);
        }
    }, new DeploymentOptions().setWorker(true), ar -> {
        if (ar.failed()) {
            fut.completeExceptionally(ar.cause());
        }
    });
    testRaceConditionWithWebsocketClient(fut.get());
}
Also used : Context(io.vertx.core.Context) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 34 with Context

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

the class VertxTestBase method createWorker.

/**
   * Create a worker verticle for the current Vert.x and return its context.
   *
   * @return the context
   * @throws Exception anything preventing the creation of the worker
   */
protected Context createWorker() throws Exception {
    CompletableFuture<Context> fut = new CompletableFuture<>();
    vertx.deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            fut.complete(context);
        }
    }, new DeploymentOptions().setWorker(true), ar -> {
        if (ar.failed()) {
            fut.completeExceptionally(ar.cause());
        }
    });
    return fut.get();
}
Also used : Context(io.vertx.core.Context) CompletableFuture(java.util.concurrent.CompletableFuture) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle)

Example 35 with Context

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

the class FakeClusterManager method getCounter.

@Override
public void getCounter(String name, Handler<AsyncResult<Counter>> resultHandler) {
    AtomicLong counter = new AtomicLong();
    AtomicLong prev = counters.putIfAbsent(name, counter);
    if (prev != null) {
        counter = prev;
    }
    AtomicLong theCounter = counter;
    Context context = vertx.getOrCreateContext();
    context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(new AsynchronousCounter(vertx, theCounter))));
}
Also used : Context(io.vertx.core.Context) AtomicLong(java.util.concurrent.atomic.AtomicLong) AsynchronousCounter(io.vertx.core.shareddata.impl.AsynchronousCounter)

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