aboutsummaryrefslogtreecommitdiff
path: root/tests/test_executor.py
blob: a7fef4807bc1e5249172d5408edfe341bbe0a62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-

from time import sleep

from mock import Mock

from pyee import ExecutorEventEmitter


class PyeeTestError(Exception):
    pass


def test_executor_emit():
    """Test that ExecutorEventEmitters can emit events."""
    with ExecutorEventEmitter() as ee:
        should_call = Mock()

        @ee.on("event")
        def event_handler():
            should_call(True)

        ee.emit("event")
        sleep(0.1)

        should_call.assert_called_once()


def test_executor_once():
    """Test that ExecutorEventEmitters also emit events for once."""
    with ExecutorEventEmitter() as ee:
        should_call = Mock()

        @ee.once("event")
        def event_handler():
            should_call(True)

        ee.emit("event")
        sleep(0.1)

        should_call.assert_called_once()


def test_executor_error():
    """Test that ExecutorEventEmitters handle errors."""
    with ExecutorEventEmitter() as ee:
        should_call = Mock()

        @ee.on("event")
        def event_handler():
            raise PyeeTestError()

        @ee.on("error")
        def handle_error(e):
            should_call(e)

        ee.emit("event")

        sleep(0.1)

        should_call.assert_called_once()