Fix the poll loop that replaced a test sleep

Review the AI's codeEasy10 minAnthropic style

Your task

A flaky Go integration test was "fixed" by an agent that added time.Sleep(500 * time.Millisecond) before the assertion. The real fix is to poll the condition until it holds or a deadline passes. To test the polling logic without a real clock, you model it over a log of what each poll sees.

An assistant wrote poll_until_ready(state_log, poll_interval_ms, timeout_ms). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • state_log: what the condition reads on each poll, in order. true means ready.
  • poll_interval_ms: the time between polls.
  • timeout_ms: the deadline, measured from the first poll.
  • Return the elapsed time, in milliseconds, of the first poll that reads true, or -1.

Rules the tests check:

  • An interval of 0 or less counts as 1 millisecond.
  • Poll number k, counting from 0, happens at elapsed time k times the interval and reads state_log[k].
  • A poll happens only when its time is at most timeout_ms. A poll exactly at the deadline still happens.
  • Return the time of the first poll that reads true. If the log runs out or the deadline passes first, return -1.

Examples

poll_until_ready(state_log: list[bool], poll_interval_ms: int, timeout_ms: int) -> int
Example 1
Inputpoll_until_ready([False, False, True], 50, 500)
Output100
Example 2
Inputpoll_until_ready([True], 50, 500)
Output0
Example 3
Inputpoll_until_ready([False, False, False], 50, 500)
Output-1