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
|
import tests
try:
import asyncio
exec('''if 1:
def hello_world(result, delay):
result.append("Hello")
# retrieve the event loop from the policy
yield from asyncio.sleep(delay)
result.append('World')
return "."
def waiter(result):
loop = asyncio.get_event_loop()
fut = asyncio.Future(loop=loop)
loop.call_soon(fut.set_result, "Future")
value = yield from fut
result.append(value)
value = yield from hello_world(result, 0.001)
result.append(value)
''')
except ImportError:
import trollius as asyncio
from trollius import From, Return
def hello_world(result, delay):
result.append("Hello")
# retrieve the event loop from the policy
yield From(asyncio.sleep(delay))
result.append('World')
raise Return(".")
def waiter(result):
loop = asyncio.get_event_loop()
fut = asyncio.Future(loop=loop)
loop.call_soon(fut.set_result, "Future")
value = yield From(fut)
result.append(value)
value = yield From(hello_world(result, 0.001))
result.append(value)
class CallbackTests(tests.TestCase):
def test_hello_world(self):
result = []
self.loop.run_until_complete(hello_world(result, 0.001))
self.assertEqual(result, ['Hello', 'World'])
def test_waiter(self):
result = []
self.loop.run_until_complete(waiter(result))
self.assertEqual(result, ['Future', 'Hello', 'World', '.'])
if __name__ == '__main__':
import unittest
unittest.main()
|