replacing assertEquals into assertEqual
[nepi.git] / test / util / parallel.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
21 from nepi.util.parallel import ParallelRun
22
23 import datetime
24 import unittest
25
26 class ParallelRunTestCase(unittest.TestCase):
27     def test_run_simple(self):
28         runner = ParallelRun(maxthreads = 4)
29         runner.start()
30
31         count = [0]
32
33         def inc(count):
34             count[0] += 1
35         
36         for x in range(10):
37             runner.put(inc, count)
38
39         runner.destroy()
40
41         self.assertEqual(count[0], 10)
42
43     def test_run_interrupt(self):
44
45         def sleep():
46             import time
47             time.sleep(5)
48
49         startt = datetime.datetime.now()
50
51         runner = ParallelRun(maxthreads = 4)
52         runner.start()
53        
54         for x in range(100):
55             runner.put(sleep)
56
57         runner.empty()
58         runner.destroy()
59
60         endt = datetime.datetime.now()
61         time_elapsed = (endt - startt).seconds
62         self.assertTrue( time_elapsed < 500)
63
64     def test_run_error(self):
65         count = [0]
66
67         def inc(count):
68             count[0] += 1
69  
70         def error():
71             raise RuntimeError()
72
73         runner = ParallelRun(maxthreads = 4)
74         runner.start()
75        
76         for x in range(4):
77             runner.put(inc, count)
78
79         runner.put(error)
80        
81         runner.destroy()
82       
83         self.assertEqual(count[0], 4)
84         
85         self.assertRaises(RuntimeError, runner.sync)
86
87 if __name__ == '__main__':
88     unittest.main()
89