033a750a30aa24cd894a8204d53907472f10e6b9
[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 class SchedulerTestCase(unittest.TestCase):
26     def test_task_order(self):
27         def first():
28             return 1
29
30         def second():
31             return 2
32
33         def third():
34             return 3
35
36         scheduler = HeapScheduler()
37         
38         t1 = tnow()
39         t2 = stabsformat("2s")
40         t3 = stabsformat("3s")
41     
42         tsk1 = Task(t1, first)
43         tsk2 = Task(t2, second)
44         tsk3 = Task(t3, third)
45
46         # schedule the tasks in disorder
47         scheduler.schedule(tsk2)
48         scheduler.schedule(tsk3)
49         scheduler.schedule(tsk1)
50
51         # Make sure tasks are retrieved in teh correct order
52         tsk = scheduler.next()
53         self.assertEquals(tsk.callback(), 1)
54         
55         tsk = scheduler.next()
56         self.assertEquals(tsk.callback(), 2)
57         
58         tsk = scheduler.next()
59         self.assertEquals(tsk.callback(), 3)
60
61
62 if __name__ == '__main__':
63     unittest.main()
64