"""test_timer
"""

import unittest

from eventlet import api
from eventlet import timer
from eventlet import runloop


class TestTimer(unittest.TestCase):
    def test_copy(self):
        t = timer.Timer(0, lambda: None)
        t2 = t.copy()
        assert t.seconds == t2.seconds
        assert t.tpl == t2.tpl
        assert t.called == t2.called

    def test_cancel(self):
        r = runloop.RunLoop()
        called = []
        t = timer.Timer(0, lambda: called.append(True))
        t.cancel()
        r.add_timer(t)
        r.add_observer(lambda r, activity: r.abort(), 'after_waiting')
        r.run()
        assert not called
        assert not r.running

    def test_schedule(self):
        hub = api.get_hub()
        r = hub.runloop
        called = []
        t = timer.Timer(0, lambda: (called.append(True), hub.runloop.abort()))
        t.schedule()
        r.default_sleep = lambda: 0.0
        r.run()
        assert called
        assert not r.running

if __name__ == '__main__':
    unittest.main()
