def test_interval_adjustment(self):
        """Ensure the interval is adjusted to account for task duration"""
        self.num_runs = 3

        now = datetime.datetime.utcnow()
        second = datetime.timedelta(seconds=1)
        smidgen = datetime.timedelta(microseconds=10000)
        timeoverrides = [now, now + second - smidgen,
                         now, now + second + second,
                         now, now + second + smidgen]

        m = mox.Mox()
        m.StubOutWithMock(greenthread, 'sleep')
        greenthread.sleep(mox.IsAlmost(0.02))
        greenthread.sleep(mox.IsAlmost(0.0))
        greenthread.sleep(mox.IsAlmost(0.0))
        m.ReplayAll()

        try:
            timeutils.set_time_override(timeoverrides)
            timer = loopingcall.LoopingCall(self._wait_for_zero)
            timer.start(interval=1.01).wait()
        finally:
            timeutils.clear_time_override()
            m.UnsetStubs()
            m.VerifyAll()
Exemplo n.º 2
0
    def test_periodic_tasks_idle_calculation(self):
        fake_time = datetime.datetime(3000, 1, 1)
        timeutils.set_time_override(fake_time)

        class Manager(periodic_task.PeriodicTasks):
            __metaclass__ = periodic_task._PeriodicTasksMeta

            @periodic_task.periodic_task(spacing=10)
            def bar(self, context):
                return 'bar'

        m = Manager()

        # Ensure initial values are correct
        self.assertEqual(1, len(m._periodic_tasks))
        task_name, task = m._periodic_tasks[0]

        # Test task values
        self.assertEqual('bar', task_name)
        self.assertEqual(10, task._periodic_spacing)
        self.assertEqual(True, task._periodic_enabled)
        self.assertEqual(False, task._periodic_external_ok)
        self.assertEqual(False, task._periodic_immediate)
        self.assertNotEqual(None, task._periodic_last_run)

        # Test the manager's representation of those values
        self.assertEqual(10, m._periodic_spacing[task_name])
        self.assertNotEqual(None, m._periodic_last_run[task_name])

        timeutils.advance_time_delta(datetime.timedelta(seconds=5))
        m.run_periodic_tasks(None)

        timeutils.advance_time_delta(datetime.timedelta(seconds=5))
        idle = m.run_periodic_tasks(None)
        self.assertAlmostEqual(10, idle, 1)
Exemplo n.º 3
0
    def test_interval_adjustment(self):
        """Ensure the interval is adjusted to account for task duration"""
        self.num_runs = 3

        now = datetime.datetime.utcnow()
        second = datetime.timedelta(seconds=1)
        smidgen = datetime.timedelta(microseconds=10000)
        timeoverrides = [
            now, now + second - smidgen, now, now + second + second, now,
            now + second + smidgen
        ]

        m = mox.Mox()
        m.StubOutWithMock(greenthread, 'sleep')
        greenthread.sleep(mox.IsAlmost(0.02))
        greenthread.sleep(mox.IsAlmost(0.0))
        greenthread.sleep(mox.IsAlmost(0.0))
        m.ReplayAll()

        try:
            timeutils.set_time_override(timeoverrides)
            timer = loopingcall.LoopingCall(self._wait_for_zero)
            timer.start(interval=1.01).wait()
        finally:
            timeutils.clear_time_override()
            m.UnsetStubs()
            m.VerifyAll()
Exemplo n.º 4
0
    def test_utcnow(self):
        timeutils.set_time_override(mock.sentinel.utcnow)
        self.assertEqual(timeutils.utcnow(), mock.sentinel.utcnow)

        timeutils.clear_time_override()
        self.assertFalse(timeutils.utcnow() == mock.sentinel.utcnow)

        self.assertTrue(timeutils.utcnow())
Exemplo n.º 5
0
    def test_utcnow(self):
        timeutils.set_time_override(mock.sentinel.utcnow)
        self.assertEqual(timeutils.utcnow(), mock.sentinel.utcnow)

        timeutils.clear_time_override()
        self.assertFalse(timeutils.utcnow() == mock.sentinel.utcnow)

        self.assertTrue(timeutils.utcnow())
Exemplo n.º 6
0
    def test_set_time_override_using_default(self):
        now = timeutils.utcnow_ts()

        # NOTE(kgriffs): Normally it's bad form to sleep in a unit test,
        # but this is the only way to test that set_time_override defaults
        # to setting the override to the current time.
        time.sleep(1)

        timeutils.set_time_override()
        overriden_now = timeutils.utcnow_ts()
        self.assertThat(now, matchers.LessThan(overriden_now))
Exemplo n.º 7
0
    def test_set_time_override_using_default(self):
        now = timeutils.utcnow_ts()

        # NOTE(kgriffs): Normally it's bad form to sleep in a unit test,
        # but this is the only way to test that set_time_override defaults
        # to setting the override to the current time.
        time.sleep(1)

        timeutils.set_time_override()
        overriden_now = timeutils.utcnow_ts()
        self.assertThat(now, matchers.LessThan(overriden_now))
Exemplo n.º 8
0
    def test_utcnow_ts(self):
        skynet_self_aware_ts = 872835240
        skynet_dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_ts)
        self.assertEqual(self.skynet_self_aware_time, skynet_dt)

        # NOTE(kgriffs): timeutils.utcnow_ts() uses time.time()
        # IFF time override is not set.
        with mock.patch('time.time') as time_mock:
            time_mock.return_value = skynet_self_aware_ts
            ts = timeutils.utcnow_ts()
            self.assertEqual(ts, skynet_self_aware_ts)

        timeutils.set_time_override(skynet_dt)
        ts = timeutils.utcnow_ts()
        self.assertEqual(ts, skynet_self_aware_ts)
Exemplo n.º 9
0
    def test_utcnow_ts(self):
        skynet_self_aware_ts = 872835240
        skynet_dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_ts)
        self.assertEqual(self.skynet_self_aware_time, skynet_dt)

        # NOTE(kgriffs): timeutils.utcnow_ts() uses time.time()
        # IFF time override is not set.
        with mock.patch('time.time') as time_mock:
            time_mock.return_value = skynet_self_aware_ts
            ts = timeutils.utcnow_ts()
            self.assertEqual(ts, skynet_self_aware_ts)

        timeutils.set_time_override(skynet_dt)
        ts = timeutils.utcnow_ts()
        self.assertEqual(ts, skynet_self_aware_ts)
Exemplo n.º 10
0
 def test_timeout(self):
     try:
         now = datetime.datetime.utcnow()
         timeutils.set_time_override(now)
         self.client.set('foo', 'bar', time=3)
         timeutils.set_time_override(now + datetime.timedelta(seconds=1))
         self.assertEqual(self.client.get('foo'), 'bar')
         timeutils.set_time_override(now + datetime.timedelta(seconds=3))
         self.assertEqual(self.client.get('foo'), None)
     finally:
         timeutils.clear_time_override()
Exemplo n.º 11
0
 def test_advance_time_seconds(self):
     timeutils.set_time_override(self.one_minute_before)
     timeutils.advance_time_seconds(60)
     self.assertEqual(timeutils.utcnow(), self.skynet_self_aware_time)
Exemplo n.º 12
0
 def test_advance_time_seconds(self):
     timeutils.set_time_override(self.one_minute_before)
     timeutils.advance_time_seconds(60)
     self.assertEqual(timeutils.utcnow(), self.skynet_self_aware_time)