Skip to content

Commit

Permalink
Upgrade to Vert.x 4.5.1
Browse files Browse the repository at this point in the history
- Update POM
- Simplify current context retrieval
- Update test to avoid aggressive cleaner bugs
  • Loading branch information
tsegismont committed Dec 14, 2023
1 parent 5e8bc85 commit 3f7a965
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright 2022 Red Hat, Inc.
~ Copyright 2023 Red Hat, Inc.
~
~ Red Hat licenses this file to you under the Apache License, version 2.0
~ (the "License"); you may not use this file except in compliance with the
Expand Down Expand Up @@ -30,7 +30,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vertx.version>4.5.0</vertx.version>
<vertx.version>4.5.1</vertx.version>
<logback.version>1.3.0</logback.version>
<log4j2.version>2.22.0</log4j2.version>
<slf4j.version>2.0.0</slf4j.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Red Hat, Inc.
* Copyright 2023 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
Expand All @@ -15,12 +15,14 @@
*/
package io.reactiverse.contextual.logging;

import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.BiFunction;
import java.util.logging.Formatter;
import java.util.logging.LogManager;
Expand Down Expand Up @@ -167,8 +169,8 @@ private String parseStringValue(String template) {
}

// if the format fails the template is invalid. This is also the technique used in the JDK however
// the JDK will silently fallback to the default format in this case it makes sense to abort as the
// behavior would not match what the users desires.
// the JDK will silently fall back to the default format in this case it makes sense to abort as the
// behavior would not match what the users desire.
String.format(format, args);
} catch (RuntimeException e) {
throw new IllegalArgumentException("format string \"" + template + "\" is not valid.");
Expand Down Expand Up @@ -211,7 +213,7 @@ private static boolean substringMatch(CharSequence str, int index, CharSequence
@Override
public String format(LogRecord record) {
final Object[] args = new Object[resolvers.size()];
final ContextInternal context = (ContextInternal) Vertx.currentContext();
final ContextInternal context = ContextInternal.current();
// process the placeholder values
for (int i = 0; i < args.length; i++) {
args[i] = resolvers.get(i).apply(record, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Red Hat, Inc.
* Copyright 2023 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
Expand All @@ -16,7 +16,6 @@

package io.reactiverse.contextual.logging;

import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
Expand Down Expand Up @@ -47,7 +46,7 @@ private Log4j2Converter(String[] options) {

@Override
public void format(LogEvent event, StringBuilder toAppendTo) {
ContextInternal context = (ContextInternal) Vertx.currentContext();
ContextInternal context = ContextInternal.current();
if (context != null && key != null) {
toAppendTo.append(ContextualData.getOrDefault(key, defaultValue));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Red Hat, Inc.
* Copyright 2023 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
Expand All @@ -18,7 +18,6 @@

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;

import static ch.qos.logback.core.util.OptionHelper.extractDefaultReplacement;
Expand Down Expand Up @@ -52,7 +51,7 @@ public void start() {

@Override
public String convert(ILoggingEvent event) {
ContextInternal context = (ContextInternal) Vertx.currentContext();
ContextInternal context = ContextInternal.current();
if (context != null && key != null) {
return ContextualData.getOrDefault(key, defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/*
* Copyright 2021 Red Hat, Inc.
* Copyright 2023 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
Expand All @@ -18,7 +18,6 @@
package io.reactiverse.contextual.logging;

import io.reactiverse.contextual.logging.impl.ContextualDataImpl;
import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;
import org.apache.logging.log4j.core.util.ContextDataProvider;

Expand All @@ -32,7 +31,7 @@ public class VertxContextDataProvider implements ContextDataProvider {

@Override
public Map<String, String> supplyContextData() {
ContextInternal ctx = (ContextInternal) Vertx.currentContext();
ContextInternal ctx = ContextInternal.current();
if (ctx != null) {
return Collections.unmodifiableMap(ContextualDataImpl.contextualDataMap(ctx));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Red Hat, Inc.
* Copyright 2023 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
Expand All @@ -16,7 +16,6 @@

package io.reactiverse.contextual.logging.impl;

import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
Expand All @@ -40,7 +39,7 @@ public class ContextualDataImpl {
public static void put(String key, String value) {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
ContextInternal ctx = (ContextInternal) Vertx.currentContext();
ContextInternal ctx = ContextInternal.current();
if (ctx == null) {
if (log.isTraceEnabled()) {
log.trace("Attempt to set contextual data from a non Vert.x thread", new Exception());
Expand All @@ -58,7 +57,7 @@ public static void put(String key, String value) {
*/
public static String get(String key) {
Objects.requireNonNull(key);
ContextInternal ctx = (ContextInternal) Vertx.currentContext();
ContextInternal ctx = ContextInternal.current();
if (ctx != null) {
return contextualDataMap(ctx).get(key);
}
Expand All @@ -74,7 +73,7 @@ public static String get(String key) {
*/
public static String getOrDefault(String key, String defaultValue) {
Objects.requireNonNull(key);
ContextInternal ctx = (ContextInternal) Vertx.currentContext();
ContextInternal ctx = ContextInternal.current();
if (ctx != null) {
return contextualDataMap(ctx).getOrDefault(key, defaultValue);
}
Expand All @@ -87,7 +86,7 @@ public static String getOrDefault(String key, String defaultValue) {
* @return the values or {@code null} if the method is invoked on a non Vert.x thread
*/
public static Map<String, String> getAll() {
ContextInternal ctx = (ContextInternal) Vertx.currentContext();
ContextInternal ctx = ContextInternal.current();
if (ctx != null) {
return new HashMap<>(contextualDataMap(ctx));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Red Hat, Inc.
* Copyright 2023 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
Expand All @@ -17,10 +17,9 @@
package io.reactiverse.contextual.logging;

import io.vertx.core.*;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServer;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
Expand Down Expand Up @@ -49,17 +48,29 @@ public class ContextualLoggingIT extends VertxTestBase {
private static final Logger log = LoggerFactory.getLogger(ContextualLoggingIT.class);

private Path logFile;
private WebClient webClient;
private HttpServer server;

@Override
public void setUp() throws Exception {
super.setUp();
logFile = Paths.get("target", ContextualLoggingIT.class.getSimpleName() + ".log");
webClient = WebClient.create(vertx, new WebClientOptions().setDefaultPort(8080));
server = vertx.createHttpServer()
.requestHandler(req -> req.response().end("Hello!"));
server.listen(8081, "127.0.0.1").toCompletionStage().toCompletableFuture().get();
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
Files.deleteIfExists(logFile);
if (webClient != null) {
webClient.close();
}
if (server != null) {
server.close().toCompletionStage().toCompletableFuture().get();
}
super.tearDown();
}

@Test
Expand All @@ -79,13 +90,13 @@ public void testContextualLogging() {
}

private void sendRequests(List<String> ids, Handler<AsyncResult<Void>> handler) {
WebClient webClient = WebClient.create(vertx, new WebClientOptions().setDefaultPort(8080));
HttpRequest<Buffer> request = webClient.get("/")
.expect(ResponsePredicate.SC_OK);
List<Future> futures = ids.stream()
.map(id -> request.putHeader(REQUEST_ID_HEADER, id).send())
List<Future<?>> futures = ids.stream()
.map(id -> webClient
.get("/")
.expect(ResponsePredicate.SC_OK)
.putHeader(REQUEST_ID_HEADER, id).send())
.collect(toList());
CompositeFuture.all(futures).<Void>mapEmpty().onComplete(handler);
Future.all(futures).<Void>mapEmpty().onComplete(handler);
}

private void verifyOutput(List<String> ids) throws IOException {
Expand All @@ -112,12 +123,13 @@ private void verifyOutput(List<String> ids) throws IOException {

private static class TestVerticle extends AbstractVerticle {

private HttpRequest<JsonObject> request;
private HttpRequest<String> request;
private WebClient webClient;

@Override
public void start(Promise<Void> startPromise) throws Exception {
WebClient webClient = WebClient.create(vertx);
request = webClient.getAbs("http://worldclockapi.com/api/json/utc/now").as(BodyCodec.jsonObject());
webClient = WebClient.create(vertx);
request = webClient.getAbs("http://127.0.0.1:8081").as(BodyCodec.string());

vertx.createHttpServer()
.requestHandler(req -> {
Expand All @@ -130,10 +142,10 @@ public void start(Promise<Void> startPromise) throws Exception {

log.info("Timer fired ### " + requestId);

vertx.executeBlocking(promise -> {
vertx.executeBlocking(() -> {

log.info("Blocking task executed ### " + requestId);
promise.complete();
return null;

}, false, bar -> {

Expand Down

0 comments on commit 3f7a965

Please sign in to comment.