Debian Codegraph @

extra_tests/test_greenlet_thread.py

raw symbols SCIP
1 import pytest
2 import sys, time
3 if sys.version_info < (3,):
4 import thread
5 else:
6 import _thread as thread
7 greenlet = pytest.importorskip('greenlet')
8
9
10 class TestThread:
11
12 def test_cannot_switch_to_main_of_different_thread(self):
13 mains = []
14 mains.append(greenlet.getcurrent())
15 lock = thread.allocate_lock()
16 lock.acquire()
17 got_exception = []
18 #
19 def run_thread():
20 main = greenlet.getcurrent()
21 assert main not in mains
22 mains.append(main)
23 try:
24 mains[0].switch()
25 except Exception as e:
26 got_exception.append(e)
27 lock.release()
28 #
29 thread.start_new_thread(run_thread, ())
30 lock.acquire()
31 assert isinstance(got_exception[0], greenlet.error)
32
33 def test_nonstarted_greenlet_is_still_attached_to_thread(self):
34 subs = []
35 lock = thread.allocate_lock()
36 lock.acquire()
37 #
38 def run_thread():
39 g = greenlet.greenlet(lambda *args: None)
40 subs.append(g)
41 lock.release()
42 time.sleep(1)
43 #
44 thread.start_new_thread(run_thread, ())
45 lock.acquire()
46 [g] = subs
47 pytest.raises(greenlet.error, g.switch)
48
49 def test_noninited_greenlet_is_still_attached_to_thread(self):
50 subs = []
51 lock = thread.allocate_lock()
52 lock.acquire()
53 #
54 def run_thread():
55 g = greenlet.greenlet.__new__(greenlet.greenlet)
56 subs.append(g)
57 lock.release()
58 time.sleep(1)
59 #
60 thread.start_new_thread(run_thread, ())
61 lock.acquire()
62 [g] = subs
63 pytest.raises(greenlet.error, g.switch)
64 g.__init__(lambda *args: None)
65 pytest.raises(greenlet.error, g.switch)
66
67 def test_noninited_greenlet_change_thread_via_parent(self):
68 subs = []
69 lock = thread.allocate_lock()
70 lock.acquire()
71 #
72 def run_thread():
73 g = greenlet.greenlet.__new__(greenlet.greenlet)
74 subs.append(g)
75 lock.release()
76 time.sleep(1)
77 #
78 thread.start_new_thread(run_thread, ())
79 lock.acquire()
80 [g] = subs
81 g.__init__(lambda *args: 44)
82 g.parent = greenlet.getcurrent()
83 x = g.switch()
84 assert x == 44
85
86 def test_nonstarted_greenlet_change_thread_via_parent(self):
87 subs = []
88 lock = thread.allocate_lock()
89 lock.acquire()
90 #
91 def run_thread():
92 g = greenlet.greenlet(lambda *args: 42)
93 subs.append(g)
94 lock.release()
95 time.sleep(1)
96 #
97 thread.start_new_thread(run_thread, ())
98 lock.acquire()
99 [g] = subs
100 g.parent = greenlet.getcurrent()
101 x = g.switch()
102 assert x == 42
103
104 def test_started_greenlet_cannot_change_thread_via_parent(self):
105 pytest.skip("not implemented on PyPy")
106 subs = []
107 lock = thread.allocate_lock()
108 lock.acquire()
109 #
110 def run_thread():
111 g_parent = greenlet.getcurrent()
112 g = greenlet.greenlet(lambda *args: g_parent.switch(42))
113 x = g.switch()
114 assert x == 42
115 subs.append(g)
116 lock.release()
117 time.sleep(1)
118 #
119 thread.start_new_thread(run_thread, ())
120 lock.acquire()
121 [g] = subs
122 with pytest.raises(ValueError) as e:
123 g.parent = greenlet.getcurrent()
124 assert str(e.value) == "parent cannot be on a different thread"
125
126 def test_finished_greenlet_cannot_change_thread_via_parent(self):
127 pytest.skip("not implemented on PyPy")
128 subs = []
129 lock = thread.allocate_lock()
130 lock.acquire()
131 #
132 def run_thread():
133 g = greenlet.greenlet(lambda *args: 42)
134 x = g.switch()
135 assert x == 42
136 subs.append(g)
137 lock.release()
138 time.sleep(1)
139 #
140 thread.start_new_thread(run_thread, ())
141 lock.acquire()
142 [g] = subs
143 with pytest.raises(ValueError) as e:
144 g.parent = greenlet.getcurrent()
145 assert str(e.value) == "parent cannot be on a different thread"