use of io.vertx.core.Handler in project vert.x by eclipse.
the class Http2ServerTest method testClientSendGoAwayNoError.
@Test
public void testClientSendGoAwayNoError() throws Exception {
Future<Void> abc = Future.future();
Context ctx = vertx.getOrCreateContext();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
AtomicInteger numShutdown = new AtomicInteger();
AtomicBoolean completed = new AtomicBoolean();
conn.shutdownHandler(v -> {
assertOnIOContext(ctx);
numShutdown.getAndIncrement();
vertx.setTimer(100, timerID -> {
completed.set(true);
testComplete();
});
});
conn.goAwayHandler(ga -> {
assertOnIOContext(ctx);
assertEquals(0, numShutdown.get());
req.response().end();
});
conn.closeHandler(v -> {
assertTrue(completed.get());
});
abc.complete();
};
server.requestHandler(requestHandler);
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
Http2ConnectionEncoder encoder = request.encoder;
int id = request.nextStreamId();
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
abc.setHandler(ar -> {
encoder.writeGoAway(request.context, id, 0, Unpooled.EMPTY_BUFFER, request.context.newPromise());
request.context.flush();
});
});
fut.sync();
await();
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class FutureTest method testDefaultCompleter.
@Test
public void testDefaultCompleter() {
AsyncResult<Object> succeededAsyncResult = new AsyncResult<Object>() {
Object result = new Object();
public Object result() {
return result;
}
public Throwable cause() {
throw new UnsupportedOperationException();
}
public boolean succeeded() {
return true;
}
public boolean failed() {
throw new UnsupportedOperationException();
}
public <U> AsyncResult<U> map(Function<Object, U> mapper) {
throw new UnsupportedOperationException();
}
public <V> AsyncResult<V> map(V value) {
throw new UnsupportedOperationException();
}
};
AsyncResult<Object> failedAsyncResult = new AsyncResult<Object>() {
Throwable cause = new Throwable();
public Object result() {
throw new UnsupportedOperationException();
}
public Throwable cause() {
return cause;
}
public boolean succeeded() {
return false;
}
public boolean failed() {
throw new UnsupportedOperationException();
}
public <U> AsyncResult<U> map(Function<Object, U> mapper) {
throw new UnsupportedOperationException();
}
public <V> AsyncResult<V> map(V value) {
throw new UnsupportedOperationException();
}
};
class DefaultCompleterTestFuture<T> implements Future<T> {
boolean succeeded;
boolean failed;
T result;
Throwable cause;
public boolean isComplete() {
throw new UnsupportedOperationException();
}
public Future<T> setHandler(Handler<AsyncResult<T>> handler) {
throw new UnsupportedOperationException();
}
public void complete(T result) {
if (!tryComplete(result)) {
throw new IllegalStateException();
}
}
public void complete() {
if (!tryComplete()) {
throw new IllegalStateException();
}
}
public void fail(Throwable cause) {
if (!tryFail(cause)) {
throw new IllegalStateException();
}
}
public void fail(String failureMessage) {
if (!tryFail(failureMessage)) {
throw new IllegalStateException();
}
}
public boolean tryComplete(T result) {
if (succeeded || failed) {
return false;
}
succeeded = true;
this.result = result;
return true;
}
public boolean tryComplete() {
throw new UnsupportedOperationException();
}
public boolean tryFail(Throwable cause) {
if (succeeded || failed) {
return false;
}
failed = true;
this.cause = cause;
return true;
}
public boolean tryFail(String failureMessage) {
throw new UnsupportedOperationException();
}
public T result() {
throw new UnsupportedOperationException();
}
public Throwable cause() {
throw new UnsupportedOperationException();
}
public boolean succeeded() {
throw new UnsupportedOperationException();
}
public boolean failed() {
throw new UnsupportedOperationException();
}
public void handle(AsyncResult<T> asyncResult) {
if (asyncResult.succeeded()) {
complete(asyncResult.result());
} else {
fail(asyncResult.cause());
}
}
}
DefaultCompleterTestFuture<Object> successFuture = new DefaultCompleterTestFuture<>();
successFuture.completer().handle(succeededAsyncResult);
assertTrue(successFuture.succeeded);
assertEquals(succeededAsyncResult.result(), successFuture.result);
DefaultCompleterTestFuture<Object> failureFuture = new DefaultCompleterTestFuture<>();
failureFuture.completer().handle(failedAsyncResult);
assertTrue(failureFuture.failed);
assertEquals(failedAsyncResult.cause(), failureFuture.cause);
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class EventBusInterceptorTest method testExceptionInInterceptor.
@Test
public void testExceptionInInterceptor() {
AtomicInteger cnt = new AtomicInteger();
Handler<SendContext> eb1 = sc -> {
cnt.incrementAndGet();
vertx.runOnContext(v -> sc.next());
throw new RuntimeException("foo");
};
Handler<SendContext> eb2 = sc -> {
cnt.incrementAndGet();
sc.next();
};
eb.addInterceptor(eb1).addInterceptor(eb2);
eb.consumer("some-address", msg -> {
assertEquals("armadillo", msg.body());
assertEquals(2, cnt.get());
testComplete();
});
eb.send("some-address", "armadillo");
await();
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class VertxImpl method close.
@Override
public synchronized void close(Handler<AsyncResult<Void>> completionHandler) {
if (closed || eventBus == null) {
// Just call the handler directly since pools shutdown
if (completionHandler != null) {
completionHandler.handle(Future.succeededFuture());
}
return;
}
closed = true;
closeHooks.run(ar -> {
deploymentManager.undeployAll(ar1 -> {
if (haManager() != null) {
haManager().stop();
}
addressResolver.close(ar2 -> {
eventBus.close(ar3 -> {
closeClusterManager(ar4 -> {
Set<HttpServer> httpServers = new HashSet<>(sharedHttpServers.values());
Set<NetServerBase> netServers = new HashSet<>(sharedNetServers.values());
sharedHttpServers.clear();
sharedNetServers.clear();
int serverCount = httpServers.size() + netServers.size();
AtomicInteger serverCloseCount = new AtomicInteger();
Handler<AsyncResult<Void>> serverCloseHandler = res -> {
if (res.failed()) {
log.error("Failure in shutting down server", res.cause());
}
if (serverCloseCount.incrementAndGet() == serverCount) {
deleteCacheDirAndShutdown(completionHandler);
}
};
for (HttpServer server : httpServers) {
server.close(serverCloseHandler);
}
for (NetServerBase server : netServers) {
server.close(serverCloseHandler);
}
if (serverCount == 0) {
deleteCacheDirAndShutdown(completionHandler);
}
});
});
});
});
});
}
use of io.vertx.core.Handler in project vert.x by eclipse.
the class NetServerBase method listen.
public synchronized void listen(Handler<? super C> handler, int port, String host, Handler<AsyncResult<Void>> listenHandler) {
if (handler == null) {
throw new IllegalStateException("Set connect handler first");
}
if (listening) {
throw new IllegalStateException("Listen already called");
}
listening = true;
listenContext = vertx.getOrCreateContext();
registeredHandler = handler;
synchronized (vertx.sharedNetServers()) {
// Will be updated on bind for a wildcard port
this.actualPort = port;
id = new ServerID(port, host);
NetServerBase shared = vertx.sharedNetServers().get(id);
if (shared == null || port == 0) {
// Wildcard port will imply a new actual server each time
serverChannelGroup = new DefaultChannelGroup("vertx-acceptor-channels", GlobalEventExecutor.INSTANCE);
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(availableWorkers);
bootstrap.channel(NioServerSocketChannel.class);
sslHelper.validate(vertx);
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
if (isPaused()) {
ch.close();
return;
}
ChannelPipeline pipeline = ch.pipeline();
NetServerBase.this.initChannel(ch.pipeline());
pipeline.addLast("handler", new ServerHandler(ch));
}
});
applyConnectionOptions(bootstrap);
handlerManager.addHandler(handler, listenContext);
try {
bindFuture = AsyncResolveConnectHelper.doBind(vertx, port, host, bootstrap);
bindFuture.addListener(res -> {
if (res.succeeded()) {
Channel ch = res.result();
log.trace("Net server listening on " + host + ":" + ch.localAddress());
NetServerBase.this.actualPort = ((InetSocketAddress) ch.localAddress()).getPort();
NetServerBase.this.id = new ServerID(NetServerBase.this.actualPort, id.host);
serverChannelGroup.add(ch);
vertx.sharedNetServers().put(id, NetServerBase.this);
metrics = vertx.metricsSPI().createMetrics(new SocketAddressImpl(id.port, id.host), options);
} else {
vertx.sharedNetServers().remove(id);
}
});
} catch (Throwable t) {
// Make sure we send the exception back through the handler (if any)
if (listenHandler != null) {
vertx.runOnContext(v -> listenHandler.handle(Future.failedFuture(t)));
} else {
// No handler - log so user can see failure
log.error(t);
}
listening = false;
return;
}
if (port != 0) {
vertx.sharedNetServers().put(id, this);
}
actualServer = this;
} else {
// Server already exists with that host/port - we will use that
actualServer = shared;
this.actualPort = shared.actualPort();
metrics = vertx.metricsSPI().createMetrics(new SocketAddressImpl(id.port, id.host), options);
actualServer.handlerManager.addHandler(handler, listenContext);
}
// just add it to the future so it gets notified once the bind is complete
actualServer.bindFuture.addListener(res -> {
if (listenHandler != null) {
AsyncResult<Void> ares;
if (res.succeeded()) {
ares = Future.succeededFuture();
} else {
listening = false;
ares = Future.failedFuture(res.cause());
}
listenContext.runOnContext(v -> listenHandler.handle(ares));
} else if (res.failed()) {
log.error("Failed to listen", res.cause());
listening = false;
}
});
}
return;
}
Aggregations