Define wrap-safe snapshot sequencing
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
from snapshot_sequence import MAX_SEQUENCE, SequenceCounter, SequenceWindow, is_newer, next_sequence
|
||||
|
||||
|
||||
def test_next_sequence_skips_zero_after_wrap():
|
||||
assert next_sequence(0) == 1
|
||||
assert next_sequence(1) == 2
|
||||
assert next_sequence(MAX_SEQUENCE) == 1
|
||||
|
||||
|
||||
def test_is_newer_is_wrap_safe():
|
||||
assert is_newer(1, 0)
|
||||
assert is_newer(2, 1)
|
||||
assert not is_newer(1, 1)
|
||||
assert not is_newer(1, 2)
|
||||
assert is_newer(1, MAX_SEQUENCE)
|
||||
assert not is_newer(MAX_SEQUENCE, 1)
|
||||
assert not is_newer(0, MAX_SEQUENCE)
|
||||
|
||||
|
||||
def test_counter_and_window_enforce_latest_wins():
|
||||
counter = SequenceCounter()
|
||||
assert counter.current == 0
|
||||
assert counter.advance() == 1
|
||||
assert counter.advance() == 2
|
||||
counter.reset()
|
||||
assert counter.advance() == 1
|
||||
|
||||
window = SequenceWindow()
|
||||
assert window.accept(1)
|
||||
assert not window.accept(1)
|
||||
assert window.accept(2)
|
||||
assert not window.accept(1)
|
||||
window.reset()
|
||||
assert window.accept(MAX_SEQUENCE)
|
||||
assert window.accept(1)
|
||||
assert not window.accept(MAX_SEQUENCE)
|
||||
|
||||
|
||||
def test_invalid_sequence_inputs_are_rejected():
|
||||
assert not is_newer(-1, 0)
|
||||
assert not is_newer(1, -1)
|
||||
assert not is_newer(True, 0)
|
||||
assert not is_newer(1, MAX_SEQUENCE + 1)
|
||||
@@ -1,4 +1,9 @@
|
||||
from transport_policy import Delivery, delivery_for_packet_type, is_snapshot_packet
|
||||
from transport_policy import (
|
||||
Delivery,
|
||||
delivery_for_packet_type,
|
||||
is_snapshot_packet,
|
||||
requires_application_sequence,
|
||||
)
|
||||
|
||||
|
||||
def test_snapshot_packets_use_unreliable_sequenced_delivery():
|
||||
@@ -6,6 +11,8 @@ def test_snapshot_packets_use_unreliable_sequenced_delivery():
|
||||
assert delivery_for_packet_type("npcState") is Delivery.UNRELIABLE_SEQUENCED
|
||||
assert is_snapshot_packet("transform")
|
||||
assert is_snapshot_packet("npcState")
|
||||
assert requires_application_sequence("transform")
|
||||
assert requires_application_sequence("npcState")
|
||||
|
||||
|
||||
def test_gameplay_and_control_packets_use_reliable_ordered_delivery():
|
||||
@@ -25,7 +32,9 @@ def test_gameplay_and_control_packets_use_reliable_ordered_delivery():
|
||||
for packet_type in reliable_types:
|
||||
assert delivery_for_packet_type(packet_type) is Delivery.RELIABLE_ORDERED
|
||||
assert not is_snapshot_packet(packet_type)
|
||||
assert not requires_application_sequence(packet_type)
|
||||
|
||||
|
||||
def test_unknown_packet_types_never_default_to_unreliable():
|
||||
assert delivery_for_packet_type("futureControlPacket") is Delivery.RELIABLE_ORDERED
|
||||
assert not requires_application_sequence("futureControlPacket")
|
||||
|
||||
Reference in New Issue
Block a user