File: test_call_wait_timeout.py

package info (click to toggle)
circuits 3.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,980 kB
  • sloc: python: 17,583; javascript: 3,226; makefile: 100
file content (97 lines) | stat: -rw-r--r-- 1,820 bytes parent folder | download | duplicates (2)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
import pytest

from circuits.core import Component, Event, TimeoutError, handler


class wait(Event):
    """wait Event"""

    success = True


class call(Event):
    """call Event"""

    success = True


class hello(Event):
    """hello Event"""

    success = True


class App(Component):
    @handler('wait')
    def _on_wait(self, timeout=-1):
        result = self.fire(hello())
        try:
            yield self.wait('hello', timeout=timeout)
        except TimeoutError as e:
            yield e
        else:
            yield result

    @handler('hello')
    def _on_hello(self):
        return 'hello'

    @handler('call')
    def _on_call(self, timeout=-1):
        result = None
        try:
            result = yield self.call(hello(), timeout=timeout)
        except TimeoutError as e:
            yield e
        else:
            yield result


@pytest.fixture()
def app(request, manager, watcher):
    app = App().register(manager)
    assert watcher.wait('registered')

    def finalizer():
        app.unregister()

    request.addfinalizer(finalizer)

    return app


def test_wait_success(manager, watcher, app):
    x = manager.fire(wait(10))
    assert watcher.wait('wait_success')

    value = x.value

    assert value == 'hello'


def test_wait_failure(manager, watcher, app):
    x = manager.fire(wait(0))
    assert watcher.wait('wait_success')

    value = x.value

    assert isinstance(value, TimeoutError)


def test_call_success(manager, watcher, app):
    x = manager.fire(call(10))
    assert watcher.wait('call_success')

    value = x.value

    assert value == 'hello'


def test_call_failure(manager, watcher, app):
    x = manager.fire(call(0))
    assert watcher.wait('call_success')

    value = x.value

    assert isinstance(value, TimeoutError)