Search in sources :

Example 86 with Context

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

the class DnsResolverProvider method close.

@Override
public void close(Handler<Void> doneHandler) {
    Context context = vertx.getOrCreateContext();
    ResolverRegistration[] registrations = this.resolvers.toArray(new ResolverRegistration[this.resolvers.size()]);
    if (registrations.length == 0) {
        context.runOnContext(doneHandler);
        return;
    }
    AtomicInteger count = new AtomicInteger(registrations.length);
    for (ResolverRegistration registration : registrations) {
        Runnable task = () -> {
            registration.resolver.close();
            if (count.decrementAndGet() == 0) {
                context.runOnContext(doneHandler);
            }
        };
        if (registration.executor.inEventLoop()) {
            task.run();
        } else {
            registration.executor.execute(task);
        }
    }
}
Also used : Context(io.vertx.core.Context) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 87 with Context

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

the class AsynchronousCounter method getAndAdd.

@Override
public void getAndAdd(long value, Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    Context context = vertx.getOrCreateContext();
    context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.getAndAdd(value))));
}
Also used : Context(io.vertx.core.Context)

Example 88 with Context

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

the class AsynchronousCounter method compareAndSet.

@Override
public void compareAndSet(long expected, long value, Handler<AsyncResult<Boolean>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    Context context = vertx.getOrCreateContext();
    context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.compareAndSet(expected, value))));
}
Also used : Context(io.vertx.core.Context)

Example 89 with Context

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

the class AsynchronousCounter method get.

@Override
public void get(Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    Context context = vertx.getOrCreateContext();
    context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.get())));
}
Also used : Context(io.vertx.core.Context)

Example 90 with Context

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

the class AsynchronousCounter method addAndGet.

@Override
public void addAndGet(long value, Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    Context context = vertx.getOrCreateContext();
    context.runOnContext(v -> resultHandler.handle(Future.succeededFuture(counter.addAndGet(value))));
}
Also used : Context(io.vertx.core.Context)

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