Debian Codegraph @

test/test_multithreaded_sharing.py

raw symbols SCIP
1 """ Test multithreaded sharing of Compressor and Decompressor instances
2
3 Originally written by Eugene Kliuchnikov at Google for the brotli
4 Python bindings under an MIT license.
5
6 """
7 import queue
8 import random
9 import threading
10 import time
11
12 import brotlicffi
13
14
15 def make_compress_input(size):
16 abc = [bytes([b]) for b in b"abcdefghijklmnopqrstuvwxyz"]
17 abc_cap = [bytes([b]) for b in b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
18 num_words_by_len = [0, 25, 100, 175, 1700, 1000, 1000, 1000]
19 word_set = set()
20 rng = random.Random()
21 rng.seed(0x4d3d3d3)
22 words_by_len = [[]]
23 for word_len in range(1, len(num_words_by_len)):
24 num_words = num_words_by_len[word_len]
25 words = []
26 for _ in range(num_words):
27 while True:
28 word = b"".join(
29 [rng.choice(abc_cap)]
30 + [rng.choice(abc) for _ in range(word_len - 1)]
31 )
32 if word not in word_set:
33 word_set.add(word)
34 words.append(word)
35 break
36 words_by_len.append(words)
37 total_size = 0
38 out = []
39 while total_size < size:
40 word_len = rng.choice(range(1, len(num_words_by_len)))
41 word = rng.choice(words_by_len[word_len])
42 total_size += len(word)
43 out.append(word)
44 return b"".join(out)
45
46
47 def _thread_compress(original, compressor, barrier, results):
48 barrier.wait()
49 compressed = compressor.process(original)
50 compressed += compressor.finish()
51 results.put(1)
52
53
54 def _thread_concurrent_process_compress(compressor, barrier, results):
55 barrier.wait()
56 time.sleep(.01)
57 try:
58 _ = compressor.process(b"whatever")
59 except brotlicffi.error:
60 results.put(2)
61
62
63 def _thread_concurrent_flush_compress(compressor, barrier, results):
64 barrier.wait()
65 time.sleep(.01)
66 try:
67 _ = compressor.flush()
68 except brotlicffi.error:
69 results.put(3)
70
71
72 def _thread_concurrent_finish_compress(compressor, barrier, results):
73 barrier.wait()
74 time.sleep(.01)
75 try:
76 _ = compressor.finish()
77 except brotlicffi.error:
78 results.put(4)
79
80
81 def test_compress_concurrency():
82 original = make_compress_input(2 * 1024 * 1024)
83 compressor = brotlicffi.Compressor(quality=9)
84 results = queue.Queue()
85 barrier = threading.Barrier(4)
86 threads = []
87 threads.append(
88 threading.Thread(
89 target=_thread_compress,
90 args=(original, compressor, barrier, results)
91 )
92 )
93 threads.append(
94 threading.Thread(
95 target=_thread_concurrent_process_compress,
96 args=(compressor, barrier, results)
97 )
98 )
99 threads.append(
100 threading.Thread(
101 target=_thread_concurrent_flush_compress,
102 args=(compressor, barrier, results)
103 )
104 )
105 threads.append(
106 threading.Thread(
107 target=_thread_concurrent_finish_compress,
108 args=(compressor, barrier, results)
109 )
110 )
111 for thread in threads:
112 thread.start()
113 for thread in threads:
114 thread.join()
115 assert sorted(list(results.queue)) == [1, 2, 3, 4]
116
117
118 def make_decompress_input(size):
119 compressor = brotlicffi.Compressor(quality=1)
120 prologue = compressor.process(b'b')
121 prologue += compressor.flush()
122 filler = compressor.process(b'c')
123 filler += compressor.flush()
124 epilogue = compressor.finish()
125 return b''.join(
126 [prologue] + [filler] * (size // len(filler)) + [epilogue])
127
128
129 def _thread_decompress(compressed, decompressor, barrier, results):
130 barrier.wait()
131 _ = decompressor.process(compressed)
132 if decompressor.is_finished():
133 results.put(1)
134
135
136 def _thread_concurrent_process(decompressor, barrier, results):
137 barrier.wait()
138 time.sleep(.001)
139 try:
140 _ = decompressor.process(b'')
141 except brotlicffi.error:
142 results.put(2)
143
144
145 def _thread_concurrent_can_accept_more_data(decompressor, barrier, results):
146 barrier.wait()
147 time.sleep(.001)
148 try:
149 _ = decompressor.can_accept_more_data()
150 except brotlicffi.error:
151 results.put(3)
152
153
154 def _thread_concurrent_is_finished(decompressor, barrier, results):
155 barrier.wait()
156 time.sleep(.001)
157 try:
158 _ = decompressor.is_finished()
159 except brotlicffi.error:
160 results.put(4)
161
162
163 def test_decompressor_concurrency():
164 compressed = make_decompress_input(16 * 1024 * 1024)
165 decompressor = brotlicffi.Decompressor()
166 results = queue.Queue()
167 barrier = threading.Barrier(4)
168 threads = []
169 threads.append(
170 threading.Thread(
171 target=_thread_decompress,
172 args=(compressed, decompressor, barrier, results)
173 )
174 )
175 threads.append(
176 threading.Thread(
177 target=_thread_concurrent_process,
178 args=(decompressor, barrier, results)
179 )
180 )
181 threads.append(
182 threading.Thread(
183 target=_thread_concurrent_can_accept_more_data,
184 args=(decompressor, barrier, results),
185 )
186 )
187 threads.append(
188 threading.Thread(
189 target=_thread_concurrent_is_finished,
190 args=(decompressor, barrier, results)
191 )
192 )
193 for thread in threads:
194 thread.start()
195 for thread in threads:
196 thread.join()
197 assert sorted(list(results.queue)) == [1, 2, 3, 4]