extends TestCase ## Issue N3: the clock that decides which tick a client's next command is for. const MARGIN: int = Tuning.NET_COMMAND_MARGIN_TICKS const DT: float = 1.0 / Tuning.TICK_RATE_HZ var clock: ClientClock func before_each() -> void: clock = ClientClock.new() clock.start(100) func test_it_starts_ahead_of_the_server_and_keeps_time() -> void: assert_true(clock.running) assert_eq(clock.tick(), 100 + Tuning.NET_COMMAND_LEAD_START_TICKS) for _tick: int in 30: clock.advance(DT) assert_eq(clock.tick(), 130 + Tuning.NET_COMMAND_LEAD_START_TICKS) clock.advance(DT * 0.25) assert_near(clock.fraction(), 0.25, 0.001) clock.stop() clock.advance(1.0) assert_eq(clock.tick(), 130 + Tuning.NET_COMMAND_LEAD_START_TICKS, "a stopped clock stays put") func test_commands_arriving_late_twice_running_make_it_jump_ahead() -> void: var start: int = clock.tick() for _tick: int in 10: clock.advance(DT) # The server was about to run a tick and the newest command it had was for two # ticks earlier: four short of having MARGIN waiting. clock.observe(start + 9, start + 7) assert_eq(clock.tick(), start + 10, "once could be a burst of lost packets") assert_eq(clock.jumps, 0) for _tick: int in 2: clock.advance(DT) clock.observe(start + 11, start + 9) assert_eq(clock.jumps, 1) assert_eq(clock.tick(), start + 12 + MARGIN + 1) # Echoes of commands sent before the jump say nothing about the clock now. var after: int = clock.tick() clock.observe(start + 13, start + 11) clock.observe(start + 15, start + 12) assert_eq(clock.tick(), after) assert_eq(clock.jumps, 1) # And the first made after it shows the margin is right. clock.observe(after - MARGIN + 1, after) clock.observe(after - MARGIN + 3, after + 2) assert_eq(clock.jumps, 1) assert_near(clock.margin, MARGIN, 0.01) func test_one_late_report_between_good_ones_changes_nothing() -> void: var start: int = clock.tick() for round: int in 20: var sim_tick: int = start + 10 + round * 2 var waiting: int = -1 if round % 5 == 2 else MARGIN clock.observe(sim_tick, sim_tick + waiting - 1) assert_eq(clock.jumps, 0) assert_eq(clock.tick(), start) func test_far_too_early_and_it_waits_for_the_server() -> void: var start: int = clock.tick() clock.observe(110, 110 + MARGIN + 20) assert_eq(clock.jumps, 1) assert_true(clock.tick() < start - 10, "went back from %d to %d" % [start, clock.tick()]) func test_a_small_error_is_worked_off_by_running_fast_or_slow() -> void: # One command too many waiting, every snapshot: the clock should lose a tick, slowly. var sim_tick: int = 110 var truth: float = float(clock.tick()) for _round: int in 600: clock.observe(sim_tick, sim_tick + MARGIN) for _tick: int in 2: clock.advance(DT) truth += 1.0 sim_tick += 2 assert_eq(clock.jumps, 0) var lost: float = truth - (float(clock.tick()) + clock.fraction()) assert_true(lost > 0.5 and lost < 30.0, "ran %.2f ticks slow over 40 s" % lost) assert_true(clock.margin > MARGIN + 0.5, "smoothed margin %.2f" % clock.margin) func test_no_command_received_yet_says_nothing() -> void: var start: int = clock.tick() for round: int in 10: clock.observe(110 + round * 2, -1) assert_eq(clock.tick(), start) assert_eq(clock.jumps, 0)