-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_awaitable.py
More file actions
264 lines (197 loc) · 6.02 KB
/
test_awaitable.py
File metadata and controls
264 lines (197 loc) · 6.02 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import asyncio
from collections.abc import Coroutine
import _pyawaitable_test
import pytest
from conftest import limit_leaks
import pyawaitable
from pyawaitable.bindings import (abi, add_await, awaitcallback,
awaitcallback_err)
@limit_leaks
@pytest.mark.asyncio
async def test_new():
awaitable = abi.new()
assert isinstance(awaitable, pyawaitable.PyAwaitable)
assert isinstance(awaitable, Coroutine)
await awaitable
await asyncio.create_task(abi.new())
await abi.new()
@limit_leaks
@pytest.mark.asyncio
async def test_await():
called = False
async def coro():
for _ in range(10000):
await asyncio.sleep(0)
nonlocal called
called = True
awaitable = abi.new()
add_await(awaitable, coro(), awaitcallback(0), awaitcallback_err(0))
await awaitable
assert called is True
@limit_leaks
@pytest.mark.asyncio
async def test_await_order():
data = []
awaitable = abi.new()
async def echo(value: int) -> int:
await asyncio.sleep(0)
return value
@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: int) -> int:
data.append(result)
return 0
for i in (1, 2, 3):
add_await(awaitable, echo(i), cb, awaitcallback_err(0))
await awaitable
assert data == [1, 2, 3]
@limit_leaks
@pytest.mark.asyncio
@pytest.mark.filterwarnings(
"ignore::RuntimeWarning"
) # Second and third iteration of echo() are skipped, resulting in a warning
async def test_await_cancel():
data = []
awaitable = abi.new()
async def echo(value: int) -> int:
await asyncio.sleep(0)
return value
@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: int) -> int:
assert result == 1
abi.cancel(awaitable_inner)
data.append(result)
return 0
for i in (1, 2, 3):
add_await(awaitable, echo(i), cb, awaitcallback_err(0))
await awaitable
assert data == [1]
@limit_leaks
@pytest.mark.asyncio
async def test_awaitable_chaining():
data = []
awaitable = abi.new()
async def echo(value: int) -> int:
await asyncio.sleep(0)
return value
@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: int) -> int:
abi.cancel(awaitable_inner)
data.append(result)
return 0
awaitable2 = abi.new()
add_await(awaitable2, echo(1), cb, awaitcallback_err(0))
add_await(awaitable, awaitable2, awaitcallback(0), awaitcallback_err(0))
await awaitable
assert data == [1]
@limit_leaks
@pytest.mark.asyncio
async def test_coro_raise():
awaitable = abi.new()
async def coro() -> None:
await asyncio.sleep(0)
raise ZeroDivisionError("test")
add_await(awaitable, coro(), awaitcallback(0), awaitcallback_err(0))
with pytest.raises(ZeroDivisionError):
await awaitable
@limit_leaks
@pytest.mark.asyncio
async def test_await_no_cb_raise():
awaitable = abi.new()
add_await(awaitable, 42, awaitcallback(0), awaitcallback_err(0))
with pytest.raises(TypeError):
await awaitable
@limit_leaks
@pytest.mark.asyncio
async def test_set_results():
awaitable = abi.new()
async def coro():
await asyncio.sleep(0)
return "abc"
@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: str):
abi.set_result(awaitable_inner, 42)
return 0
add_await(awaitable, coro(), cb, awaitcallback(0))
assert (await awaitable) == 42
@limit_leaks
@pytest.mark.asyncio
async def test_set_results_tracked_type():
awaitable = abi.new()
class TestObject:
def __init__(self) -> None:
self.value = [1, 2, 3]
async def coro():
pass
@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: str):
obj = TestObject()
obj.value.append(4)
abi.set_result(awaitable_inner, obj)
return 0
add_await(awaitable, coro(), cb, awaitcallback(0))
value = await awaitable
assert isinstance(value, TestObject)
assert value.value == [1, 2, 3, 4]
@limit_leaks
@pytest.mark.asyncio
async def test_await_function():
awaitable = abi.new()
called: bool = False
async def coro(value: int, suffix: str) -> str:
await asyncio.sleep(0)
return str(value * 2) + suffix
@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: str):
nonlocal called
called = True
assert result == "42hello"
return 0
abi.await_function(
awaitable, coro, b"is", cb, awaitcallback_err(0), 21, b"hello"
)
await awaitable
assert called is True
@limit_leaks
@pytest.mark.asyncio
async def test_await_function_keywords():
awaitable = abi.new()
called: bool = False
async def coro(value: int, suffix: str ="test") -> str:
await asyncio.sleep(0)
return str(value * 2) + suffix
@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: str):
nonlocal called
called = True
assert result == "70hello"
return 0
abi.await_function_keywords(
awaitable, coro, (35,), b"s:s", cb, awaitcallback_err(0), b"suffix", b"hello"
)
await awaitable
assert called is True
@limit_leaks
@pytest.mark.asyncio
async def test_await_function_no_args():
awaitable = abi.new()
called: bool = False
async def coro() -> str:
await asyncio.sleep(0)
return "70Test"
@awaitcallback
def cb(awaitable_inner: pyawaitable.PyAwaitable, result: str):
nonlocal called
called = True
assert result == "70Test"
return 0
abi.await_function_no_args(awaitable, coro, cb, awaitcallback_err(0))
await awaitable
assert called is True
@limit_leaks
@pytest.mark.asyncio
async def test_c_built_extension():
async def hello():
await asyncio.sleep(0)
return 42
assert (await _pyawaitable_test.test(hello())) == 42
assert (await _pyawaitable_test.test2()) is None