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:| Method | Description |
|---|---|
assertPacketRoundTrip(packet) | Encode the packet, decode it back, and assert the result is non-null |
How It Works
When you callassertPacketRoundTrip, the adapter:
- Looks for a
CODECstatic field on the packet class - If found, calls
codec.encode(packet)thencodec.decode(encoded) - Asserts the decoded result is non-null
- If no
CODECfield exists, checks for theNetworkSerializableinterface as a fallback
Examples
Test a simple packet
Test a simple packet
Test a complex packet with nested data
Test a complex packet with nested data
Test a packet from your custom mod
Test a packet from your custom mod
Relationship to CodecAssert
NetworkTestAdapter and CodecAssert are complementary:
| Use Case | Tool |
|---|---|
| Test a standalone codec (component, data type) | CodecAssert.assertRoundTrip() |
| Test a network packet’s codec | NetworkTestAdapter.assertPacketRoundTrip() |
| Test decode of specific BSON values | CodecAssert.assertDecodeEquals() |
| Test decode of malformed data | CodecAssert.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.
Failure Cases
If the packet class has neither aCODEC field nor a NetworkSerializable interface, the test fails with:
Next Steps
- Codec Testing — deeper serialization testing
- Benchmarks — measure serialization performance
- ECS Testing — test component serialization