summaryrefslogtreecommitdiff
path: root/tests/test_timer.py
blob: fbb492b5663731c9d55610a443ce70773517b9ec (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
44
45
46
47
48
import datetime
import eventlet
import tests

class TimerTests(tests.TestCase):
    def test_display_date(self):
        result = []
        delay = 0.050
        count = 3

        def display_date(end_time, loop):
            result.append(datetime.datetime.now())
            if (loop.time() + delay) < end_time:
                loop.call_later(delay, display_date, end_time, loop)
            else:
                loop.stop()

        end_time = self.loop.time() + delay * count
        self.loop.call_soon(display_date, end_time, self.loop)
        self.loop.run_forever()

        self.assertEqual(len(result), count, result)

    def test_later_stop_later(self):
        result = []

        def hello():
            result.append("Hello")

        def world(loop):
            result.append("World")
            loop.stop()

        self.loop.call_later(0.050, hello)
        self.loop.call_later(0.100, self.loop.stop)
        self.loop.call_later(0.150, world, self.loop)
        self.loop.run_forever()

        eventlet.sleep(0.200)
        self.assertEqual(result, ["Hello"])

        self.loop.run_forever()
        self.assertEqual(result, ["Hello", "World"])


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