当前位置: 首页>>代码示例>>Python>>正文


Python Engine.poll方法代码示例

本文整理汇总了Python中pants.engine.Engine.poll方法的典型用法代码示例。如果您正苦于以下问题:Python Engine.poll方法的具体用法?Python Engine.poll怎么用?Python Engine.poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pants.engine.Engine的用法示例。


在下文中一共展示了Engine.poll方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestEnginePoll

# 需要导入模块: from pants.engine import Engine [as 别名]
# 或者: from pants.engine.Engine import poll [as 别名]
class TestEnginePoll(unittest.TestCase):
    def setUp(self):
        self.engine = Engine()

    def test_poll_updates_time(self):
        current_time = self.engine.latest_poll_time
        time.sleep(0.02)
        self.engine.poll(0.02)
        self.assertTrue(self.engine.latest_poll_time > current_time)

    def test_poll_executes_callbacks(self):
        callback = MagicMock()
        callback.function = MagicMock()
        callback.requeue = False
        self.engine._callbacks.append(callback)
        self.engine.poll(0.02)
        callback.function.assert_called_once_with()
        self.assertTrue(len(self.engine._callbacks) == 0)

    def test_callback_exception_doesnt_break_poll(self):
        callback = MagicMock()
        callback.function = MagicMock(side_effect=Exception)
        callback.requeue = False
        self.engine._callbacks.append(callback)
        try:
            self.engine.poll(0.02)
        except Exception:
            self.fail("Exception in callback function was not caught.")

    def test_keyboardinterrupt_during_callback_processing_is_raised(self):
        callback = MagicMock()
        callback.function = MagicMock(side_effect=KeyboardInterrupt)
        callback.requeue = False
        self.engine._callbacks.append(callback)
        self.assertRaises(KeyboardInterrupt, self.engine.poll, 0.02)

    def test_systemexit_during_callback_processing_is_raised(self):
        callback = MagicMock()
        callback.function = MagicMock(side_effect=SystemExit)
        callback.requeue = False
        self.engine._callbacks.append(callback)
        self.assertRaises(SystemExit, self.engine.poll, 0.02)

    def test_poll_requeues_loops(self):
        loop = MagicMock()
        loop.function = MagicMock()
        loop.requeue = True
        self.engine._callbacks.append(loop)
        self.engine.poll(0.02)
        self.assertTrue(loop in self.engine._callbacks)

    def test_poll_executes_deferreds(self):
        defer = MagicMock()
        defer.function = MagicMock()
        defer.requeue = False
        defer.end = self.engine.latest_poll_time - 1
        self.engine._deferreds.append(defer)
        self.engine.poll(0.02)
        defer.function.assert_called_once_with()

    def test_deferred_exception_doesnt_break_poll(self):
        defer = MagicMock()
        defer.function = MagicMock()
        defer.requeue = False
        defer.end = self.engine.latest_poll_time - 1
        self.engine._deferreds.append(defer)
        try:
            self.engine.poll(0.02)
        except Exception:
            self.fail("Exception in deferred was not caught.")

    def test_keyboardinterrupt_during_deferred_processing_is_raised(self):
        defer = MagicMock()
        defer.function = MagicMock(side_effect=KeyboardInterrupt)
        defer.requeue = False
        defer.end = self.engine.latest_poll_time - 1
        self.engine._deferreds.append(defer)
        self.assertRaises(KeyboardInterrupt, self.engine.poll, 0.02)

    def test_systemexit_during_deferred_processing_is_raised(self):
        defer = MagicMock()
        defer.function = MagicMock(side_effect=SystemExit)
        defer.requeue = False
        defer.end = self.engine.latest_poll_time - 1
        self.engine._deferreds.append(defer)
        self.assertRaises(SystemExit, self.engine.poll, 0.02)

    def test_poll_requeues_deferreds(self):
        cycle = MagicMock()
        cycle.function = MagicMock()
        cycle.requeue = True
        cycle.end = self.engine.latest_poll_time - 1
        cycle.delay = 10
        self.engine._deferreds.append(cycle)
        self.engine.poll(0.02)
        self.assertTrue(cycle in self.engine._deferreds)

    def test_poll_returns_if_timer_shuts_down_engine(self):
        # Pretty ugly way of testing this, to be honest.
        self.engine._poller.poll = MagicMock()
#.........这里部分代码省略.........
开发者ID:larbing,项目名称:pants,代码行数:103,代码来源:test_engine.py

示例2: TestTimers

# 需要导入模块: from pants.engine import Engine [as 别名]
# 或者: from pants.engine.Engine import poll [as 别名]
class TestTimers(unittest.TestCase):
    def setUp(self):
        self.times_called = []
        self.engine = Engine()

    def timer(self):
        self.times_called.append(self.engine.latest_poll_time)

    def test_callback(self):
        timer = MagicMock()
        self.engine.callback(timer)
        self.engine.poll(0.01)
        self.engine.poll(0.01)
        self.engine.poll(0.01)
        timer.assert_called_once_with()

    def test_callback_cancel(self):
        timer = MagicMock()
        cancel_callback = self.engine.callback(timer)
        cancel_callback()
        self.engine.poll(0.01)
        self.engine.poll(0.01)
        self.engine.poll(0.01)
        self.assertRaises(AssertionError, timer.assert_called_with)

    def test_loop(self):
        timer = MagicMock()
        self.engine.loop(timer)
        self.engine.poll(0.01)
        self.engine.poll(0.01)
        self.engine.poll(0.01)
        timer.assert_has_calls([call() for _ in range(3)])

    def test_loop_cancel(self):
        timer = MagicMock()
        cancel_loop = self.engine.loop(timer)
        self.engine.poll(0.01)
        self.engine.poll(0.01)
        timer.assert_has_calls([call() for _ in range(2)])
        cancel_loop()
        self.engine.poll(0.01)
        timer.assert_has_calls([call() for _ in range(2)])

    def test_defer(self):
        self.engine.poll(0.01)
        timer = MagicMock(side_effect=self.timer)
        expected_time = self.engine.latest_poll_time + 0.01
        self.engine.defer(0.01, timer)
        self.engine.poll(0.2)
        self.engine.poll(0.2)
        self.engine.poll(0.2)
        timer.assert_called_once_with()
        self.assertLess(abs(expected_time - self.times_called[0]), 0.01)

    def test_defer_cancel(self):
        timer = MagicMock()
        cancel_defer = self.engine.defer(0.01, timer)
        cancel_defer()
        self.engine.poll(0.2)
        self.engine.poll(0.2)
        self.engine.poll(0.2)
        self.assertRaises(AssertionError, timer.assert_called_with)

    def test_cycle(self):
        self.engine.poll(0.01)
        timer = MagicMock(side_effect=self.timer)
        expected_times = [
            self.engine.latest_poll_time + 0.01,
            self.engine.latest_poll_time + 0.02,
            self.engine.latest_poll_time + 0.03
            ]
        self.engine.cycle(0.01, timer)
        self.engine.poll(0.2)
        self.engine.poll(0.2)
        self.engine.poll(0.2)
        if sys.platform == "win32": self.engine.poll(0.02)  # See issue #40
        timer.assert_has_calls([call() for _ in range(3)])
        for i in range(3):
            self.assertLess(abs(expected_times[i] - self.times_called[i]), 0.01)

    def test_cycle_cancel(self):
        self.engine.poll(0.01)
        timer = MagicMock(side_effect=self.timer)
        expected_times = [
            self.engine.latest_poll_time + 0.01,
            self.engine.latest_poll_time + 0.02
            ]
        cancel_cycle = self.engine.cycle(0.01, timer)
        self.engine.poll(0.2)
        self.engine.poll(0.2)
        if sys.platform == "win32": self.engine.poll(0.02)  # See issue #40
        timer.assert_has_calls([call() for _ in range(2)])
        cancel_cycle()
        self.engine.poll(0.2)
        timer.assert_has_calls([call() for _ in range(2)])
        for i in range(2):
            self.assertLess(abs(expected_times[i] - self.times_called[i]), 0.01)
开发者ID:Sureiya,项目名称:pants,代码行数:99,代码来源:test_timers.py


注:本文中的pants.engine.Engine.poll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。