Beispiel #1
0
def test_timer_external():
    """Test externally-clocked timer functionality."""
    timer = TimeoutTimer(timeout=0.2, clock=Clock(time=0.0))
    assert timer.timeout_time is None
    assert timer.elapsed is None
    assert_timer_stopped(timer)

    # Basic test
    timer.clock.update(1.0)
    timer.start()
    assert repr(timer) == 'TimeoutTimer(running from 1.0, timeout=0.2)'
    assert timer.timeout_time == 1.2
    timer.clock.update(1.1)
    assert_timer_halfway(timer)
    timer.clock.update(1.2)
    assert_timer_finished(timer)
    timer.clock.update(1.3)
    assert_timer_finished(timer)
    timer.clock.update(0.0)
    timer.reset()
    timer.clock.update(0.1)
    assert_timer_halfway(timer)
    timer.clock.update(0.2)
    assert_timer_finished(timer)

    # Shared-clock test
    timer.clock.update(1.0)
    timer.reset()
    timer_slow = TimeoutTimer(timeout=0.4, clock=timer.clock)
    timer_slow.start()
    timer.clock.update(1.1)
    assert_timer_halfway(timer)
    timer.clock.update(1.2)
    assert_timer_finished(timer)
    assert_timer_halfway(timer_slow)
    timer.clock.update(1.4)
    assert_timer_finished(timer)
    assert_timer_finished(timer_slow)
Beispiel #2
0
def test_timer_realtime():
    """Test real-time timer functionality."""
    timer = TimeoutTimer(timeout=0.2)
    assert timer.timeout_time is None
    assert timer.elapsed is None
    timer.enabled = True
    assert timer.elapsed is None  # no start time was set due to invalid state change
    timer.enabled = False
    assert_timer_stopped(timer)

    # Basic test
    timer.start()
    assert_timer_running_realtime(timer)

    # Stop-and-start test
    timer.reset_and_stop()
    assert_timer_stopped(timer)
    timer.reset()
    assert_timer_stopped(timer)
    timer.start()
    assert_timer_running_realtime(timer)

    # Reset test
    timer.reset()
    assert_timer_running_realtime(timer)
    timer.reset()
    assert_timer_running_realtime(timer)

    # Start test
    timer.start(timeout=0.1)
    assert_timer_running_realtime(timer)
    timer.reset()
    assert_timer_running_realtime(timer)
    timer.start()
    assert_timer_running_realtime(timer)

    # Externally-clocked mode-switching test
    timer.clock.reset(0.0)
    timer.start(timeout=0.2)
    timer.clock.update(0.1)
    assert_timer_halfway(timer)
    timer.clock.update(0.2)
    assert_timer_finished(timer)