Пример #1
0
 def test__current_timeline_replacing(self):
     self.addCleanup(flux.current_timeline.set, flux.current_timeline.get())
     new_timeline = Timeline()
     new_factor = 12345
     flux.current_timeline.set(new_timeline)
     flux.current_timeline.set_time_factor(new_factor)
     self.assertEqual(flux.current_timeline.get_time_factor(), new_factor)
     self.assertEqual(new_timeline.get_time_factor(), new_factor)
Пример #2
0
 def test__current_timeline_replacing(self):
     self.addCleanup(flux.current_timeline.set, flux.current_timeline.get())
     new_timeline = Timeline()
     new_factor = 12345
     flux.current_timeline.set(new_timeline)
     flux.current_timeline.set_time_factor(new_factor)
     self.assertEquals(flux.current_timeline.get_time_factor(), new_factor)
     self.assertEquals(new_timeline.get_time_factor(), new_factor)
Пример #3
0
 def _forge_timeline(self):
     self.forge = forge.Forge()
     self.forge.replace_with(Timeline, "_real_time", self.time)
     self.forge.replace_with(Timeline, "_real_sleep", self.sleep)
     self.timeline = Timeline()
Пример #4
0
class TimeFactorTest(TestCase):
    def setUp(self):
        super(TimeFactorTest, self).setUp()
        self._real_time = self._start_real_time = 1337.0
        self._callback_calls = []
        self._forge_timeline()

    def _forge_timeline(self):
        self.forge = forge.Forge()
        self.forge.replace_with(Timeline, "_real_time", self.time)
        self.forge.replace_with(Timeline, "_real_sleep", self.sleep)
        self.timeline = Timeline()

    def tearDown(self):
        self.forge.restore_all_replacements()
        self.forge.verify()
        self.forge.reset()
        super(TimeFactorTest, self).tearDown()

    def callback(self):
        self._callback_calls.append(self.timeline.time())

    def time(self):
        return self._real_time

    def sleep(self, seconds):
        assert seconds >= 0
        self._real_time += seconds

    def test__default_factor(self):
        start_time = self.timeline.time()
        self.sleep(10)
        self.assertEqual(self.timeline.time(), start_time + 10)
        self.assertEqual(self._real_time, self._start_real_time + 10)

    def test__scheduled_callbacks(self):
        start_time = self.timeline.time()
        schedule_delay = 5
        schedule_time = start_time + schedule_delay
        self.timeline.schedule_callback(schedule_delay, self.callback)
        self.sleep(10)
        # need to trigger the callbacks
        self.timeline.sleep(0)
        self.assertEqual(self.timeline.time(), start_time + 10)
        self.assertEqual(self._callback_calls, [schedule_time])

    def test__factor_changes_real_sleeps(self):
        self._test__factor_changes(real_sleep=True)

    def test__factor_changes_fake_sleeps(self):
        self._test__factor_changes(real_sleep=False)

    def test__freeze(self):
        self.timeline.freeze()
        self.assertEqual(self.timeline.get_time_factor(), 0)

    def _test__factor_changes(self, real_sleep):
        expected_virtual_time = self.timeline.time()
        expected_real_time = self._real_time
        for index, (factor, sleep) in enumerate([
            (1, 20),
            (2.5, 3),
            (0.5, 70),
            (0, 30),
        ]):
            vtime_before_factor_change = self.timeline.time()
            self.timeline.set_time_factor(factor)
            vtime_after_factor_change = self.timeline.time()
            self.assertEqual(
                vtime_before_factor_change, vtime_after_factor_change,
                "set_time_factor() unexpectedly changed virtual time!")
            if real_sleep:
                self.sleep(sleep)
                expected_virtual_time += (sleep * factor)
                expected_real_time += sleep
            else:
                self.timeline.sleep(sleep)
                expected_virtual_time += sleep
                if factor != 0:
                    expected_real_time += sleep / factor
            self._assert_equals(
                expected_virtual_time, self.timeline.time(),
                "Sleep {0}X{1} did not sleep virtual time as expected".format(
                    sleep, factor))
            self._assert_equals(
                expected_real_time, self._real_time,
                "Sleep {0}X{1} did not sleep real time as expected".format(
                    sleep, factor))

    def _assert_equals(self, a, b, msg):
        assert a == b, "{0} ({1} != {2})".format(msg, a, b)
Пример #5
0
 def _get_timeline(self):
     return Timeline()
Пример #6
0
 def _forge_timeline(self):
     self.forge = forge.Forge()
     self.forge.replace_with(Timeline, "_real_time", self.time)
     self.forge.replace_with(Timeline, "_real_sleep", self.sleep)
     self.timeline = Timeline()
Пример #7
0
class TimeFactorTest(TestCase):

    def setUp(self):
        super(TimeFactorTest, self).setUp()
        self._real_time = self._start_real_time = 1337.0
        self._callback_calls = []
        self._forge_timeline()

    def _forge_timeline(self):
        self.forge = forge.Forge()
        self.forge.replace_with(Timeline, "_real_time", self.time)
        self.forge.replace_with(Timeline, "_real_sleep", self.sleep)
        self.timeline = Timeline()

    def tearDown(self):
        self.forge.restore_all_replacements()
        self.forge.verify()
        self.forge.reset()
        super(TimeFactorTest, self).tearDown()

    def callback(self):
        self._callback_calls.append(self.timeline.time())

    def time(self):
        return self._real_time

    def sleep(self, seconds):
        assert seconds >= 0
        self._real_time += seconds

    def test__default_factor(self):
        start_time = self.timeline.time()
        self.sleep(10)
        self.assertEquals(self.timeline.time(), start_time + 10)
        self.assertEquals(self._real_time, self._start_real_time + 10)

    def test__scheduled_callbacks(self):
        start_time = self.timeline.time()
        schedule_delay = 5
        schedule_time = start_time + schedule_delay
        self.timeline.schedule_callback(schedule_delay, self.callback)
        self.sleep(10)
        # need to trigger the callbacks
        self.timeline.sleep(0)
        self.assertEquals(self.timeline.time(), start_time + 10)
        self.assertEquals(self._callback_calls, [schedule_time])

    def test__factor_changes_real_sleeps(self):
        self._test__factor_changes(real_sleep=True)

    def test__factor_changes_fake_sleeps(self):
        self._test__factor_changes(real_sleep=False)

    def test__freeze(self):
        self.timeline.freeze()
        self.assertEquals(self.timeline.get_time_factor(), 0)

    def _test__factor_changes(self, real_sleep):
        expected_virtual_time = self.timeline.time()
        expected_real_time = self._real_time
        for index, (factor, sleep) in enumerate([
            (1, 20),
            (2.5, 3),
            (0.5, 70),
            (0, 30),
        ]):
            vtime_before_factor_change = self.timeline.time()
            self.timeline.set_time_factor(factor)
            vtime_after_factor_change = self.timeline.time()
            self.assertEquals(
                vtime_before_factor_change, vtime_after_factor_change,
                "set_time_factor() unexpectedly changed virtual time!")
            if real_sleep:
                self.sleep(sleep)
                expected_virtual_time += (sleep * factor)
                expected_real_time += sleep
            else:
                self.timeline.sleep(sleep)
                expected_virtual_time += sleep
                if factor != 0:
                    expected_real_time += sleep / factor
            self._assert_equals(expected_virtual_time, self.timeline.time(),
                                "Sleep {0}X{1} did not sleep virtual time as expected".format(sleep, factor))
            self._assert_equals(expected_real_time, self._real_time,
                                "Sleep {0}X{1} did not sleep real time as expected".format(sleep, factor))

    def _assert_equals(self, a, b, msg):
        assert a == b, "{0} ({1} != {2})".format(msg, a, b)