summaryrefslogtreecommitdiff
path: root/tests/test_greenlet.py
blob: ded3cb62c1bd968acf1a2164df6c6ae91bc284d9 (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
import aioeventlet
import greenlet
import tests


class WrapGreenletTests(tests.TestCase):
    def test_wrap_greenlet(self):
        def func(value):
            return value * 3

        gl = greenlet.greenlet(func)
        fut = aioeventlet.wrap_greenthread(gl)
        gl.switch(5)
        result = self.loop.run_until_complete(fut)
        self.assertEqual(result, 15)

    def test_wrap_greenlet_exc(self):
        def func():
            raise ValueError(7)

        gl = greenlet.greenlet(func)
        fut = aioeventlet.wrap_greenthread(gl)
        gl.switch()
        self.assertRaises(ValueError, self.loop.run_until_complete, fut)

    def test_wrap_greenlet_no_run_attr(self):
        gl = greenlet.greenlet()
        msg = "wrap_greenthread: the run attribute of the greenlet is not set"
        self.assertRaisesRegex(RuntimeError, msg,
                               aioeventlet.wrap_greenthread, gl)

    def test_wrap_greenlet_running(self):
        def func(value):
            gl = greenlet.getcurrent()
            return aioeventlet.wrap_greenthread(gl)

        gl = greenlet.greenlet(func)
        msg = "wrap_greenthread: the greenthread is running"
        self.assertRaisesRegex(RuntimeError, msg, gl.switch, 5)

    def test_wrap_greenlet_dead(self):
        def func(value):
            return value * 3

        gl = greenlet.greenlet(func)
        gl.switch(5)
        msg = "wrap_greenthread: the greenthread already finished"
        self.assertRaisesRegex(RuntimeError, msg, aioeventlet.wrap_greenthread, gl)


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