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