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