summaryrefslogtreecommitdiffstats
path: root/test/test-gtimer.py
blob: 8562049a3bd90a55076584139c0f963ac8e4e5f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python3
import unittest
from staslib import gutil


class Test(unittest.TestCase):
    '''Unit tests for class GTimer'''

    def test_new_timer(self):
        tmr = gutil.GTimer(interval_sec=5)
        self.assertEqual(tmr.get_timeout(), 5)
        self.assertEqual(tmr.time_remaining(), 0)
        self.assertEqual(str(tmr), '5.0s [off]')
        tmr.set_timeout(new_interval_sec=18)
        self.assertEqual(tmr.get_timeout(), 18)
        self.assertEqual(tmr.time_remaining(), 0)

    def test_callback(self):
        tmr = gutil.GTimer(interval_sec=1, user_cback=lambda: "ok")
        self.assertEqual(tmr._callback(), "ok")
        tmr.set_callback(user_cback=lambda: "notok")
        self.assertEqual(tmr._callback(), "notok")
        tmr.kill()
        self.assertEqual(tmr._user_cback, None)
        self.assertRaises(TypeError, tmr._user_cback)

    def test_start_timer(self):
        tmr = gutil.GTimer(interval_sec=1, user_cback=lambda: "ok")
        self.assertEqual(str(tmr), '1.0s [off]')
        tmr.start()
        self.assertNotEqual(tmr.time_remaining(), 0)
        self.assertNotEqual(str(tmr), '1.0s [off]')

    def test_clear(self):
        tmr = gutil.GTimer(interval_sec=1, user_cback=lambda: "ok")
        tmr.start()
        tmr.clear()
        self.assertEqual(tmr.time_remaining(), 0)
        self.assertEqual(str(tmr), '1.0s [0s]')


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