Skip to main content
Network packets in Hytale are serialized using codecs or the NetworkSerializable interface. HRTK’s NetworkTestAdapter lets you verify that packets survive an encode-decode round-trip without data loss.

NetworkTestAdapter

The adapter provides a single key method:
MethodDescription
assertPacketRoundTrip(packet)Encode the packet, decode it back, and assert the result is non-null

How It Works

When you call assertPacketRoundTrip, the adapter:
  1. Looks for a CODEC static field on the packet class
  2. If found, calls codec.encode(packet) then codec.decode(encoded)
  3. Asserts the decoded result is non-null
  4. If no CODEC field exists, checks for the NetworkSerializable interface as a fallback
import com.frotty27.hrtk.server.surface.NetworkTestAdapter;

@HytaleTest
void testChatPacketRoundTrip() {
    NetworkTestAdapter adapter = new NetworkTestAdapter();

    ChatPacket packet = new ChatPacket("Hello, world!");
    adapter.assertPacketRoundTrip(packet);
}

Examples

@HytaleTest
@Tag("networking")
void pingPacketRoundTrips() {
    NetworkTestAdapter adapter = new NetworkTestAdapter();
    PingPacket packet = new PingPacket(System.currentTimeMillis());
    adapter.assertPacketRoundTrip(packet);
}
@HytaleTest
@Tag("networking")
void inventoryUpdatePacketRoundTrips() {
    NetworkTestAdapter adapter = new NetworkTestAdapter();

    InventoryUpdatePacket packet = new InventoryUpdatePacket();
    packet.setSlot(0);
    packet.setItemId("hytale:iron_sword");
    packet.setQuantity(1);

    adapter.assertPacketRoundTrip(packet);
}
@HytaleTest
@Tag("networking")
void customModPacketRoundTrips() {
    NetworkTestAdapter adapter = new NetworkTestAdapter();

    MyModSyncPacket packet = new MyModSyncPacket();
    packet.setData(new byte[]{1, 2, 3, 4, 5});

    adapter.assertPacketRoundTrip(packet);
}

Relationship to CodecAssert

NetworkTestAdapter and CodecAssert are complementary:
Use CaseTool
Test a standalone codec (component, data type)CodecAssert.assertRoundTrip()
Test a network packet’s codecNetworkTestAdapter.assertPacketRoundTrip()
Test decode of specific BSON valuesCodecAssert.assertDecodeEquals()
Test decode of malformed dataCodecAssert.assertDecodeThrows()
NetworkTestAdapter is a convenience wrapper that automatically locates the codec on the packet class. If you need more control (custom equality checks, malformed input testing), use CodecAssert directly with the packet’s CODEC field.
Packet round-trip tests are excellent candidates for @ParameterizedTest. Create packets with different field combinations and verify they all survive serialization.

Failure Cases

If the packet class has neither a CODEC field nor a NetworkSerializable interface, the test fails with:
Packet class MyPacket has no CODEC field or NetworkSerializable interface
If the codec’s encode or decode method throws, the test fails with the exception message.
assertPacketRoundTrip currently asserts that the decoded packet is non-null but does not deep-compare field values. For field-level verification, use CodecAssert.assertRoundTrip() with a custom equality predicate.

Next Steps