You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/03/08 14:53:13 UTC
(shardingsphere) branch master updated: Add more test cases on PacketCodecTest (#38377)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 283d752fcee Add more test cases on PacketCodecTest (#38377)
283d752fcee is described below
commit 283d752fceef81044ddc7c3c512b4c77cc843a8e
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Mar 8 22:53:07 2026 +0800
Add more test cases on PacketCodecTest (#38377)
---
database/protocol/core/pom.xml | 6 ++
.../database/protocol/codec/PacketCodecTest.java | 98 ++++++++++++++++++----
2 files changed, 87 insertions(+), 17 deletions(-)
diff --git a/database/protocol/core/pom.xml b/database/protocol/core/pom.xml
index 9d06ff90a8c..27fb669b4f0 100644
--- a/database/protocol/core/pom.xml
+++ b/database/protocol/core/pom.xml
@@ -33,6 +33,12 @@
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-test-infra-framework</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-infra-fixture-database</artifactId>
diff --git a/database/protocol/core/src/test/java/org/apache/shardingsphere/database/protocol/codec/PacketCodecTest.java b/database/protocol/core/src/test/java/org/apache/shardingsphere/database/protocol/codec/PacketCodecTest.java
index bcca4f7fb59..858b64d2062 100644
--- a/database/protocol/core/src/test/java/org/apache/shardingsphere/database/protocol/codec/PacketCodecTest.java
+++ b/database/protocol/core/src/test/java/org/apache/shardingsphere/database/protocol/codec/PacketCodecTest.java
@@ -17,23 +17,31 @@
package org.apache.shardingsphere.database.protocol.codec;
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelId;
import org.apache.shardingsphere.database.protocol.packet.DatabasePacket;
+import org.apache.shardingsphere.test.infra.framework.extension.log.LogCaptureAssertion;
+import org.apache.shardingsphere.test.infra.framework.extension.log.LogCaptureExtension;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import org.slf4j.LoggerFactory;
-import java.util.Collections;
+import java.util.LinkedList;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-@ExtendWith(MockitoExtension.class)
+@ExtendWith({MockitoExtension.class, LogCaptureExtension.class})
class PacketCodecTest {
@Mock
@@ -43,35 +51,91 @@ class PacketCodecTest {
private ChannelHandlerContext context;
@Mock
- private ByteBuf byteBuf;
+ private Channel channel;
+
+ @Mock
+ private ChannelId channelId;
private PacketCodec packetCodec;
+ private Logger packetCodecLogger;
+
+ private Level originalLevel;
+
@BeforeEach
void setUp() {
packetCodec = new PacketCodec(databasePacketCodecEngine);
+ packetCodecLogger = (Logger) LoggerFactory.getLogger(PacketCodec.class);
+ originalLevel = packetCodecLogger.getLevel();
+ }
+
+ @AfterEach
+ void tearDown() {
+ packetCodecLogger.setLevel(originalLevel);
}
@Test
- void assertDecodeWithValidHeader() {
- when(byteBuf.readableBytes()).thenReturn(1);
- when(databasePacketCodecEngine.isValidHeader(1)).thenReturn(true);
- packetCodec.decode(context, byteBuf, Collections.emptyList());
- verify(databasePacketCodecEngine).decode(context, byteBuf, Collections.emptyList());
+ void assertDecodeWithInvalidHeader(final LogCaptureAssertion logCaptureAssertion) {
+ ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
+ LinkedList<Object> actualOut = new LinkedList<>();
+ packetCodecLogger.setLevel(Level.INFO);
+ when(databasePacketCodecEngine.isValidHeader(input.readableBytes())).thenReturn(false);
+ packetCodec.decode(context, input, actualOut);
+ verify(databasePacketCodecEngine).isValidHeader(input.readableBytes());
+ logCaptureAssertion.assertLogCount(0);
}
@Test
- void assertDecodeWithInvalidHeader() {
- when(byteBuf.readableBytes()).thenReturn(1);
- when(databasePacketCodecEngine.isValidHeader(1)).thenReturn(false);
- packetCodec.decode(context, byteBuf, Collections.emptyList());
- verify(databasePacketCodecEngine, never()).decode(context, byteBuf, Collections.emptyList());
+ void assertDecodeWithDebugDisabled(final LogCaptureAssertion logCaptureAssertion) {
+ ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
+ LinkedList<Object> actualOut = new LinkedList<>();
+ packetCodecLogger.setLevel(Level.INFO);
+ when(databasePacketCodecEngine.isValidHeader(input.readableBytes())).thenReturn(true);
+ packetCodec.decode(context, input, actualOut);
+ verify(databasePacketCodecEngine).isValidHeader(input.readableBytes());
+ verify(databasePacketCodecEngine).decode(context, input, actualOut);
+ logCaptureAssertion.assertLogCount(0);
}
@Test
- void assertEncode() {
+ void assertDecodeWithDebugEnabled(final LogCaptureAssertion logCaptureAssertion) {
+ ByteBuf input = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
+ packetCodecLogger.setLevel(Level.DEBUG);
+ mockChannelId();
+ when(databasePacketCodecEngine.isValidHeader(input.readableBytes())).thenReturn(true);
+ LinkedList<Object> actualOut = new LinkedList<>();
+ packetCodec.decode(context, input, actualOut);
+ verify(databasePacketCodecEngine).isValidHeader(input.readableBytes());
+ verify(databasePacketCodecEngine).decode(context, input, actualOut);
+ logCaptureAssertion.assertLogCount(1);
+ logCaptureAssertion.assertLogContent(0, Level.DEBUG, "Read from client {} :\n{}", false);
+ }
+
+ @Test
+ void assertEncodeWithDebugDisabled(final LogCaptureAssertion logCaptureAssertion) {
DatabasePacket databasePacket = mock(DatabasePacket.class);
- packetCodec.encode(context, databasePacket, byteBuf);
- verify(databasePacketCodecEngine).encode(context, databasePacket, byteBuf);
+ ByteBuf actualOut = Unpooled.buffer();
+ packetCodecLogger.setLevel(Level.INFO);
+ packetCodec.encode(context, databasePacket, actualOut);
+ verify(databasePacketCodecEngine).encode(context, databasePacket, actualOut);
+ logCaptureAssertion.assertLogCount(0);
+ }
+
+ @Test
+ void assertEncodeWithDebugEnabled(final LogCaptureAssertion logCaptureAssertion) {
+ DatabasePacket databasePacket = mock(DatabasePacket.class);
+ ByteBuf actualOut = Unpooled.buffer();
+ packetCodecLogger.setLevel(Level.DEBUG);
+ mockChannelId();
+ packetCodec.encode(context, databasePacket, actualOut);
+ verify(databasePacketCodecEngine).encode(context, databasePacket, actualOut);
+ logCaptureAssertion.assertLogCount(1);
+ logCaptureAssertion.assertLogContent(0, Level.DEBUG, "Write to client {} :\n{}", false);
+ }
+
+ private void mockChannelId() {
+ when(context.channel()).thenReturn(channel);
+ when(channel.id()).thenReturn(channelId);
+ when(channelId.asShortText()).thenReturn("foo_id");
}
}