forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_generators.py
More file actions
159 lines (127 loc) · 4.45 KB
/
test_generators.py
File metadata and controls
159 lines (127 loc) · 4.45 KB
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
import concurrent.futures
import itertools
import threading
import unittest
from threading import Barrier
from unittest import TestCase
import random
import time
from test.support import threading_helper, Py_GIL_DISABLED
threading_helper.requires_working_threading(module=True)
def random_sleep():
delay_us = random.randint(50, 100)
time.sleep(delay_us * 1e-6)
def random_string():
return ''.join(random.choice('0123456789ABCDEF') for _ in range(10))
def set_gen_name(g, b):
b.wait()
random_sleep()
g.__name__ = random_string()
return g.__name__
def set_gen_qualname(g, b):
b.wait()
random_sleep()
g.__qualname__ = random_string()
return g.__qualname__
@unittest.skipUnless(Py_GIL_DISABLED, "Enable only in FT build")
class TestFTGenerators(TestCase):
NUM_THREADS = 4
def concurrent_write_with_func(self, func):
gen = (x for x in range(42))
for j in range(1000):
with concurrent.futures.ThreadPoolExecutor(max_workers=self.NUM_THREADS) as executor:
b = Barrier(self.NUM_THREADS)
futures = {executor.submit(func, gen, b): i for i in range(self.NUM_THREADS)}
for fut in concurrent.futures.as_completed(futures):
gen_name = fut.result()
self.assertEqual(len(gen_name), 10)
def test_concurrent_write(self):
with self.subTest(func=set_gen_name):
self.concurrent_write_with_func(func=set_gen_name)
with self.subTest(func=set_gen_qualname):
self.concurrent_write_with_func(func=set_gen_qualname)
def test_concurrent_send(self):
def gen():
yield 1
yield 2
yield 3
yield 4
yield 5
def run_test(drive_generator):
g = gen()
values = []
threading_helper.run_concurrently(drive_generator, self.NUM_THREADS, args=(g, values,))
self.assertEqual(sorted(values), [1, 2, 3, 4, 5])
def call_next(g, values):
while True:
try:
values.append(next(g))
except ValueError:
continue
except StopIteration:
break
with self.subTest(method='next'):
run_test(call_next)
def call_send(g, values):
while True:
try:
values.append(g.send(None))
except ValueError:
continue
except StopIteration:
break
with self.subTest(method='send'):
run_test(call_send)
def for_iter_gen(g, values):
while True:
try:
for value in g:
values.append(value)
else:
break
except ValueError:
continue
with self.subTest(method='for'):
run_test(for_iter_gen)
def test_concurrent_close(self):
def gen():
for i in range(10):
yield i
time.sleep(0.001)
def drive_generator(g):
while True:
try:
for value in g:
if value == 5:
g.close()
else:
return
except ValueError as e:
self.assertEqual(e.args[0], "generator already executing")
g = gen()
threading_helper.run_concurrently(drive_generator, self.NUM_THREADS, args=(g,))
def test_concurrent_gi_yieldfrom(self):
def gen_yield_from():
yield from itertools.count()
g = gen_yield_from()
next(g) # Put in FRAME_SUSPENDED_YIELD_FROM state
def read_yieldfrom(gen):
for _ in range(10000):
self.assertIsNotNone(gen.gi_yieldfrom)
threading_helper.run_concurrently(read_yieldfrom, self.NUM_THREADS, args=(g,))
def test_gi_yieldfrom_close_race(self):
def gen_yield_from():
yield from itertools.count()
g = gen_yield_from()
next(g)
done = threading.Event()
def reader():
while not done.is_set():
g.gi_yieldfrom
def closer():
try:
g.close()
except ValueError:
pass
done.set()
threading_helper.run_concurrently([reader, closer])