-
Notifications
You must be signed in to change notification settings - Fork 240
/
test_meta.py
140 lines (105 loc) · 3 KB
/
test_meta.py
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
from curio import meta
from curio import *
from functools import partial
import pytest
import sys
def test_blocking(kernel):
@meta.blocking
def func():
return 1
async def main():
r = await func()
assert r == 1
assert func() == 1
kernel.run(main)
@meta.cpubound
def cpufunc():
return 1
def test_cpubound(kernel):
async def main():
r = await cpufunc()
assert r == 1
assert cpufunc() == 1
kernel.run(main)
def test_iscoroutinefunc():
async def spam(x, y):
pass
assert meta.iscoroutinefunction(partial(spam, 1))
def test_async_abc():
class AsyncSpam(meta.AsyncABC):
async def spam(self):
pass
with pytest.raises(TypeError):
class Child(AsyncSpam):
def spam(self):
pass
class Child2(AsyncSpam):
async def spam(self):
pass
def test_sync_only(kernel):
@meta.sync_only
def func():
return 1
async def main():
with pytest.raises(SyncIOError):
r = func()
assert func() == 1
kernel.run(main)
def test_bad_awaitable():
def spam(x, y):
pass
with pytest.raises(TypeError):
@meta.awaitable(spam)
def spam(x, y, z):
pass
def test_awaitable_partial(kernel):
def func(x, y, z):
assert False
@meta.awaitable(func)
async def func(x, y, z):
assert x == 1
assert y == 2
assert z == 3
return True
async def main():
assert await func(1, 2, 3)
assert await ignore_after(1, func(1,2,3))
assert await ignore_after(1, func, 1, 2, 3)
assert await ignore_after(1, partial(func, 1, 2), 3)
assert await ignore_after(1, partial(func, z=3), 1, 2)
assert await ignore_after(1, partial(partial(func, 1), 2), 3)
# Try spawns
t = await spawn(func(1,2,3))
assert await t.join()
t = await spawn(func, 1, 2, 3)
assert await t.join()
t = await spawn(partial(func, 1, 2), 3)
assert await t.join()
t = await spawn(partial(func, z=3), 1, 2)
assert await t.join()
t = await spawn(partial(partial(func, 1), 2), 3)
assert await t.join()
kernel.run(main)
kernel.run(func, 1, 2, 3)
kernel.run(partial(func, 1, 2), 3)
kernel.run(partial(func, z=3), 1, 2)
if sys.version_info >= (3,7):
import contextlib
def test_asynccontextmanager(kernel):
results = []
@contextlib.asynccontextmanager
async def manager():
try:
yield (await coro())
finally:
await cleanup()
async def coro():
results.append('coro')
return 'result'
async def cleanup():
results.append('cleanup')
async def main():
async with manager() as r:
results.append(r)
kernel.run(main)
assert results == ['coro', 'result', 'cleanup']