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