# Path: paco/defer.py
# @decorate
# def defer(coro, delay=1):
#     """
#     Returns a coroutine function wrapper that will defer the given coroutine
#     execution for a certain amount of seconds in a non-blocking way.
# 
#     This function can be used as decorator.
# 
#     Arguments:
#         coro (coroutinefunction): coroutine function to defer.
#         delay (int/float): number of seconds to defer execution.
# 
#     Raises:
#         TypeError: if coro argument is not a coroutine function.
# 
#     Returns:
#         filtered values (list): ordered list of resultant values.
# 
#     Usage::
# 
#         # Usage as function
#         await paco.defer(coro, delay=1)
#         await paco.defer(coro, delay=0.5)
# 
#         # Usage as decorator
#         @paco.defer(delay=1)
#         async def mul_2(num):
#             return num * 2
# 
#         await mul_2(2)
#         # => 4
# 
#     """
#     assert_corofunction(coro=coro)
# 
#     @asyncio.coroutine
#     def wrapper(*args, **kw):
#         # Wait until we're done
#         yield from asyncio.sleep(delay)
#         return (yield from coro(*args, **kw))
# 
#     return wrapper
# 
# Path: tests/helpers.py
# def run_in_loop(coro, *args, **kw):
#     loop = asyncio.get_event_loop()
#     if asyncio.iscoroutinefunction(coro) or isfunction(coro):
#         coro = coro(*args, **kw)
#     return loop.run_until_complete(coro)


import time
import asyncio
from paco import defer
from .helpers import run_in_loop
# -*- coding: utf-8 -*-


@asyncio.coroutine
def coro(x):
    return x


def test_defer():
    task = defer(coro, delay=0.2)
    now = time.time()
    assert run_in_loop(task, 1) == 1
    assert time.time() - now >= 0.2


def test_defer_decorator():
    task = defer(delay=0.2)(coro)
    now = time.time()

# Path: paco/defer.py
# @decorate
# def defer(coro, delay=1):
#     """
#     Returns a coroutine function wrapper that will defer the given coroutine
#     execution for a certain amount of seconds in a non-blocking way.
# 
#     This function can be used as decorator.
# 
#     Arguments:
#         coro (coroutinefunction): coroutine function to defer.
#         delay (int/float): number of seconds to defer execution.
# 
#     Raises:
#         TypeError: if coro argument is not a coroutine function.
# 
#     Returns:
#         filtered values (list): ordered list of resultant values.
# 
#     Usage::
# 
#         # Usage as function
#         await paco.defer(coro, delay=1)
#         await paco.defer(coro, delay=0.5)
# 
#         # Usage as decorator
#         @paco.defer(delay=1)
#         async def mul_2(num):
#             return num * 2
# 
#         await mul_2(2)
#         # => 4
# 
#     """
#     assert_corofunction(coro=coro)
# 
#     @asyncio.coroutine
#     def wrapper(*args, **kw):
#         # Wait until we're done
#         yield from asyncio.sleep(delay)
#         return (yield from coro(*args, **kw))
# 
#     return wrapper
# 
# Path: tests/helpers.py
# def run_in_loop(coro, *args, **kw):
#     loop = asyncio.get_event_loop()
#     if asyncio.iscoroutinefunction(coro) or isfunction(coro):
#         coro = coro(*args, **kw)
#     return loop.run_until_complete(coro)


# Path: tests/defer_test.py
import time
import asyncio
from paco import defer
from .helpers import run_in_loop

# -*- coding: utf-8 -*-


@asyncio.coroutine
def coro(x):
    return x


def test_defer():
    task = defer(coro, delay=0.2)
    now = time.time()
    assert run_in_loop(task, 1) == 1
    assert time.time() - now >= 0.2


def test_defer_decorator():
    task = defer(delay=0.2)(coro)
    now = time.time()

    assert run_in_loop(task, 1) == 1