summaryrefslogtreecommitdiffstats
path: root/deluge/tests/test_maybe_coroutine.py
blob: 2717e78bb636741213103a7f2332da1b5761555b (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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
import pytest
import pytest_twisted
import twisted.python.failure
from twisted.internet import defer, reactor, task
from twisted.internet.defer import maybeDeferred

from deluge.decorators import maybe_coroutine


@defer.inlineCallbacks
def inline_func():
    result = yield task.deferLater(reactor, 0, lambda: 'function_result')
    return result


@defer.inlineCallbacks
def inline_error():
    raise Exception('function_error')
    yield


@maybe_coroutine
async def coro_func():
    result = await task.deferLater(reactor, 0, lambda: 'function_result')
    return result


@maybe_coroutine
async def coro_error():
    raise Exception('function_error')


@defer.inlineCallbacks
def coro_func_from_inline():
    result = yield coro_func()
    return result


@defer.inlineCallbacks
def coro_error_from_inline():
    result = yield coro_error()
    return result


@maybe_coroutine
async def coro_func_from_coro():
    return await coro_func()


@maybe_coroutine
async def coro_error_from_coro():
    return await coro_error()


@maybe_coroutine
async def inline_func_from_coro():
    return await inline_func()


@maybe_coroutine
async def inline_error_from_coro():
    return await inline_error()


@pytest_twisted.inlineCallbacks
def test_standard_twisted():
    """Sanity check that twisted tests work how we expect.

    Not really testing deluge code at all.
    """
    result = yield inline_func()
    assert result == 'function_result'

    with pytest.raises(Exception, match='function_error'):
        yield inline_error()


@pytest.mark.parametrize(
    'function',
    [
        inline_func,
        coro_func,
        coro_func_from_coro,
        coro_func_from_inline,
        inline_func_from_coro,
    ],
)
@pytest_twisted.inlineCallbacks
def test_from_inline(function):
    """Test our coroutines wrapped with maybe_coroutine as if they returned plain twisted deferreds."""
    result = yield function()
    assert result == 'function_result'

    def cb(result):
        assert result == 'function_result'

    d = function()
    d.addCallback(cb)
    yield d


@pytest.mark.parametrize(
    'function',
    [
        inline_error,
        coro_error,
        coro_error_from_coro,
        coro_error_from_inline,
        inline_error_from_coro,
    ],
)
@pytest_twisted.inlineCallbacks
def test_error_from_inline(function):
    """Test our coroutines wrapped with maybe_coroutine as if they returned plain twisted deferreds that raise."""
    with pytest.raises(Exception, match='function_error'):
        yield function()

    def eb(result):
        assert isinstance(result, twisted.python.failure.Failure)
        assert result.getErrorMessage() == 'function_error'

    d = function()
    d.addErrback(eb)
    yield d


@pytest.mark.parametrize(
    'function',
    [
        inline_func,
        coro_func,
        coro_func_from_coro,
        coro_func_from_inline,
        inline_func_from_coro,
    ],
)
@pytest_twisted.ensureDeferred
async def test_from_coro(function):
    """Test our coroutines wrapped with maybe_coroutine work from another coroutine."""
    result = await function()
    assert result == 'function_result'


@pytest.mark.parametrize(
    'function',
    [
        inline_error,
        coro_error,
        coro_error_from_coro,
        coro_error_from_inline,
        inline_error_from_coro,
    ],
)
@pytest_twisted.ensureDeferred
async def test_error_from_coro(function):
    """Test our coroutines wrapped with maybe_coroutine work from another coroutine with errors."""
    with pytest.raises(Exception, match='function_error'):
        await function()


@pytest_twisted.ensureDeferred
async def test_tracebacks_preserved():
    with pytest.raises(Exception) as exc:
        await coro_error_from_coro()
    traceback_lines = [
        'await coro_error_from_coro()',
        'return await coro_error()',
        "raise Exception('function_error')",
    ]
    # If each coroutine got wrapped with ensureDeferred, the traceback will be mangled
    # verify the coroutines passed through by checking the traceback.
    for expected, actual in zip(traceback_lines, exc.traceback):
        assert expected in str(actual)


@pytest_twisted.ensureDeferred
async def test_maybe_deferred_coroutine():
    result = await maybeDeferred(coro_func)
    assert result == 'function_result'


@pytest_twisted.ensureDeferred
async def test_callback_before_await():
    def cb(res):
        assert res == 'function_result'
        return res

    d = coro_func()
    d.addCallback(cb)
    result = await d
    assert result == 'function_result'


@pytest_twisted.ensureDeferred
async def test_callback_after_await():
    """If it has already been used as a coroutine, can't be retroactively turned into a Deferred.
    This limitation could be fixed, but the extra complication doesn't feel worth it.
    """

    def cb(res):
        pass

    d = coro_func()
    await d
    with pytest.raises(
        Exception, match='Cannot add callbacks to an already awaited coroutine'
    ):
        d.addCallback(cb)