File: test_asyncio_trollius.py

package info (click to toggle)
python-greenio 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 144 kB
  • ctags: 200
  • sloc: python: 988; makefile: 32
file content (70 lines) | stat: -rw-r--r-- 1,675 bytes parent folder | download
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
"""
Test interoperability between asyncio and trollius. Trollius event loop should
support asyncio coroutines.
"""

import asyncio
import greenio
import trollius
import unittest
from trollius import From, Return
from trollius.test_utils import TestCase


class TrolliusEventLoopTests(TestCase):
    def setUp(self):
        policy = greenio.GreenTrolliusEventLoopPolicy()
        asyncio.set_event_loop_policy(policy)
        trollius.set_event_loop_policy(policy)
        self.loop = trollius.new_event_loop()
        policy.set_event_loop(self.loop)

    def tearDown(self):
        self.loop.close()
        asyncio.set_event_loop_policy(None)

    def test_trollius_coroutine(self):
        @trollius.coroutine
        def bar():
            yield From(None)
            raise Return(30)

        @trollius.coroutine
        def foo():
            bar_result = greenio.yield_from(bar())
            return bar_result + 12

        @greenio.task
        def test():
            res = yield From(foo())
            raise Return(res)

        fut = test()
        self.loop.run_until_complete(fut)

        self.assertEqual(fut.result(), 42)

    def test_asyncio_coroutine(self):
        @asyncio.coroutine
        def bar():
            yield from []
            return 30

        @asyncio.coroutine
        def foo():
            bar_result = greenio.yield_from(bar())
            return bar_result + 12

        @greenio.task
        def test():
            res = yield From(foo())
            raise Return(res)

        fut = test()
        self.loop.run_until_complete(fut)

        self.assertEqual(fut.result(), 42)


if __name__ == '__main__':
    unittest.main()