the big merge
[nepi.git] / test / execution / scheduler.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License version 2 as
8 #    published by the Free Software Foundation;
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.scheduler import HeapScheduler, Task, TaskStatus
21 from nepi.util.timefuncs import tnow, stabsformat
22
23 import unittest
24
25 from six import next
26
27 class SchedulerTestCase(unittest.TestCase):
28     def test_task_order(self):
29         def first():
30             return 1
31
32         def second():
33             return 2
34
35         def third():
36             return 3
37
38         scheduler = HeapScheduler()
39         
40         t1 = tnow()
41         t2 = stabsformat("2s")
42         t3 = stabsformat("3s")
43     
44         tsk1 = Task(t1, first)
45         tsk2 = Task(t2, second)
46         tsk3 = Task(t3, third)
47
48         # schedule the tasks in disorder
49         scheduler.schedule(tsk2)
50         scheduler.schedule(tsk3)
51         scheduler.schedule(tsk1)
52
53         # Make sure tasks are retrieved in teh correct order
54         tsk = next(scheduler)
55         self.assertEqual(tsk.callback(), 1)
56         
57         tsk = next(scheduler)
58         self.assertEqual(tsk.callback(), 2)
59         
60         tsk = next(scheduler)
61         self.assertEqual(tsk.callback(), 3)
62
63
64 if __name__ == '__main__':
65     unittest.main()
66