Beispiel #1
0
    def test_cancel_grace_period(self):
        st = timeutils.wallclock()
        task = DummyTask(5)

        self.m.StubOutWithMock(task, 'do_step')
        self.m.StubOutWithMock(scheduler.TaskRunner, '_sleep')
        self.m.StubOutWithMock(timeutils, 'wallclock')

        task.do_step(1).AndReturn(None)
        task.do_step(2).AndReturn(None)
        timeutils.wallclock().AndReturn(st)
        timeutils.wallclock().AndReturn(st + 0.5)
        task.do_step(3).AndReturn(None)
        timeutils.wallclock().AndReturn(st + 1.0)
        task.do_step(4).AndReturn(None)
        timeutils.wallclock().AndReturn(st + 1.5)

        self.m.ReplayAll()

        runner = scheduler.TaskRunner(task)

        self.assertFalse(runner.started())
        runner.start()
        self.assertTrue(runner.started())

        self.assertFalse(runner.step())
        runner.cancel(grace_period=1.0)
        self.assertFalse(runner.step())
        self.assertFalse(runner.step())
        self.assertTrue(runner.step())
Beispiel #2
0
    def test_timeout_swallowed(self):
        st = timeutils.wallclock()

        def task():
            while True:
                try:
                    yield
                except scheduler.Timeout:
                    yield
                    self.fail('Task still running')

        self.m.StubOutWithMock(timeutils, 'wallclock')
        timeutils.wallclock().AndReturn(st)
        timeutils.wallclock().AndReturn(st + 0.5)
        timeutils.wallclock().AndReturn(st + 1.5)

        self.m.ReplayAll()

        runner = scheduler.TaskRunner(task)

        runner.start(timeout=1)
        self.assertTrue(runner)
        self.assertTrue(runner.step())
        self.assertFalse(runner)
        self.assertTrue(runner.step())
Beispiel #3
0
    def test_timeout(self):
        st = timeutils.wallclock()

        def task():
            while True:
                yield

        self.m.StubOutWithMock(timeutils, 'wallclock')
        timeutils.wallclock().AndReturn(st)
        timeutils.wallclock().AndReturn(st + 0.5)
        timeutils.wallclock().AndReturn(st + 1.5)

        self.m.ReplayAll()

        runner = scheduler.TaskRunner(task)

        runner.start(timeout=1)
        self.assertTrue(runner)
        self.assertRaises(scheduler.Timeout, runner.step)
    def test_timeout_return(self):
        st = timeutils.wallclock()

        def task():
            while True:
                try:
                    yield
                except scheduler.Timeout:
                    return

        self.m.StubOutWithMock(timeutils, "wallclock")
        timeutils.wallclock().AndReturn(st)
        timeutils.wallclock().AndReturn(st + 0.5)
        timeutils.wallclock().AndReturn(st + 1.5)

        self.m.ReplayAll()

        runner = scheduler.TaskRunner(task)

        runner.start(timeout=1)
        self.assertTrue(runner)
        self.assertTrue(runner.step())
        self.assertFalse(runner)
    def test_cancel_grace_period_after_timeout(self):
        st = timeutils.wallclock()
        task = DummyTask(5)

        self.m.StubOutWithMock(task, "do_step")
        self.m.StubOutWithMock(scheduler.TaskRunner, "_sleep")
        self.m.StubOutWithMock(timeutils, "wallclock")

        timeutils.wallclock().AndReturn(st)
        timeutils.wallclock().AndReturn(st + 0.1)
        task.do_step(1).AndReturn(None)
        timeutils.wallclock().AndReturn(st + 0.2)
        task.do_step(2).AndReturn(None)
        timeutils.wallclock().AndReturn(st + 0.2)
        timeutils.wallclock().AndReturn(st + 0.5)
        task.do_step(3).AndReturn(None)
        timeutils.wallclock().AndReturn(st + 1.0)
        task.do_step(4).AndReturn(None)
        timeutils.wallclock().AndReturn(st + 1.5)

        self.m.ReplayAll()

        runner = scheduler.TaskRunner(task)

        self.assertFalse(runner.started())
        runner.start(timeout=1.25)
        self.assertTrue(runner.started())

        self.assertFalse(runner.step())
        runner.cancel(grace_period=3)
        self.assertFalse(runner.step())
        self.assertFalse(runner.step())
        self.assertRaises(scheduler.Timeout, runner.step)
Beispiel #6
0
 def setUp(self):
     super(DurationTest, self).setUp()
     st = util.wallclock()
     mock_clock = self.patchobject(util, 'wallclock')
     mock_clock.side_effect = [st, st + 0.5]