Created
November 20, 2015 00:51
-
-
Save cablehead/8b4d2984f28a8dca8844 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import functools | |
from decorator import decorator | |
import pytest | |
from tornado import ioloop | |
from tornado import gen | |
@pytest.fixture | |
def loop(): | |
loop = ioloop.IOLoop() | |
loop.make_current() | |
return loop | |
@pytest.fixture | |
def three(loop): | |
return 3 | |
def sleep(loop, s): | |
result = gen.Future() | |
loop.add_timeout( | |
time.time()+s, lambda: result.set_result(None)) | |
return result | |
@decorator | |
def async_test(f, *args, **kwargs): | |
loop = None | |
for arg in args: | |
if isinstance(arg, ioloop.IOLoop): | |
loop = arg | |
break | |
if not loop: | |
loop = ioloop.IOLoop() | |
f = gen.coroutine(f) | |
loop.run_sync(functools.partial(f, *args, **kwargs)) | |
loop.stop() | |
@async_test | |
def test_main(loop, three): | |
print three | |
yield sleep(loop, three) | |
print "done" | |
class TestFoo(object): | |
@async_test | |
def test_bar(self, loop, three): | |
print three | |
yield sleep(loop, three) | |
print "done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment