1# -*- coding: utf-8 -*-
2
3from time import sleep
4
5from mock import Mock
6
7from pyee import ExecutorEventEmitter
8
9
10class PyeeTestError(Exception):
11    pass
12
13
14def test_executor_emit():
15    """Test that ExecutorEventEmitters can emit events."""
16    with ExecutorEventEmitter() as ee:
17        should_call = Mock()
18
19        @ee.on("event")
20        def event_handler():
21            should_call(True)
22
23        ee.emit("event")
24        sleep(0.1)
25
26        should_call.assert_called_once()
27
28
29def test_executor_once():
30    """Test that ExecutorEventEmitters also emit events for once."""
31    with ExecutorEventEmitter() as ee:
32        should_call = Mock()
33
34        @ee.once("event")
35        def event_handler():
36            should_call(True)
37
38        ee.emit("event")
39        sleep(0.1)
40
41        should_call.assert_called_once()
42
43
44def test_executor_error():
45    """Test that ExecutorEventEmitters handle errors."""
46    with ExecutorEventEmitter() as ee:
47        should_call = Mock()
48
49        @ee.on("event")
50        def event_handler():
51            raise PyeeTestError()
52
53        @ee.on("error")
54        def handle_error(e):
55            should_call(e)
56
57        ee.emit("event")
58
59        sleep(0.1)
60
61        should_call.assert_called_once()
62