mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 02:57:37 +01:00
Shortcut to assert a single tracked packet
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
19be2546e6
commit
57976a1595
@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface TestConnection {
|
||||
@NotNull CompletableFuture<@NotNull Player> connect(@NotNull Instance instance, @NotNull Pos pos);
|
||||
@ -16,5 +17,7 @@ public interface TestConnection {
|
||||
|
||||
interface PacketTracker<T> {
|
||||
@NotNull List<@NotNull T> collect();
|
||||
|
||||
<P extends T> void assertSingle(Class<P> packetType, Consumer<P> consumer);
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,10 @@ import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
|
||||
final class TestConnectionImpl implements TestConnection {
|
||||
private final Env env;
|
||||
@ -93,5 +97,14 @@ final class TestConnectionImpl implements TestConnection {
|
||||
incomingTrackers.remove(this);
|
||||
return List.copyOf(packets);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <P extends T> void assertSingle(Class<P> packetType, Consumer<P> consumer) {
|
||||
var packets = collect();
|
||||
assertEquals(1, packets.size(), "Expected 1 packet, got " + packets);
|
||||
var packet = packets.get(0);
|
||||
assertInstanceOf(packetType, packet, "Expected packet of type " + packetType.getSimpleName() + ", got " + packet.getClass().getSimpleName());
|
||||
consumer.accept((P) packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -55,21 +55,13 @@ public class EntityTeleportIntegrationTest {
|
||||
assertEquals(teleportPosition, player.getPosition());
|
||||
|
||||
// Verify received packet(s)
|
||||
{
|
||||
var packets = tracker.collect();
|
||||
assertEquals(1, packets.size());
|
||||
var packet = ((PlayerPositionAndLookPacket) packets.get(0));
|
||||
assertEquals(teleportPosition, packet.position());
|
||||
}
|
||||
|
||||
tracker.assertSingle(PlayerPositionAndLookPacket.class,
|
||||
packet -> assertEquals(teleportPosition, packet.position()));
|
||||
// Verify broadcast packet(s)
|
||||
{
|
||||
var packets = viewerTracker.collect();
|
||||
assertEquals(1, packets.size());
|
||||
var packet = ((EntityTeleportPacket) packets.get(0));
|
||||
viewerTracker.assertSingle(EntityTeleportPacket.class, packet -> {
|
||||
assertEquals(player.getEntityId(), packet.entityId());
|
||||
assertEquals(teleportPosition, packet.position());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user