Esempio n. 1
0
class TestTimerfdObjRestart(unittest.TestCase):
    """Docstring for TimerfdObjRestart """

    def setUp(self):
        """todo: to be defined"""
        self.deadline = 1000000
        self.interval = 1000000
        self.d_deadline = datetime.timedelta(microseconds=self.deadline)
        self.d_interval = datetime.timedelta(microseconds=self.interval)
        self.max_intervals = 5
        self.tfd = None

    # setUp()

    def test_basic_restart(self):
        """Start a timer, stop it before it's done, then restart it."""

        # Set up a timer in a normal fashion and start it.

        self.tfd = Timerfd(deadline=self.deadline, interval=self.interval)
        self.assertIsInstance(self.tfd, Timerfd)

        print("\ntest_basic_restart settime ", self.deadline, self.interval)
        self.tfd.start()

        # Sleep for a short spell
        time.sleep(0.300)

        # Stop the timer, there should be time left on it.
        ldelta, litv = self.tfd.stop()
        print("test_basic_restart stopped, ", ldelta.microseconds, litv)
        self.assertGreater(ldelta.microseconds, 0)

        # restart and run the timer until the end,
        print("test_basic_restart stopped, about to restart ", ldelta.microseconds, litv)
        self.tfd.restart()

        # it should stop at the remaining time interval.
        print("test_basic_restart waiting on expired ")
        exp, cb_res = self.tfd.expired()
        print("test_basic_restart expired ", exp, cb_res)
Esempio n. 2
0
class _Timeout(object):
    """"""

    __slots__ = ['deadline', 'callback', 'tfd', 'io_loop']

    def __init__(self, deadline, callback, io_loop):
        self.callback = callback
        self.deadline = deadline
        self.io_loop = io_loop
        self.tfd = Timerfd(deadline=deadline)
    #__init__()

    def __call__(self, fd, events):
        self.callback()

        # Remove ourselves
        self.io_loop.remove_handler(self.tfd.fd)
    #__call__()

    def start(self):
        """todo: Docstring for start
        :return:
        :rtype:
        """
        return self.tfd.start()
        self.io_loop.add_handler(self.tfd.fd, self, self.READ)
    #start()

    def remove(self):
        """todo: Docstring for remove
        :return:
        :rtype:
        """
        self.tfd.stop()
        self.io_loop.remove_handler(self.tfd.fd)
        self.tfd = None
        self.callback = None
        self.deadline = None
        self.io_loop = None
Esempio n. 3
0
class TestTimerfdObjTime(unittest.TestCase):
    """Docstring for TimerfdObjTime """

    def setUp(self):
        """todo: to be defined"""

        self.deadline = 1000000
        self.interval = 1000000
        self.d_deadline = datetime.timedelta(microseconds=self.deadline)
        self.d_interval = datetime.timedelta(microseconds=self.interval)
        self.max_intervals = 5
        self.tfd = None

    # setUp()

    def timer_sub_test(self, deadline=None, interval=0, **kwargs):
        """todo: Docstring for timer_sub_test
        :return:
        :rtype:
        """

        deadline = deadline or self.d_deadline

        self.tfd = Timerfd(deadline=deadline, interval=interval, **kwargs)
        self.assertIsInstance(self.tfd, Timerfd)

        print("\ntimer_sub_test settime ", deadline, interval)
        now = datetime.datetime.now()
        self.tfd.start()

        print("timer_sub_test reading ", self.tfd.fd)
        expires, cbr = self.tfd.expired
        self.assertIsInstance(expires, int)
        self.assertIsInstance(cbr, list)

        while expires == 0:
            expires, cbr = self.tfd.expired

        delta = datetime.datetime.now() - now
        # Throw in a little compensation for interval testing below.
        now = datetime.datetime.now() - datetime.timedelta(microseconds=100000)
        print("timer_sub_test read ", expires, " delta ", delta)

        # This is tricky, but the seconds of the delta 'should' be the same
        # as deadline seconds.
        self.assertEqual(self.d_deadline.seconds, delta.seconds)

        c_interval = 0
        if interval:
            print("Testing interval", interval)
            print("\ntimer_sub_test settime with interval ", deadline, interval)
            while c_interval < self.max_intervals:
                print("current interval", c_interval)
                print("timer_sub_test reading ", self.tfd.fd)
                expires, cbr = self.tfd.expired
                while expires == 0:
                    expires, cbr = self.tfd.expired
                c_interval += expires

                delta = datetime.datetime.now() - now
                print("timer_sub_test read interval ", expires, " delta ", delta)
                # Throw in a little compensation for interval testing below.
                now = datetime.datetime.now() - datetime.timedelta(microseconds=100000)

                # This is tricky, but the seconds of the delta 'should' be the same
                # as deadline seconds.
                self.assertEqual(self.d_interval.seconds, delta.seconds)

        self.tfd.stop()

    # timer_sub_test()

    def timer_test(self, *args, **kwargs):
        """A generic timer test.

        :param *args: arg description
        :type *args: type description
        :param **kwargs: arg description
        :type **kwargs: type description
        :return:
        :rtype:
        """
        self.timer_sub_test(self.deadline, *args, **kwargs)
        self.timer_sub_test(self.deadline, self.interval, *args, **kwargs)

        self.timer_sub_test(self.d_deadline, *args, **kwargs)
        self.timer_sub_test(self.d_deadline, self.d_interval, *args, **kwargs)

    # timer_test()

    def test_default_timer(self):
        """todo: Basic tests for a blocking timer."""
        self.timer_test(monotonic=True)
        self.timer_test(realtime=True)

    # test_default_timer()

    def test_async_timer(self):
        """todo: Basic tests for a non-blocking timer."""
        self.timer_test(monotonic=True, async=True)
        self.timer_test(realtime=True, async=True)