Test CachedPacket

This commit is contained in:
themode 2022-01-01 20:07:58 +01:00 committed by TheMode
parent 8a18913d32
commit e169b631d5

View File

@ -2,20 +2,21 @@ package network;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.minestom.server.message.ChatPosition; import net.minestom.server.message.ChatPosition;
import net.minestom.server.network.packet.server.CachedPacket;
import net.minestom.server.network.packet.server.LazyPacket; import net.minestom.server.network.packet.server.LazyPacket;
import net.minestom.server.network.packet.server.play.ChatMessagePacket; import net.minestom.server.network.packet.server.play.ChatMessagePacket;
import net.minestom.server.utils.PacketUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.fail;
public class LazyPacketTest { public class SendablePacketTest {
@Test @Test
public void testCache() { public void lazy() {
var packet = new ChatMessagePacket(Component.text("Hello World!"), ChatPosition.CHAT, UUID.randomUUID()); var packet = new ChatMessagePacket(Component.text("Hello World!"), ChatPosition.CHAT, UUID.randomUUID());
AtomicBoolean called = new AtomicBoolean(false); AtomicBoolean called = new AtomicBoolean(false);
var lazy = new LazyPacket(() -> { var lazy = new LazyPacket(() -> {
@ -26,4 +27,17 @@ public class LazyPacketTest {
assertSame(packet, lazy.packet()); assertSame(packet, lazy.packet());
assertSame(packet, lazy.packet()); assertSame(packet, lazy.packet());
} }
@Test
public void cached() {
var packet = new ChatMessagePacket(Component.text("Hello World!"), ChatPosition.CHAT, UUID.randomUUID());
var cached = new CachedPacket(packet);
assertSame(packet, cached.packet());
var buffer = PacketUtils.allocateTrimmedPacket(packet);
assertEquals(buffer.body(), cached.body());
assertEquals(buffer.body(), cached.body());
assertSame(packet, cached.packet());
}
} }