Fix issue
Fix test_clock_fps by using a busy sleep and using pyglet clock's
time function to measure time accurately.
@@ -11,6 +11,12 @@ import unittest
from pyglet import clock
+def sleep(seconds):
+ "Busy sleep on the CPU which is very precise"
+ pyclock = clock.get_default()
+ start = pyclock.time()
+ while pyclock.time() - start < seconds:
+ pass
class ClockTimingTestCase(unittest.TestCase):
@@ -41,23 +47,21 @@ class ClockTimingTestCase(unittest.TestCase):
Because we are measuring time differences, we
expect a small error (1%) from the expected value.
"""
- pyclock = clock.get_default()
sleep_time = 0.2
-
# initialize internal counter
clock.tick()
# test between initialization and first tick
- pyclock.sleep(sleep_time * 1e6)
+ sleep(sleep_time)
delta_time_1 = clock.tick()
# test between non-initialization tick and next tick
- pyclock.sleep(sleep_time * 1e6)
+ sleep(sleep_time)
delta_time_2 = clock.tick()
- self.assertAlmostEqual(delta_time_1, sleep_time, delta=0.01*sleep_time)
- self.assertAlmostEqual(delta_time_2, sleep_time, delta=0.01*sleep_time)
+ self.assertAlmostEqual(delta_time_1, sleep_time, delta=0.015*sleep_time)
+ self.assertAlmostEqual(delta_time_2, sleep_time, delta=0.015*sleep_time)
def test_compute_fps(self):
"""
@@ -89,15 +93,16 @@ class ClockTimingTestCase(unittest.TestCase):
ticks = 20
fps_limit = 60
expected_delta_time = ticks*1./fps_limit
- pyclock = clock.get_default()
clock.set_fps_limit(fps_limit)
+ pyclock = clock.get_default()
t1 = pyclock.time()
# Initializes the timer state.
clock.tick()
for i in range(ticks):
clock.tick()
+
t2 = pyclock.time()
computed_time_delta = t2 - t1
