test_asynchronous.py 2.2 KB

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