forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebSockets Next: enable traffic logging for debugging purposes
- resolves quarkusio#37947
- Loading branch information
Showing
16 changed files
with
491 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...rc/test/java/io/quarkus/websockets/next/test/traffic/BasicConnectorTrafficLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkus.websockets.next.test.traffic; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.websockets.next.BasicWebSocketConnector; | ||
import io.quarkus.websockets.next.WebSocketClientConnection; | ||
import io.vertx.core.Context; | ||
|
||
public class BasicConnectorTrafficLoggerTest extends TrafficLoggerTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Endpoint.class); | ||
TrafficLoggerTest.addApplicationProperties(root, false); | ||
}) | ||
.setLogRecordPredicate(TrafficLoggerTest::isTrafficLogRecord) | ||
.assertLogRecords(logRecordsConsumer(true)); | ||
|
||
@Inject | ||
BasicWebSocketConnector connector; | ||
|
||
@Test | ||
public void testTrafficLogger() throws InterruptedException { | ||
List<String> messages = new CopyOnWriteArrayList<>(); | ||
CountDownLatch closedLatch = new CountDownLatch(1); | ||
CountDownLatch messageLatch = new CountDownLatch(1); | ||
WebSocketClientConnection conn = connector | ||
.baseUri(endUri) | ||
.path("end") | ||
.onTextMessage((c, m) -> { | ||
assertTrue(Context.isOnWorkerThread()); | ||
messages.add(m); | ||
messageLatch.countDown(); | ||
}) | ||
.onClose((c, s) -> closedLatch.countDown()) | ||
.connectAndAwait(); | ||
conn.sendTextAndAwait("dummy"); | ||
assertTrue(messageLatch.await(5, TimeUnit.SECONDS)); | ||
assertEquals("ok", messages.get(0)); | ||
conn.closeAndAwait(); | ||
assertTrue(closedLatch.await(5, TimeUnit.SECONDS)); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...rc/test/java/io/quarkus/websockets/next/test/traffic/ClientEndpointTrafficLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.websockets.next.test.traffic; | ||
Check failure on line 1 in extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/traffic/ClientEndpointTrafficLoggerTest.java GitHub Actions / Build summary for fa07049dba454cfd1dcd94298ce1c7036cef1e1bJVM Tests - JDK 17 Windows
Raw output
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.websockets.next.OnClose; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocketClient; | ||
import io.quarkus.websockets.next.WebSocketClientConnection; | ||
import io.quarkus.websockets.next.WebSocketConnector; | ||
|
||
public class ClientEndpointTrafficLoggerTest extends TrafficLoggerTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Endpoint.class, Client.class); | ||
TrafficLoggerTest.addApplicationProperties(root, false); | ||
}) | ||
.setLogRecordPredicate( | ||
TrafficLoggerTest::isTrafficLogRecord) | ||
.assertLogRecords(logRecordsConsumer(true)); | ||
|
||
@Inject | ||
WebSocketConnector<Client> connector; | ||
|
||
@Test | ||
public void testTrafficLogger() throws InterruptedException { | ||
WebSocketClientConnection conn = connector | ||
.baseUri(endUri) | ||
.connectAndAwait(); | ||
assertTrue(Client.MESSAGE_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertEquals("ok", Client.MESSAGES.get(0)); | ||
conn.closeAndAwait(); | ||
assertTrue(Client.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertTrue(Endpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); | ||
} | ||
|
||
@WebSocketClient(path = "/end") | ||
public static class Client { | ||
|
||
static final List<String> MESSAGES = new CopyOnWriteArrayList<>(); | ||
|
||
static final CountDownLatch MESSAGE_LATCH = new CountDownLatch(1); | ||
|
||
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1); | ||
|
||
@OnTextMessage | ||
void onMessage(String message) { | ||
MESSAGES.add(message); | ||
MESSAGE_LATCH.countDown(); | ||
} | ||
|
||
@OnClose | ||
void onClose() { | ||
CLOSED_LATCH.countDown(); | ||
} | ||
|
||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...oyment/src/test/java/io/quarkus/websockets/next/test/traffic/ServerTrafficLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.quarkus.websockets.next.test.traffic; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.vertx.core.Vertx; | ||
|
||
public class ServerTrafficLoggerTest extends TrafficLoggerTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Endpoint.class, WSClient.class); | ||
TrafficLoggerTest.addApplicationProperties(root, true); | ||
}) | ||
.setLogRecordPredicate(TrafficLoggerTest::isTrafficLogRecord) | ||
.assertLogRecords(logRecordsConsumer(false)); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@Test | ||
public void testTrafficLogger() throws InterruptedException, ExecutionException { | ||
try (WSClient client = new WSClient(vertx)) { | ||
client.connect(WSClient.toWS(endUri, "end")); | ||
client.waitForMessages(1); | ||
assertEquals("ok", client.getMessages().get(0).toString()); | ||
} | ||
assertTrue(Endpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...t/deployment/src/test/java/io/quarkus/websockets/next/test/traffic/TrafficLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.quarkus.websockets.next.test.traffic; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.function.Consumer; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogRecord; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
|
||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.websockets.next.OnClose; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.WebSocket; | ||
|
||
public abstract class TrafficLoggerTest { | ||
|
||
@TestHTTPResource("/") | ||
URI endUri; | ||
|
||
@WebSocket(path = "/end") | ||
public static class Endpoint { | ||
|
||
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1); | ||
|
||
@OnOpen | ||
public String open() { | ||
return "ok"; | ||
} | ||
|
||
@OnClose | ||
public void close() { | ||
CLOSED_LATCH.countDown(); | ||
} | ||
|
||
} | ||
|
||
static void addApplicationProperties(JavaArchive archive, boolean server) { | ||
archive.addAsResource(new StringAsset( | ||
"quarkus.websockets-next." + (server ? "server" : "client") + ".traffic-logging.enabled=true\n" | ||
+ "quarkus.log.category.\"io.quarkus.websockets.next.traffic\".level=DEBUG"), | ||
"application.properties"); | ||
} | ||
|
||
static Consumer<List<LogRecord>> logRecordsConsumer(boolean received) { | ||
return recs -> { | ||
assertTrue(recs.stream() | ||
.anyMatch(r -> r.getMessage().contains("%s connection opened:"))); | ||
assertTrue(recs.stream() | ||
.anyMatch(r -> r.getMessage() | ||
.contains("%s " + (received ? "received" : "sent") + " text message, Connection[%s]"))); | ||
assertTrue(recs.stream() | ||
.anyMatch(r -> r.getMessage().contains("%s connection closed,"))); | ||
}; | ||
} | ||
|
||
static boolean isTrafficLogRecord(LogRecord r) { | ||
return r.getLevel().equals(Level.FINE) | ||
&& r.getLoggerName().equals("io.quarkus.websockets.next.traffic"); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...ebsockets-next/runtime/src/main/java/io/quarkus/websockets/next/TrafficLoggingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.websockets.next; | ||
|
||
import io.smallrye.config.WithDefault; | ||
|
||
public interface TrafficLoggingConfig { | ||
|
||
/** | ||
* If set to true then binary/text messages received/sent are logged if the {@code DEBUG} level is enabled for the | ||
* logger {@code io.quarkus.websockets.next.traffic}. | ||
*/ | ||
@WithDefault("false") | ||
public boolean enabled(); | ||
|
||
/** | ||
* The number of characters of a text message which will be logged if traffic logging is enabled. The payload of a | ||
* binary message is never logged. | ||
*/ | ||
@WithDefault("100") | ||
public int textPayloadLimit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.