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

import nose.tools as nt
from asyncio import get_event_loop
from asyncio import sleep as async_sleep

from pyee import EventEmitter


def test_async_emit():
    """Test that event_emitters can handle wrapping coroutines
    """
    ee = EventEmitter()
    loop = get_event_loop()

    class SenseWasCalled():
        def __init__(self):
            self.was_called = False

        def am_calling(self):
            self.was_called = True

        def assert_was_called(self):
            nt.assert_true(self.was_called)

    sensor = SenseWasCalled()

    @ee.on('event')
    async def event_handler():
        sensor.am_calling()

    ee.emit('event')
    loop.run_until_complete(async_sleep(1))

    sensor.assert_was_called()

# -*- coding: utf-8 -*-
import nose.tools as nt
from asyncio import Future, gather, get_event_loop, sleep

from pyee import EventEmitter


def test_async_emit():
    """Test that event_emitters can handle wrapping coroutines
    """
    loop = get_event_loop()
    ee = EventEmitter(loop=loop)

    future = Future()

    @ee.on('event')
    async def event_handler():
        future.set_result(True)

    async def create_timeout(loop=loop):
        await sleep(1, loop=loop)
        future.cancel()

    timeout = create_timeout(loop=loop)

    @future.add_done_callback
    def _done(result):
        nt.assert_true(result)

    ee.emit('event')

    loop.run_until_complete(gather(future, timeout))

Rewrite asyncio test to use futures