test_asynchronous.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # coding: utf-8
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import salt.ext.tornado.gen
  4. import salt.ext.tornado.testing
  5. import salt.utils.asynchronous as asynchronous
  6. from salt.ext.tornado.testing import AsyncTestCase
  7. from tests.support.helpers import slowTest
  8. class HelperA(object):
  9. def __init__(self, io_loop=None):
  10. pass
  11. @salt.ext.tornado.gen.coroutine
  12. def sleep(self):
  13. yield salt.ext.tornado.gen.sleep(0.5)
  14. raise salt.ext.tornado.gen.Return(True)
  15. class HelperB(object):
  16. def __init__(self, a=None, io_loop=None):
  17. if a is None:
  18. a = asynchronous.SyncWrapper(HelperA)
  19. self.a = a
  20. @salt.ext.tornado.gen.coroutine
  21. def sleep(self):
  22. yield salt.ext.tornado.gen.sleep(0.5)
  23. self.a.sleep()
  24. raise salt.ext.tornado.gen.Return(False)
  25. class TestSyncWrapper(AsyncTestCase):
  26. @salt.ext.tornado.testing.gen_test
  27. @slowTest
  28. def test_helpers(self):
  29. """
  30. Test that the helper classes do what we expect within a regular asynchronous env
  31. """
  32. ha = HelperA()
  33. ret = yield ha.sleep()
  34. self.assertTrue(ret)
  35. hb = HelperB()
  36. ret = yield hb.sleep()
  37. self.assertFalse(ret)
  38. def test_basic_wrap(self):
  39. """
  40. Test that we can wrap an asynchronous caller.
  41. """
  42. sync = asynchronous.SyncWrapper(HelperA)
  43. ret = sync.sleep()
  44. self.assertTrue(ret)
  45. @slowTest
  46. def test_double(self):
  47. """
  48. Test when the asynchronous wrapper object itself creates a wrap of another thing
  49. This works fine since the second wrap is based on the first's IOLoop so we
  50. don't have to worry about complex start/stop mechanics
  51. """
  52. sync = asynchronous.SyncWrapper(HelperB)
  53. ret = sync.sleep()
  54. self.assertFalse(ret)
  55. @slowTest
  56. def test_double_sameloop(self):
  57. """
  58. Test asynchronous wrappers initiated from the same IOLoop, to ensure that
  59. we don't wire up both to the same IOLoop (since it causes MANY problems).
  60. """
  61. a = asynchronous.SyncWrapper(HelperA)
  62. sync = asynchronous.SyncWrapper(HelperB, (a,))
  63. ret = sync.sleep()
  64. self.assertFalse(ret)