Debian Codegraph @

Lib/test/test_interpreters/test_channels.py

raw
1 import importlib
2 import pickle
3 import threading
4 from textwrap import dedent
5 import unittest
6 import time
7
8 from test.support import import_helper
9 # Raise SkipTest if subinterpreters not supported.
10 _channels = import_helper.import_module('_interpchannels')
11 from test.support import interpreters
12 from test.support.interpreters import channels
13 from .utils import _run_output, TestBase
14
15
16 class LowLevelTests(TestBase):
17
18 # The behaviors in the low-level module is important in as much
19 # as it is exercised by the high-level module. Therefore the
20 # most # important testing happens in the high-level tests.
21 # These low-level tests cover corner cases that are not
22 # encountered by the high-level module, thus they
23 # mostly shouldn't matter as much.
24
25 # Additional tests are found in Lib/test/test__interpchannels.py.
26 # XXX Those should be either moved to LowLevelTests or eliminated
27 # in favor of high-level tests in this file.
28
29 def test_highlevel_reloaded(self):
30 # See gh-115490 (https://github.com/python/cpython/issues/115490).
31 importlib.reload(channels)
32
33
34 class TestChannels(TestBase):
35
36 def test_create(self):
37 r, s = channels.create()
38 self.assertIsInstance(r, channels.RecvChannel)
39 self.assertIsInstance(s, channels.SendChannel)
40
41 def test_list_all(self):
42 self.assertEqual(channels.list_all(), [])
43 created = set()
44 for _ in range(3):
45 ch = channels.create()
46 created.add(ch)
47 after = set(channels.list_all())
48 self.assertEqual(after, created)
49
50 def test_list_all_closed(self):
51 created = [channels.create() for _ in range(3)]
52 rch, sch = created.pop(1)
53 rch.close()
54 self.assertEqual(set(channels.list_all()), set(created))
55
56 def test_shareable(self):
57 interp = interpreters.create()
58 rch, sch = channels.create()
59
60 self.assertTrue(
61 interpreters.is_shareable(rch))
62 self.assertTrue(
63 interpreters.is_shareable(sch))
64
65 sch.send_nowait(rch)
66 sch.send_nowait(sch)
67 rch2 = rch.recv()
68 sch2 = rch.recv()
69
70 interp.prepare_main(rch=rch, sch=sch)
71 sch.send_nowait(rch)
72 sch.send_nowait(sch)
73 interp.exec(dedent("""
74 rch2 = rch.recv()
75 sch2 = rch.recv()
76 assert rch2 == rch
77 assert sch2 == sch
78
79 sch.send_nowait(rch2)
80 sch.send_nowait(sch2)
81 """))
82 rch3 = rch.recv()
83 sch3 = rch.recv()
84
85 self.assertEqual(rch2, rch)
86 self.assertEqual(sch2, sch)
87 self.assertEqual(rch3, rch)
88 self.assertEqual(sch3, sch)
89
90 def test_is_closed(self):
91 rch, sch = channels.create()
92 rbefore = rch.is_closed
93 sbefore = sch.is_closed
94 rch.close()
95 rafter = rch.is_closed
96 safter = sch.is_closed
97
98 self.assertFalse(rbefore)
99 self.assertFalse(sbefore)
100 self.assertTrue(rafter)
101 self.assertTrue(safter)
102
103
104 class TestRecvChannelAttrs(TestBase):
105
106 def test_id_type(self):
107 rch, _ = channels.create()
108 self.assertIsInstance(rch.id, _channels.ChannelID)
109
110 def test_custom_id(self):
111 rch = channels.RecvChannel(1)
112 self.assertEqual(rch.id, 1)
113
114 with self.assertRaises(TypeError):
115 channels.RecvChannel('1')
116
117 def test_id_readonly(self):
118 rch = channels.RecvChannel(1)
119 with self.assertRaises(AttributeError):
120 rch.id = 2
121
122 def test_equality(self):
123 ch1, _ = channels.create()
124 ch2, _ = channels.create()
125 self.assertEqual(ch1, ch1)
126 self.assertNotEqual(ch1, ch2)
127
128 def test_pickle(self):
129 ch, _ = channels.create()
130 data = pickle.dumps(ch)
131 unpickled = pickle.loads(data)
132 self.assertEqual(unpickled, ch)
133
134
135 class TestSendChannelAttrs(TestBase):
136
137 def test_id_type(self):
138 _, sch = channels.create()
139 self.assertIsInstance(sch.id, _channels.ChannelID)
140
141 def test_custom_id(self):
142 sch = channels.SendChannel(1)
143 self.assertEqual(sch.id, 1)
144
145 with self.assertRaises(TypeError):
146 channels.SendChannel('1')
147
148 def test_id_readonly(self):
149 sch = channels.SendChannel(1)
150 with self.assertRaises(AttributeError):
151 sch.id = 2
152
153 def test_equality(self):
154 _, ch1 = channels.create()
155 _, ch2 = channels.create()
156 self.assertEqual(ch1, ch1)
157 self.assertNotEqual(ch1, ch2)
158
159 def test_pickle(self):
160 _, ch = channels.create()
161 data = pickle.dumps(ch)
162 unpickled = pickle.loads(data)
163 self.assertEqual(unpickled, ch)
164
165
166 class TestSendRecv(TestBase):
167
168 def test_send_recv_main(self):
169 r, s = channels.create()
170 orig = b'spam'
171 s.send_nowait(orig)
172 obj = r.recv()
173
174 self.assertEqual(obj, orig)
175 self.assertIsNot(obj, orig)
176
177 def test_send_recv_same_interpreter(self):
178 interp = interpreters.create()
179 interp.exec(dedent("""
180 from test.support.interpreters import channels
181 r, s = channels.create()
182 orig = b'spam'
183 s.send_nowait(orig)
184 obj = r.recv()
185 assert obj == orig, 'expected: obj == orig'
186 assert obj is not orig, 'expected: obj is not orig'
187 """))
188
189 @unittest.skip('broken (see BPO-...)')
190 def test_send_recv_different_interpreters(self):
191 r1, s1 = channels.create()
192 r2, s2 = channels.create()
193 orig1 = b'spam'
194 s1.send_nowait(orig1)
195 out = _run_output(
196 interpreters.create(),
197 dedent(f"""
198 obj1 = r.recv()
199 assert obj1 == b'spam', 'expected: obj1 == orig1'
200 # When going to another interpreter we get a copy.
201 assert id(obj1) != {id(orig1)}, 'expected: obj1 is not orig1'
202 orig2 = b'eggs'
203 print(id(orig2))
204 s.send_nowait(orig2)
205 """),
206 channels=dict(r=r1, s=s2),
207 )
208 obj2 = r2.recv()
209
210 self.assertEqual(obj2, b'eggs')
211 self.assertNotEqual(id(obj2), int(out))
212
213 def test_send_recv_different_threads(self):
214 r, s = channels.create()
215
216 def f():
217 while True:
218 try:
219 obj = r.recv()
220 break
221 except channels.ChannelEmptyError:
222 time.sleep(0.1)
223 s.send(obj)
224 t = threading.Thread(target=f)
225 t.start()
226
227 orig = b'spam'
228 s.send(orig)
229 obj = r.recv()
230 t.join()
231
232 self.assertEqual(obj, orig)
233 self.assertIsNot(obj, orig)
234
235 def test_send_recv_nowait_main(self):
236 r, s = channels.create()
237 orig = b'spam'
238 s.send_nowait(orig)
239 obj = r.recv_nowait()
240
241 self.assertEqual(obj, orig)
242 self.assertIsNot(obj, orig)
243
244 def test_send_recv_nowait_main_with_default(self):
245 r, _ = channels.create()
246 obj = r.recv_nowait(None)
247
248 self.assertIsNone(obj)
249
250 def test_send_recv_nowait_same_interpreter(self):
251 interp = interpreters.create()
252 interp.exec(dedent("""
253 from test.support.interpreters import channels
254 r, s = channels.create()
255 orig = b'spam'
256 s.send_nowait(orig)
257 obj = r.recv_nowait()
258 assert obj == orig, 'expected: obj == orig'
259 # When going back to the same interpreter we get the same object.
260 assert obj is not orig, 'expected: obj is not orig'
261 """))
262
263 @unittest.skip('broken (see BPO-...)')
264 def test_send_recv_nowait_different_interpreters(self):
265 r1, s1 = channels.create()
266 r2, s2 = channels.create()
267 orig1 = b'spam'
268 s1.send_nowait(orig1)
269 out = _run_output(
270 interpreters.create(),
271 dedent(f"""
272 obj1 = r.recv_nowait()
273 assert obj1 == b'spam', 'expected: obj1 == orig1'
274 # When going to another interpreter we get a copy.
275 assert id(obj1) != {id(orig1)}, 'expected: obj1 is not orig1'
276 orig2 = b'eggs'
277 print(id(orig2))
278 s.send_nowait(orig2)
279 """),
280 channels=dict(r=r1, s=s2),
281 )
282 obj2 = r2.recv_nowait()
283
284 self.assertEqual(obj2, b'eggs')
285 self.assertNotEqual(id(obj2), int(out))
286
287 def test_recv_timeout(self):
288 r, _ = channels.create()
289 with self.assertRaises(TimeoutError):
290 r.recv(timeout=1)
291
292 def test_recv_channel_does_not_exist(self):
293 ch = channels.RecvChannel(1_000_000)
294 with self.assertRaises(channels.ChannelNotFoundError):
295 ch.recv()
296
297 def test_send_channel_does_not_exist(self):
298 ch = channels.SendChannel(1_000_000)
299 with self.assertRaises(channels.ChannelNotFoundError):
300 ch.send(b'spam')
301
302 def test_recv_nowait_channel_does_not_exist(self):
303 ch = channels.RecvChannel(1_000_000)
304 with self.assertRaises(channels.ChannelNotFoundError):
305 ch.recv_nowait()
306
307 def test_send_nowait_channel_does_not_exist(self):
308 ch = channels.SendChannel(1_000_000)
309 with self.assertRaises(channels.ChannelNotFoundError):
310 ch.send_nowait(b'spam')
311
312 def test_recv_nowait_empty(self):
313 ch, _ = channels.create()
314 with self.assertRaises(channels.ChannelEmptyError):
315 ch.recv_nowait()
316
317 def test_recv_nowait_default(self):
318 default = object()
319 rch, sch = channels.create()
320 obj1 = rch.recv_nowait(default)
321 sch.send_nowait(None)
322 sch.send_nowait(1)
323 sch.send_nowait(b'spam')
324 sch.send_nowait(b'eggs')
325 obj2 = rch.recv_nowait(default)
326 obj3 = rch.recv_nowait(default)
327 obj4 = rch.recv_nowait()
328 obj5 = rch.recv_nowait(default)
329 obj6 = rch.recv_nowait(default)
330
331 self.assertIs(obj1, default)
332 self.assertIs(obj2, None)
333 self.assertEqual(obj3, 1)
334 self.assertEqual(obj4, b'spam')
335 self.assertEqual(obj5, b'eggs')
336 self.assertIs(obj6, default)
337
338 def test_send_buffer(self):
339 buf = bytearray(b'spamspamspam')
340 obj = None
341 rch, sch = channels.create()
342
343 def f():
344 nonlocal obj
345 while True:
346 try:
347 obj = rch.recv()
348 break
349 except channels.ChannelEmptyError:
350 time.sleep(0.1)
351 t = threading.Thread(target=f)
352 t.start()
353
354 sch.send_buffer(buf)
355 t.join()
356
357 self.assertIsNot(obj, buf)
358 self.assertIsInstance(obj, memoryview)
359 self.assertEqual(obj, buf)
360
361 buf[4:8] = b'eggs'
362 self.assertEqual(obj, buf)
363 obj[4:8] = b'ham.'
364 self.assertEqual(obj, buf)
365
366 def test_send_buffer_nowait(self):
367 buf = bytearray(b'spamspamspam')
368 rch, sch = channels.create()
369 sch.send_buffer_nowait(buf)
370 obj = rch.recv()
371
372 self.assertIsNot(obj, buf)
373 self.assertIsInstance(obj, memoryview)
374 self.assertEqual(obj, buf)
375
376 buf[4:8] = b'eggs'
377 self.assertEqual(obj, buf)
378 obj[4:8] = b'ham.'
379 self.assertEqual(obj, buf)
380
381 def test_send_cleared_with_subinterpreter(self):
382 def common(rch, sch, unbound=None, presize=0):
383 if not unbound:
384 extraargs = ''
385 elif unbound is channels.UNBOUND:
386 extraargs = ', unbound=channels.UNBOUND'
387 elif unbound is channels.UNBOUND_ERROR:
388 extraargs = ', unbound=channels.UNBOUND_ERROR'
389 elif unbound is channels.UNBOUND_REMOVE:
390 extraargs = ', unbound=channels.UNBOUND_REMOVE'
391 else:
392 raise NotImplementedError(repr(unbound))
393 interp = interpreters.create()
394
395 _run_output(interp, dedent(f"""
396 from test.support.interpreters import channels
397 sch = channels.SendChannel({sch.id})
398 obj1 = b'spam'
399 obj2 = b'eggs'
400 sch.send_nowait(obj1{extraargs})
401 sch.send_nowait(obj2{extraargs})
402 """))
403 self.assertEqual(
404 _channels.get_count(rch.id),
405 presize + 2,
406 )
407
408 if presize == 0:
409 obj1 = rch.recv()
410 self.assertEqual(obj1, b'spam')
411 self.assertEqual(
412 _channels.get_count(rch.id),
413 presize + 1,
414 )
415
416 return interp
417
418 with self.subTest('default'): # UNBOUND
419 rch, sch = channels.create()
420 interp = common(rch, sch)
421 del interp
422 self.assertEqual(_channels.get_count(rch.id), 1)
423 obj1 = rch.recv()
424 self.assertEqual(_channels.get_count(rch.id), 0)
425 self.assertIs(obj1, channels.UNBOUND)
426 self.assertEqual(_channels.get_count(rch.id), 0)
427 with self.assertRaises(channels.ChannelEmptyError):
428 rch.recv_nowait()
429
430 with self.subTest('UNBOUND'):
431 rch, sch = channels.create()
432 interp = common(rch, sch, channels.UNBOUND)
433 del interp
434 self.assertEqual(_channels.get_count(rch.id), 1)
435 obj1 = rch.recv()
436 self.assertIs(obj1, channels.UNBOUND)
437 self.assertEqual(_channels.get_count(rch.id), 0)
438 with self.assertRaises(channels.ChannelEmptyError):
439 rch.recv_nowait()
440
441 with self.subTest('UNBOUND_ERROR'):
442 rch, sch = channels.create()
443 interp = common(rch, sch, channels.UNBOUND_ERROR)
444
445 del interp
446 self.assertEqual(_channels.get_count(rch.id), 1)
447 with self.assertRaises(channels.ItemInterpreterDestroyed):
448 rch.recv()
449
450 self.assertEqual(_channels.get_count(rch.id), 0)
451 with self.assertRaises(channels.ChannelEmptyError):
452 rch.recv_nowait()
453
454 with self.subTest('UNBOUND_REMOVE'):
455 rch, sch = channels.create()
456
457 interp = common(rch, sch, channels.UNBOUND_REMOVE)
458 del interp
459 self.assertEqual(_channels.get_count(rch.id), 0)
460 with self.assertRaises(channels.ChannelEmptyError):
461 rch.recv_nowait()
462
463 sch.send_nowait(b'ham', unbound=channels.UNBOUND_REMOVE)
464 self.assertEqual(_channels.get_count(rch.id), 1)
465 interp = common(rch, sch, channels.UNBOUND_REMOVE, 1)
466 self.assertEqual(_channels.get_count(rch.id), 3)
467 sch.send_nowait(42, unbound=channels.UNBOUND_REMOVE)
468 self.assertEqual(_channels.get_count(rch.id), 4)
469 del interp
470 self.assertEqual(_channels.get_count(rch.id), 2)
471 obj1 = rch.recv()
472 obj2 = rch.recv()
473 self.assertEqual(obj1, b'ham')
474 self.assertEqual(obj2, 42)
475 self.assertEqual(_channels.get_count(rch.id), 0)
476 with self.assertRaises(channels.ChannelEmptyError):
477 rch.recv_nowait()
478
479 def test_send_cleared_with_subinterpreter_mixed(self):
480 rch, sch = channels.create()
481 interp = interpreters.create()
482
483 # If we don't associate the main interpreter with the channel
484 # then the channel will be automatically closed when interp
485 # is destroyed.
486 sch.send_nowait(None)
487 rch.recv()
488 self.assertEqual(_channels.get_count(rch.id), 0)
489
490 _run_output(interp, dedent(f"""
491 from test.support.interpreters import channels
492 sch = channels.SendChannel({sch.id})
493 sch.send_nowait(1, unbound=channels.UNBOUND)
494 sch.send_nowait(2, unbound=channels.UNBOUND_ERROR)
495 sch.send_nowait(3)
496 sch.send_nowait(4, unbound=channels.UNBOUND_REMOVE)
497 sch.send_nowait(5, unbound=channels.UNBOUND)
498 """))
499 self.assertEqual(_channels.get_count(rch.id), 5)
500
501 del interp
502 self.assertEqual(_channels.get_count(rch.id), 4)
503
504 obj1 = rch.recv()
505 self.assertIs(obj1, channels.UNBOUND)
506 self.assertEqual(_channels.get_count(rch.id), 3)
507
508 with self.assertRaises(channels.ItemInterpreterDestroyed):
509 rch.recv()
510 self.assertEqual(_channels.get_count(rch.id), 2)
511
512 obj2 = rch.recv()
513 self.assertIs(obj2, channels.UNBOUND)
514 self.assertEqual(_channels.get_count(rch.id), 1)
515
516 obj3 = rch.recv()
517 self.assertIs(obj3, channels.UNBOUND)
518 self.assertEqual(_channels.get_count(rch.id), 0)
519
520 def test_send_cleared_with_subinterpreter_multiple(self):
521 rch, sch = channels.create()
522 interp1 = interpreters.create()
523 interp2 = interpreters.create()
524
525 sch.send_nowait(1)
526 _run_output(interp1, dedent(f"""
527 from test.support.interpreters import channels
528 rch = channels.RecvChannel({rch.id})
529 sch = channels.SendChannel({sch.id})
530 obj1 = rch.recv()
531 sch.send_nowait(2, unbound=channels.UNBOUND)
532 sch.send_nowait(obj1, unbound=channels.UNBOUND_REMOVE)
533 """))
534 _run_output(interp2, dedent(f"""
535 from test.support.interpreters import channels
536 rch = channels.RecvChannel({rch.id})
537 sch = channels.SendChannel({sch.id})
538 obj2 = rch.recv()
539 obj1 = rch.recv()
540 """))
541 self.assertEqual(_channels.get_count(rch.id), 0)
542 sch.send_nowait(3)
543 _run_output(interp1, dedent("""
544 sch.send_nowait(4, unbound=channels.UNBOUND)
545 # interp closed here
546 sch.send_nowait(5, unbound=channels.UNBOUND_REMOVE)
547 sch.send_nowait(6, unbound=channels.UNBOUND)
548 """))
549 _run_output(interp2, dedent("""
550 sch.send_nowait(7, unbound=channels.UNBOUND_ERROR)
551 # interp closed here
552 sch.send_nowait(obj1, unbound=channels.UNBOUND_ERROR)
553 sch.send_nowait(obj2, unbound=channels.UNBOUND_REMOVE)
554 sch.send_nowait(8, unbound=channels.UNBOUND)
555 """))
556 _run_output(interp1, dedent("""
557 sch.send_nowait(9, unbound=channels.UNBOUND_REMOVE)
558 sch.send_nowait(10, unbound=channels.UNBOUND)
559 """))
560 self.assertEqual(_channels.get_count(rch.id), 10)
561
562 obj3 = rch.recv()
563 self.assertEqual(obj3, 3)
564 self.assertEqual(_channels.get_count(rch.id), 9)
565
566 obj4 = rch.recv()
567 self.assertEqual(obj4, 4)
568 self.assertEqual(_channels.get_count(rch.id), 8)
569
570 del interp1
571 self.assertEqual(_channels.get_count(rch.id), 6)
572
573 # obj5 was removed
574
575 obj6 = rch.recv()
576 self.assertIs(obj6, channels.UNBOUND)
577 self.assertEqual(_channels.get_count(rch.id), 5)
578
579 obj7 = rch.recv()
580 self.assertEqual(obj7, 7)
581 self.assertEqual(_channels.get_count(rch.id), 4)
582
583 del interp2
584 self.assertEqual(_channels.get_count(rch.id), 3)
585
586 # obj1
587 with self.assertRaises(channels.ItemInterpreterDestroyed):
588 rch.recv()
589 self.assertEqual(_channels.get_count(rch.id), 2)
590
591 # obj2 was removed
592
593 obj8 = rch.recv()
594 self.assertIs(obj8, channels.UNBOUND)
595 self.assertEqual(_channels.get_count(rch.id), 1)
596
597 # obj9 was removed
598
599 obj10 = rch.recv()
600 self.assertIs(obj10, channels.UNBOUND)
601 self.assertEqual(_channels.get_count(rch.id), 0)
602
603
604 if __name__ == '__main__':
605 # Test needs to be a package, so we can do relative imports.
606 unittest.main()