Ejemplo n.º 1
0
    def test__run_once_logging(self, m_logger, m_time):
        # Log to INFO level if timeout > 1.0 sec.
        idx = -1
        data = [10.0, 10.0, 12.0, 13.0]

        def monotonic():
            nonlocal data, idx
            idx += 1
            return data[idx]

        m_time.monotonic = monotonic

        self.loop._scheduled.append(
            asyncio.TimerHandle(11.0, lambda: True, (), self.loop))
        self.loop._process_events = mock.Mock()
        self.loop._run_once()
        self.assertEqual(logging.INFO, m_logger.log.call_args[0][0])

        idx = -1
        data = [10.0, 10.0, 10.3, 13.0]
        self.loop._scheduled = [
            asyncio.TimerHandle(11.0, lambda: True, (), self.loop)
        ]
        self.loop._run_once()
        self.assertEqual(logging.DEBUG, m_logger.log.call_args[0][0])
Ejemplo n.º 2
0
    def call_at(self, when, callback, *args, **context):
        """asyncio's time-based delay

        Note that the callback is a sync function.
        """
        self._check_callback(callback, 'call_at')
        return self._queue_handle(asyncio.TimerHandle(when, callback, args, self, **context))
Ejemplo n.º 3
0
 def call_at(self, when, callback, *args):
     if when < self._time:
         raise Exception("Can't schedule in the past")
     h = asyncio.TimerHandle(when, callback, args, self)
     heapq.heappush(self._scheduled, h)
     h._scheduled = True
     return h
Ejemplo n.º 4
0
    def test__run_once(self):
        h1 = asyncio.TimerHandle(time.monotonic() + 5.0, lambda: True, (),
                                 self.loop)
        h2 = asyncio.TimerHandle(time.monotonic() + 10.0, lambda: True, (),
                                 self.loop)

        h1.cancel()

        self.loop._process_events = mock.Mock()
        self.loop._scheduled.append(h1)
        self.loop._scheduled.append(h2)
        self.loop._run_once()

        t = self.loop._selector.select.call_args[0][0]
        self.assertTrue(9.5 < t < 10.5, t)
        self.assertEqual([h2], self.loop._scheduled)
        self.assertTrue(self.loop._process_events.called)
Ejemplo n.º 5
0
    def test_run_once_in_executor_handle(self):
        def cb():
            pass

        self.assertRaises(AssertionError, self.loop.run_in_executor, None,
                          asyncio.Handle(cb, (), self.loop), ('', ))
        self.assertRaises(AssertionError, self.loop.run_in_executor, None,
                          asyncio.TimerHandle(10, cb, (), self.loop))
Ejemplo n.º 6
0
 def call_at(self, when: float, callback: Callable, *args: Any,
             **kwargs: Any) -> asyncio.TimerHandle:
     th = asyncio.TimerHandle(when,
                              callback,
                              list(args),
                              loop=self,
                              **kwargs)
     heapq.heappush(self._tasks, th)
     return th
Ejemplo n.º 7
0
    def call_later(self, delay, callback, *args, **context):
        """asyncio's timer-based delay

        Note that the callback is a sync function.

        :param delay: Time to wait, in seconds.
        :param callback: Sync function to call.
        :return: a handle which may be used to cancel the timer.
        """
        self._check_callback(callback, 'call_later')
        assert delay >= 0, delay
        h = asyncio.TimerHandle(delay + self.time(), callback, args, self, **context)
        self._queue_handle(h)
        return h
Ejemplo n.º 8
0
    def test__run_once_schedule_handle(self):
        handle = None
        processed = False

        def cb(loop):
            nonlocal processed, handle
            processed = True
            handle = loop.call_soon(lambda: True)

        h = asyncio.TimerHandle(time.monotonic() - 1, cb, (self.loop, ),
                                self.loop)

        self.loop._process_events = mock.Mock()
        self.loop._scheduled.append(h)
        self.loop._run_once()

        self.assertTrue(processed)
        self.assertEqual([handle], list(self.loop._ready))
Ejemplo n.º 9
0
    def call_at(self, when, callback, *args, context=None):
        cfwhen = posix2cftime(when)
        if self._timer is None:
            self._timer = CFRunLoopTimerCreateWithHandler(
                None, cfwhen, 1000.0, 0, 0,
                lambda timer: self._process_timer())
            CFRunLoopAddTimer(self._loop, self._timer, kCFRunLoopCommonModes)

        handle = asyncio.TimerHandle(when,
                                     callback,
                                     args,
                                     self,
                                     context=context)
        heapq.heappush(self._timer_q, handle)

        if CFRunLoopTimerGetNextFireDate(self._timer) > posix2cftime(
                self._timer_q[0].when()):
            CFRunLoopTimerSetNextFireDate(
                self._timer, posix2cftime(self._timer_q[0].when()))

        return handle
Ejemplo n.º 10
0
    def test__add_callback_timer(self):
        h = asyncio.TimerHandle(time.monotonic() + 10, lambda: False, (),
                                self.loop)

        self.loop._add_callback(h)
        self.assertIn(h, self.loop._scheduled)
Ejemplo n.º 11
0
 def create_callback(when, callback, args, loop, context):
     return asyncio.TimerHandle(when, callback, args, loop, context=context)
Ejemplo n.º 12
0
 def create_callback(when, callback, args, loop, _):
     return asyncio.TimerHandle(when, callback, args, loop)
Ejemplo n.º 13
0
 def call_at(self, when, callback, *args):
     handler = asyncio.TimerHandle(when, callback, args, self)
     self._scheduled.append(handler)
     return handler
Ejemplo n.º 14
0
 def call_at(self, when:float, callback:Callable[...,Any], *args:Any, context:Optional[Context]=None) -> asyncio.TimerHandle:
     th = asyncio.TimerHandle(when, callback, list(args), self, context) # type:ignore
     heapq.heappush(self._tasks, th)
     return th