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