test_timeout.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import logging
  5. import time
  6. import pytest
  7. # Import Salt libs
  8. from salt.utils.timeout import wait_for
  9. # Import test libs
  10. from tests.support.unit import TestCase
  11. log = logging.getLogger(__name__)
  12. def return_something_after(seconds, something=True):
  13. start = time.time()
  14. end = start + seconds
  15. log.debug("Will return %s at %s", something, end)
  16. def actual():
  17. t = time.time()
  18. condition = t >= end
  19. log.debug("Return something at %s ? %s", t, condition)
  20. if condition:
  21. return something
  22. else:
  23. return False
  24. return actual
  25. def return_args_after(seconds):
  26. start = time.time()
  27. end = start + seconds
  28. def actual(*args):
  29. if time.time() >= end:
  30. return args
  31. else:
  32. return False
  33. return actual
  34. def return_kwargs_after(seconds):
  35. start = time.time()
  36. end = start + seconds
  37. def actual(**kwargs):
  38. if time.time() >= end:
  39. return kwargs
  40. else:
  41. return False
  42. return actual
  43. class WaitForTests(TestCase):
  44. def setUp(self):
  45. self.true_after_1s = return_something_after(1)
  46. self.self_after_1s = return_something_after(1, something=self)
  47. def tearDown(self):
  48. del self.true_after_1s
  49. del self.self_after_1s
  50. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  51. def test_wait_for_true(self):
  52. ret = wait_for(self.true_after_1s, timeout=2, step=0.5)
  53. self.assertTrue(ret)
  54. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  55. def test_wait_for_self(self):
  56. ret = wait_for(self.self_after_1s, timeout=2, step=0.5)
  57. self.assertEqual(ret, self)
  58. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  59. def test_wait_for_too_long(self):
  60. ret = wait_for(self.true_after_1s, timeout=0.5, step=0.1, default=False)
  61. self.assertFalse(ret)
  62. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  63. def test_wait_for_with_big_step(self):
  64. ret = wait_for(self.true_after_1s, timeout=1.5, step=2)
  65. self.assertTrue(ret)
  66. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  67. def test_wait_for_custom_args(self):
  68. args_after_1s = return_args_after(1)
  69. args = ("one", "two")
  70. ret = wait_for(args_after_1s, timeout=2, step=0.5, func_args=args)
  71. self.assertEqual(ret, args)
  72. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  73. def test_wait_for_custom_kwargs(self):
  74. kwargs_after_1s = return_kwargs_after(1)
  75. kwargs = {"one": 1, "two": 2}
  76. ret = wait_for(kwargs_after_1s, timeout=2, step=0.5, func_kwargs=kwargs)
  77. self.assertEqual(ret, kwargs)
  78. def test_return_false(self):
  79. ret = self.true_after_1s()
  80. self.assertFalse(ret)