Giampaolo RodolĂ | b5c2376 | 2010-08-04 09:28:05 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sched |
| 4 | import time |
| 5 | import unittest |
| 6 | from test import support |
| 7 | |
| 8 | |
| 9 | class TestCase(unittest.TestCase): |
| 10 | |
| 11 | def test_enter(self): |
| 12 | l = [] |
| 13 | fun = lambda x: l.append(x) |
| 14 | scheduler = sched.scheduler(time.time, time.sleep) |
| 15 | for x in [0.05, 0.04, 0.03, 0.02, 0.01]: |
| 16 | z = scheduler.enter(x, 1, fun, (x,)) |
| 17 | scheduler.run() |
| 18 | self.assertEqual(l, [0.01, 0.02, 0.03, 0.04, 0.05]) |
| 19 | |
| 20 | def test_enterabs(self): |
| 21 | l = [] |
| 22 | fun = lambda x: l.append(x) |
| 23 | scheduler = sched.scheduler(time.time, time.sleep) |
| 24 | for x in [0.05, 0.04, 0.03, 0.02, 0.01]: |
| 25 | z = scheduler.enterabs(x, 1, fun, (x,)) |
| 26 | scheduler.run() |
| 27 | self.assertEqual(l, [0.01, 0.02, 0.03, 0.04, 0.05]) |
| 28 | |
| 29 | def test_priority(self): |
| 30 | l = [] |
| 31 | fun = lambda x: l.append(x) |
| 32 | scheduler = sched.scheduler(time.time, time.sleep) |
| 33 | for priority in [1, 2, 3, 4, 5]: |
| 34 | z = scheduler.enter(0.01, priority, fun, (priority,)) |
| 35 | scheduler.run() |
| 36 | self.assertEqual(l, [1, 2, 3, 4, 5]) |
| 37 | |
| 38 | def test_cancel(self): |
| 39 | l = [] |
| 40 | fun = lambda x: l.append(x) |
| 41 | scheduler = sched.scheduler(time.time, time.sleep) |
| 42 | event1 = scheduler.enter(0.01, 1, fun, (0.01,)) |
| 43 | event2 = scheduler.enter(0.02, 1, fun, (0.02,)) |
| 44 | event3 = scheduler.enter(0.03, 1, fun, (0.03,)) |
| 45 | event4 = scheduler.enter(0.04, 1, fun, (0.04,)) |
| 46 | event5 = scheduler.enter(0.05, 1, fun, (0.05,)) |
| 47 | scheduler.cancel(event1) |
| 48 | scheduler.cancel(event5) |
| 49 | scheduler.run() |
| 50 | self.assertEqual(l, [0.02, 0.03, 0.04]) |
| 51 | |
| 52 | def test_empty(self): |
| 53 | l = [] |
| 54 | fun = lambda x: l.append(x) |
| 55 | scheduler = sched.scheduler(time.time, time.sleep) |
| 56 | self.assertTrue(scheduler.empty()) |
| 57 | for x in [0.05, 0.04, 0.03, 0.02, 0.01]: |
| 58 | z = scheduler.enterabs(x, 1, fun, (x,)) |
| 59 | self.assertFalse(scheduler.empty()) |
| 60 | scheduler.run() |
| 61 | self.assertTrue(scheduler.empty()) |
| 62 | |
| 63 | def test_queue(self): |
| 64 | l = [] |
| 65 | events = [] |
| 66 | fun = lambda x: l.append(x) |
| 67 | scheduler = sched.scheduler(time.time, time.sleep) |
| 68 | self.assertEqual(scheduler._queue, []) |
| 69 | for x in [0.05, 0.04, 0.03, 0.02, 0.01]: |
| 70 | events.append(scheduler.enterabs(x, 1, fun, (x,))) |
| 71 | self.assertEqual(scheduler._queue.sort(), events.sort()) |
| 72 | scheduler.run() |
| 73 | self.assertEqual(scheduler._queue, []) |
| 74 | |
| 75 | |
| 76 | def test_main(): |
| 77 | support.run_unittest(TestCase) |
| 78 | |
| 79 | if __name__ == "__main__": |
| 80 | test_main() |