summaryrefslogtreecommitdiff
path: root/tests/test_eventlet.py
blob: 4d5f019732f2914c500f67eb364140079a2d3551 (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
214
215
216
import eventlet
import tests

def eventlet_slow_append(result, value, delay):
    eventlet.sleep(delay)
    result.append(value)
    return value * 10

def eventlet_slow_error(delay):
    eventlet.sleep(delay)
    raise ValueError("error")

try:
    import asyncio

    exec('''if 1:
        @asyncio.coroutine
        def wait_greenthread(gt, loop=None):
            if loop is None:
                loop = asyncio.get_event_loop()
            fut = asyncio.Future(loop=loop)

            def copy_result(gt):
                try:
                    value = gt.wait()
                except Exception as exc:
                    loop.call_soon(fut.set_exception, exc)
                else:
                    loop.call_soon(fut.set_result, value)
            gt.link(copy_result)

            value = yield from fut
            return value

        @asyncio.coroutine
        def coro_chain_greenthread():
            result = []
            loop = asyncio.get_event_loop()

            g1 = eventlet.spawn(eventlet_slow_append, result, 1, 0.2)

            value = yield from wait_greenthread(g1, loop=loop)
            result.append(value)

            g2 = eventlet.spawn(eventlet_slow_append, result, 2, 0.1)
            value = yield from wait_greenthread(g2, loop=loop)
            result.append(value)

            g3 = eventlet.spawn(eventlet_slow_error, 0.001)
            try:
                yield from wait_greenthread(g3, loop=loop)
            except ValueError as exc:
                result.append(str(exc))

            result.append(4)
            return result

        @asyncio.coroutine
        def coro_slow_append(result, value, delay):
            yield from asyncio.sleep(delay)
            result.append(value)
            return value * 10

        @asyncio.coroutine
        def coro_slow_error(delay):
            yield from asyncio.sleep(delay)
            raise ValueError("error")
    ''')
except ImportError:
    import trollius as asyncio
    from trollius import From, Return

    @asyncio.coroutine
    def wait_greenthread(gt, loop=None):
        if loop is None:
            loop = asyncio.get_event_loop()
        fut = asyncio.Future(loop=loop)

        def copy_result(gt):
            try:
                value = gt.wait()
            except Exception as exc:
                loop.call_soon(fut.set_exception, exc)
            else:
                loop.call_soon(fut.set_result, value)
        gt.link(copy_result)

        value = yield From(fut)
        raise Return(value)

    @asyncio.coroutine
    def coro_chain_greenthread():
        result = []
        loop = asyncio.get_event_loop()

        g1 = eventlet.spawn(eventlet_slow_append, result, 1, 0.2)

        value = yield From(wait_greenthread(g1, loop=loop))
        result.append(value)

        g2 = eventlet.spawn(eventlet_slow_append, result, 2, 0.1)
        value = yield From(wait_greenthread(g2, loop=loop))
        result.append(value)

        g3 = eventlet.spawn(eventlet_slow_error, 0.001)
        try:
            yield From(wait_greenthread(g3, loop=loop))
        except ValueError as exc:
            result.append(str(exc))

        result.append(4)
        raise Return(result)

    @asyncio.coroutine
    def coro_slow_append(result, value, delay):
        yield From(asyncio.sleep(delay))
        result.append(value)
        raise Return(value * 10)

    @asyncio.coroutine
    def coro_slow_error(delay):
        yield From(asyncio.sleep(delay))
        raise ValueError("error")


def wait_task(task):
    event = eventlet.event.Event()
    def done(fut):
        try:
            result = fut.result()
        except Exception as exc:
            # FIXME
            event.send_exception(exc)
        else:
            event.send(result)

    task.add_done_callback(done)
    return event.wait()

def greenthread_chain_coro(result, loop):
    t1 = loop.create_task(coro_slow_append(result, 1, 0.2))
    value = wait_task(t1)
    result.append(value)

    t2 = loop.create_task(coro_slow_append(result, 2, 0.1))
    value = wait_task(t2)
    result.append(value)

    t3 = loop.create_task(coro_slow_error(0.001))
    try:
        value = wait_task(t3)
    except ValueError as exc:
        result.append(str(exc))

    result.append(4)
    loop.call_soon(loop.stop)
    return result


class EventletTests(tests.TestCase):
    def test_stop(self):
        def func():
            eventlet.spawn(self.loop.call_soon, self.loop.stop)

        def schedule_greenthread():
            eventlet.spawn(func)

        self.loop.call_soon(schedule_greenthread)
        self.loop.run_forever()

    def test_soon(self):
        result = []

        def func():
            result.append("spawn")
            self.loop.call_soon(self.loop.stop)

        def schedule_greenthread():
            eventlet.spawn(func)

        self.loop.call_soon(schedule_greenthread)
        self.loop.run_forever()
        self.assertEqual(result, ["spawn"])

    def test_soon_spawn(self):
        result = []

        def func1():
            result.append("spawn")

        def func2():
            result.append("spawn_after")
            self.loop.call_soon(self.loop.stop)

        def schedule_greenthread():
            eventlet.spawn(func1)
            eventlet.spawn_after(0.010, func2)

        self.loop.call_soon(schedule_greenthread)
        self.loop.run_forever()
        self.assertEqual(result, ["spawn", "spawn_after"])

    def test_coro_chain_greenthread(self):
        result = self.loop.run_until_complete(coro_chain_greenthread())
        self.assertEqual(result, [1, 10, 2, 20, 'error', 4])

    def test_greenthread_chain_coro(self):
        result = []
        self.loop.call_soon(eventlet.spawn, greenthread_chain_coro, result, self.loop)
        self.loop.run_forever()
        self.assertEqual(result, [1, 10, 2, 20, 'error', 4])


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