Show More
@@ -1,88 +1,85 b'' | |||||
1 | """Tests for pylab tools module. |
|
1 | """Tests for pylab tools module. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (c) 2011, the IPython Development Team. |
|
4 | # Copyright (c) 2011, the IPython Development Team. | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the Modified BSD License. |
|
6 | # Distributed under the terms of the Modified BSD License. | |
7 | # |
|
7 | # | |
8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | # Stdlib imports |
|
15 | # Stdlib imports | |
16 | import time |
|
16 | import time | |
17 |
|
17 | |||
18 | # Third-party imports |
|
|||
19 | import nose.tools as nt |
|
|||
20 |
|
||||
21 | # Our own imports |
|
18 | # Our own imports | |
22 | from IPython.lib import backgroundjobs as bg |
|
19 | from IPython.lib import backgroundjobs as bg | |
23 |
|
20 | |||
24 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
25 | # Globals and constants |
|
22 | # Globals and constants | |
26 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
27 | t_short = 0.0001 # very short interval to wait on jobs |
|
24 | t_short = 0.0001 # very short interval to wait on jobs | |
28 |
|
25 | |||
29 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
30 | # Local utilities |
|
27 | # Local utilities | |
31 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
32 | def sleeper(interval=t_short, *a, **kw): |
|
29 | def sleeper(interval=t_short, *a, **kw): | |
33 | args = dict(interval=interval, |
|
30 | args = dict(interval=interval, | |
34 | other_args=a, |
|
31 | other_args=a, | |
35 | kw_args=kw) |
|
32 | kw_args=kw) | |
36 | time.sleep(interval) |
|
33 | time.sleep(interval) | |
37 | return args |
|
34 | return args | |
38 |
|
35 | |||
39 | def crasher(interval=t_short, *a, **kw): |
|
36 | def crasher(interval=t_short, *a, **kw): | |
40 | time.sleep(interval) |
|
37 | time.sleep(interval) | |
41 | raise Exception("Dead job with interval %s" % interval) |
|
38 | raise Exception("Dead job with interval %s" % interval) | |
42 |
|
39 | |||
43 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
44 | # Classes and functions |
|
41 | # Classes and functions | |
45 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
46 |
|
43 | |||
47 | def test_result(): |
|
44 | def test_result(): | |
48 | """Test job submission and result retrieval""" |
|
45 | """Test job submission and result retrieval""" | |
49 | jobs = bg.BackgroundJobManager() |
|
46 | jobs = bg.BackgroundJobManager() | |
50 | j = jobs.new(sleeper) |
|
47 | j = jobs.new(sleeper) | |
51 | j.join() |
|
48 | j.join() | |
52 |
|
|
49 | assert j.result["interval"] == t_short | |
53 |
|
50 | |||
54 |
|
51 | |||
55 | def test_flush(): |
|
52 | def test_flush(): | |
56 | """Test job control""" |
|
53 | """Test job control""" | |
57 | jobs = bg.BackgroundJobManager() |
|
54 | jobs = bg.BackgroundJobManager() | |
58 | j = jobs.new(sleeper) |
|
55 | j = jobs.new(sleeper) | |
59 | j.join() |
|
56 | j.join() | |
60 |
|
|
57 | assert len(jobs.completed) == 1 | |
61 |
|
|
58 | assert len(jobs.dead) == 0 | |
62 | jobs.flush() |
|
59 | jobs.flush() | |
63 |
|
|
60 | assert len(jobs.completed) == 0 | |
64 |
|
61 | |||
65 |
|
62 | |||
66 | def test_dead(): |
|
63 | def test_dead(): | |
67 | """Test control of dead jobs""" |
|
64 | """Test control of dead jobs""" | |
68 | jobs = bg.BackgroundJobManager() |
|
65 | jobs = bg.BackgroundJobManager() | |
69 | j = jobs.new(crasher) |
|
66 | j = jobs.new(crasher) | |
70 | j.join() |
|
67 | j.join() | |
71 |
|
|
68 | assert len(jobs.completed) == 0 | |
72 |
|
|
69 | assert len(jobs.dead) == 1 | |
73 | jobs.flush() |
|
70 | jobs.flush() | |
74 |
|
|
71 | assert len(jobs.dead) == 0 | |
75 |
|
72 | |||
76 |
|
73 | |||
77 | def test_longer(): |
|
74 | def test_longer(): | |
78 | """Test control of longer-running jobs""" |
|
75 | """Test control of longer-running jobs""" | |
79 | jobs = bg.BackgroundJobManager() |
|
76 | jobs = bg.BackgroundJobManager() | |
80 | # Sleep for long enough for the following two checks to still report the |
|
77 | # Sleep for long enough for the following two checks to still report the | |
81 | # job as running, but not so long that it makes the test suite noticeably |
|
78 | # job as running, but not so long that it makes the test suite noticeably | |
82 | # slower. |
|
79 | # slower. | |
83 | j = jobs.new(sleeper, 0.1) |
|
80 | j = jobs.new(sleeper, 0.1) | |
84 |
|
|
81 | assert len(jobs.running) == 1 | |
85 |
|
|
82 | assert len(jobs.completed) == 0 | |
86 | j.join() |
|
83 | j.join() | |
87 |
|
|
84 | assert len(jobs.running) == 0 | |
88 |
|
|
85 | assert len(jobs.completed) == 1 |
General Comments 0
You need to be logged in to leave comments.
Login now