test_backgroundjobs.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. # Imports
  12. #-----------------------------------------------------------------------------
  13. from __future__ import print_function
  14. # Stdlib imports
  15. import time
  16. # Third-party imports
  17. import nose.tools as nt
  18. # Our own imports
  19. from IPython.lib import backgroundjobs as bg
  20. #-----------------------------------------------------------------------------
  21. # Globals and constants
  22. #-----------------------------------------------------------------------------
  23. t_short = 0.0001 # very short interval to wait on jobs
  24. #-----------------------------------------------------------------------------
  25. # Local utilities
  26. #-----------------------------------------------------------------------------
  27. def sleeper(interval=t_short, *a, **kw):
  28. args = dict(interval=interval,
  29. other_args=a,
  30. kw_args=kw)
  31. time.sleep(interval)
  32. return args
  33. def crasher(interval=t_short, *a, **kw):
  34. time.sleep(interval)
  35. raise Exception("Dead job with interval %s" % interval)
  36. #-----------------------------------------------------------------------------
  37. # Classes and functions
  38. #-----------------------------------------------------------------------------
  39. def test_result():
  40. """Test job submission and result retrieval"""
  41. jobs = bg.BackgroundJobManager()
  42. j = jobs.new(sleeper)
  43. j.join()
  44. nt.assert_equal(j.result['interval'], t_short)
  45. def test_flush():
  46. """Test job control"""
  47. jobs = bg.BackgroundJobManager()
  48. j = jobs.new(sleeper)
  49. j.join()
  50. nt.assert_equal(len(jobs.completed), 1)
  51. nt.assert_equal(len(jobs.dead), 0)
  52. jobs.flush()
  53. nt.assert_equal(len(jobs.completed), 0)
  54. def test_dead():
  55. """Test control of dead jobs"""
  56. jobs = bg.BackgroundJobManager()
  57. j = jobs.new(crasher)
  58. j.join()
  59. nt.assert_equal(len(jobs.completed), 0)
  60. nt.assert_equal(len(jobs.dead), 1)
  61. jobs.flush()
  62. nt.assert_equal(len(jobs.dead), 0)
  63. def test_longer():
  64. """Test control of longer-running jobs"""
  65. jobs = bg.BackgroundJobManager()
  66. # Sleep for long enough for the following two checks to still report the
  67. # job as running, but not so long that it makes the test suite noticeably
  68. # slower.
  69. j = jobs.new(sleeper, 0.1)
  70. nt.assert_equal(len(jobs.running), 1)
  71. nt.assert_equal(len(jobs.completed), 0)
  72. j.join()
  73. nt.assert_equal(len(jobs.running), 0)
  74. nt.assert_equal(len(jobs.completed), 1)