Shortcut to assert a single tracked packet

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-01-22 03:54:24 +01:00
parent 19be2546e6
commit 57976a1595
3 changed files with 20 additions and 12 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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