2015-07-29 15:43:10 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
# Import Python Libs
|
2018-01-12 21:03:04 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-07-29 15:43:10 +00:00
|
|
|
|
|
|
|
# Import 3rd-party libs
|
|
|
|
import tornado.testing
|
|
|
|
import tornado.gen
|
|
|
|
from tornado.testing import AsyncTestCase
|
|
|
|
|
2018-07-23 09:05:22 +00:00
|
|
|
import salt.utils.asynchronous as asynchronous
|
2015-07-29 15:43:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HelperA(object):
|
|
|
|
def __init__(self, io_loop=None):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@tornado.gen.coroutine
|
|
|
|
def sleep(self):
|
|
|
|
yield tornado.gen.sleep(0.5)
|
|
|
|
raise tornado.gen.Return(True)
|
|
|
|
|
|
|
|
|
|
|
|
class HelperB(object):
|
|
|
|
def __init__(self, a=None, io_loop=None):
|
|
|
|
if a is None:
|
2018-07-23 09:05:22 +00:00
|
|
|
a = asynchronous.SyncWrapper(HelperA)
|
2015-07-29 15:43:10 +00:00
|
|
|
self.a = a
|
|
|
|
|
|
|
|
@tornado.gen.coroutine
|
|
|
|
def sleep(self):
|
|
|
|
yield tornado.gen.sleep(0.5)
|
|
|
|
self.a.sleep()
|
|
|
|
raise tornado.gen.Return(False)
|
|
|
|
|
|
|
|
|
|
|
|
class TestSyncWrapper(AsyncTestCase):
|
|
|
|
@tornado.testing.gen_test
|
|
|
|
def test_helpers(self):
|
|
|
|
'''
|
2018-07-23 09:05:22 +00:00
|
|
|
Test that the helper classes do what we expect within a regular asynchronous env
|
2015-07-29 15:43:10 +00:00
|
|
|
'''
|
|
|
|
ha = HelperA()
|
|
|
|
ret = yield ha.sleep()
|
|
|
|
self.assertTrue(ret)
|
|
|
|
|
|
|
|
hb = HelperB()
|
|
|
|
ret = yield hb.sleep()
|
|
|
|
self.assertFalse(ret)
|
|
|
|
|
|
|
|
def test_basic_wrap(self):
|
|
|
|
'''
|
2018-07-23 09:05:22 +00:00
|
|
|
Test that we can wrap an asynchronous caller.
|
2015-07-29 15:43:10 +00:00
|
|
|
'''
|
2018-07-23 09:05:22 +00:00
|
|
|
sync = asynchronous.SyncWrapper(HelperA)
|
2015-07-29 15:43:10 +00:00
|
|
|
ret = sync.sleep()
|
|
|
|
self.assertTrue(ret)
|
|
|
|
|
|
|
|
def test_double(self):
|
|
|
|
'''
|
2018-07-23 09:05:22 +00:00
|
|
|
Test when the asynchronous wrapper object itself creates a wrap of another thing
|
2015-07-29 15:43:10 +00:00
|
|
|
|
|
|
|
This works fine since the second wrap is based on the first's IOLoop so we
|
|
|
|
don't have to worry about complex start/stop mechanics
|
|
|
|
'''
|
2018-07-23 09:05:22 +00:00
|
|
|
sync = asynchronous.SyncWrapper(HelperB)
|
2015-07-29 15:43:10 +00:00
|
|
|
ret = sync.sleep()
|
|
|
|
self.assertFalse(ret)
|
|
|
|
|
|
|
|
def test_double_sameloop(self):
|
|
|
|
'''
|
2018-07-23 09:05:22 +00:00
|
|
|
Test asynchronous wrappers initiated from the same IOLoop, to ensure that
|
2015-07-29 15:43:10 +00:00
|
|
|
we don't wire up both to the same IOLoop (since it causes MANY problems).
|
|
|
|
'''
|
2018-07-23 09:05:22 +00:00
|
|
|
a = asynchronous.SyncWrapper(HelperA)
|
|
|
|
sync = asynchronous.SyncWrapper(HelperB, (a,))
|
2015-07-29 15:43:10 +00:00
|
|
|
ret = sync.sleep()
|
|
|
|
self.assertFalse(ret)
|