1
0

test_asynchronous.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # coding: utf-8
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import 3rd-party libs
  5. import salt.ext.tornado.testing
  6. import salt.ext.tornado.gen
  7. from salt.ext.tornado.testing import AsyncTestCase
  8. import salt.utils.asynchronous as asynchronous
  9. class HelperA(object):
  10. def __init__(self, io_loop=None):
  11. pass
  12. @salt.ext.tornado.gen.coroutine
  13. def sleep(self):
  14. yield salt.ext.tornado.gen.sleep(0.5)
  15. raise salt.ext.tornado.gen.Return(True)
  16. class HelperB(object):
  17. def __init__(self, a=None, io_loop=None):
  18. if a is None:
  19. a = asynchronous.SyncWrapper(HelperA)
  20. self.a = a
  21. @salt.ext.tornado.gen.coroutine
  22. def sleep(self):
  23. yield salt.ext.tornado.gen.sleep(0.5)
  24. self.a.sleep()
  25. raise salt.ext.tornado.gen.Return(False)
  26. class TestSyncWrapper(AsyncTestCase):
  27. @salt.ext.tornado.testing.gen_test
  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. def test_double(self):
  46. '''
  47. Test when the asynchronous wrapper object itself creates a wrap of another thing
  48. This works fine since the second wrap is based on the first's IOLoop so we
  49. don't have to worry about complex start/stop mechanics
  50. '''
  51. sync = asynchronous.SyncWrapper(HelperB)
  52. ret = sync.sleep()
  53. self.assertFalse(ret)
  54. def test_double_sameloop(self):
  55. '''
  56. Test asynchronous wrappers initiated from the same IOLoop, to ensure that
  57. we don't wire up both to the same IOLoop (since it causes MANY problems).
  58. '''
  59. a = asynchronous.SyncWrapper(HelperA)
  60. sync = asynchronous.SyncWrapper(HelperB, (a,))
  61. ret = sync.sleep()
  62. self.assertFalse(ret)