| 1 |
from collections import namedtuple |
| 2 |
import contextlib |
| 3 |
import sys |
| 4 |
from textwrap import dedent |
| 5 |
import threading |
| 6 |
import time |
| 7 |
import unittest |
| 8 |
|
| 9 |
from test.support import import_helper |
| 10 |
|
| 11 |
_channels = import_helper.import_module('_interpchannels') |
| 12 |
from concurrent.interpreters import _crossinterp |
| 13 |
from test.test__interpreters import ( |
| 14 |
_interpreters, |
| 15 |
_run_output, |
| 16 |
clean_up_interpreters, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
REPLACE = _crossinterp._UNBOUND_CONSTANT_TO_FLAG[_crossinterp.UNBOUND] |
| 21 |
|
| 22 |
|
| 23 |
# Additional tests are found in Lib/test/test_interpreters/test_channels.py. |
| 24 |
# New tests should be added there. |
| 25 |
# XXX The tests here should be moved there. See the note under LowLevelTests. |
| 26 |
|
| 27 |
|
| 28 |
################################## |
| 29 |
# helpers |
| 30 |
|
| 31 |
def recv_wait(cid): |
| 32 |
while True: |
| 33 |
try: |
| 34 |
obj, unboundop = _channels.recv(cid) |
| 35 |
except _channels.ChannelEmptyError: |
| 36 |
time.sleep(0.1) |
| 37 |
else: |
| 38 |
assert unboundop is None, repr(unboundop) |
| 39 |
return obj |
| 40 |
|
| 41 |
|
| 42 |
def recv_nowait(cid, *args, unbound=False): |
| 43 |
obj, unboundop = _channels.recv(cid, *args) |
| 44 |
assert (unboundop is None) != unbound, repr(unboundop) |
| 45 |
return obj |
| 46 |
|
| 47 |
|
| 48 |
#@contextmanager |
| 49 |
#def run_threaded(id, source, **shared): |
| 50 |
# def run(): |
| 51 |
# run_interp(id, source, **shared) |
| 52 |
# t = threading.Thread(target=run) |
| 53 |
# t.start() |
| 54 |
# yield |
| 55 |
# t.join() |
| 56 |
|
| 57 |
|
| 58 |
def run_interp(id, source, **shared): |
| 59 |
_run_interp(id, source, shared) |
| 60 |
|
| 61 |
|
| 62 |
def _run_interp(id, source, shared, _mainns={}): |
| 63 |
source = dedent(source) |
| 64 |
main, *_ = _interpreters.get_main() |
| 65 |
if main == id: |
| 66 |
cur, *_ = _interpreters.get_current() |
| 67 |
if cur != main: |
| 68 |
raise RuntimeError |
| 69 |
# XXX Run a func? |
| 70 |
exec(source, _mainns) |
| 71 |
else: |
| 72 |
_interpreters.run_string(id, source, shared) |
| 73 |
|
| 74 |
|
| 75 |
class Interpreter(namedtuple('Interpreter', 'name id')): |
| 76 |
|
| 77 |
@classmethod |
| 78 |
def from_raw(cls, raw): |
| 79 |
if isinstance(raw, cls): |
| 80 |
return raw |
| 81 |
elif isinstance(raw, str): |
| 82 |
return cls(raw) |
| 83 |
else: |
| 84 |
raise NotImplementedError |
| 85 |
|
| 86 |
def __new__(cls, name=None, id=None): |
| 87 |
main, *_ = _interpreters.get_main() |
| 88 |
if id == main: |
| 89 |
if not name: |
| 90 |
name = 'main' |
| 91 |
elif name != 'main': |
| 92 |
raise ValueError( |
| 93 |
'name mismatch (expected "main", got "{}")'.format(name)) |
| 94 |
id = main |
| 95 |
elif id is not None: |
| 96 |
if not name: |
| 97 |
name = 'interp' |
| 98 |
elif name == 'main': |
| 99 |
raise ValueError('name mismatch (unexpected "main")') |
| 100 |
assert isinstance(id, int), repr(id) |
| 101 |
elif not name or name == 'main': |
| 102 |
name = 'main' |
| 103 |
id = main |
| 104 |
else: |
| 105 |
id = _interpreters.create() |
| 106 |
self = super().__new__(cls, name, id) |
| 107 |
return self |
| 108 |
|
| 109 |
|
| 110 |
# XXX expect_channel_closed() is unnecessary once we improve exc propagation. |
| 111 |
|
| 112 |
@contextlib.contextmanager |
| 113 |
def expect_channel_closed(): |
| 114 |
try: |
| 115 |
yield |
| 116 |
except _channels.ChannelClosedError: |
| 117 |
pass |
| 118 |
else: |
| 119 |
assert False, 'channel not closed' |
| 120 |
|
| 121 |
|
| 122 |
class ChannelAction(namedtuple('ChannelAction', 'action end interp')): |
| 123 |
|
| 124 |
def __new__(cls, action, end=None, interp=None): |
| 125 |
if not end: |
| 126 |
end = 'both' |
| 127 |
if not interp: |
| 128 |
interp = 'main' |
| 129 |
self = super().__new__(cls, action, end, interp) |
| 130 |
return self |
| 131 |
|
| 132 |
def __init__(self, *args, **kwargs): |
| 133 |
if self.action == 'use': |
| 134 |
if self.end not in ('same', 'opposite', 'send', 'recv'): |
| 135 |
raise ValueError(self.end) |
| 136 |
elif self.action in ('close', 'force-close'): |
| 137 |
if self.end not in ('both', 'same', 'opposite', 'send', 'recv'): |
| 138 |
raise ValueError(self.end) |
| 139 |
else: |
| 140 |
raise ValueError(self.action) |
| 141 |
if self.interp not in ('main', 'same', 'other', 'extra'): |
| 142 |
raise ValueError(self.interp) |
| 143 |
|
| 144 |
def resolve_end(self, end): |
| 145 |
if self.end == 'same': |
| 146 |
return end |
| 147 |
elif self.end == 'opposite': |
| 148 |
return 'recv' if end == 'send' else 'send' |
| 149 |
else: |
| 150 |
return self.end |
| 151 |
|
| 152 |
def resolve_interp(self, interp, other, extra): |
| 153 |
if self.interp == 'same': |
| 154 |
return interp |
| 155 |
elif self.interp == 'other': |
| 156 |
if other is None: |
| 157 |
raise RuntimeError |
| 158 |
return other |
| 159 |
elif self.interp == 'extra': |
| 160 |
if extra is None: |
| 161 |
raise RuntimeError |
| 162 |
return extra |
| 163 |
elif self.interp == 'main': |
| 164 |
if interp.name == 'main': |
| 165 |
return interp |
| 166 |
elif other and other.name == 'main': |
| 167 |
return other |
| 168 |
else: |
| 169 |
raise RuntimeError |
| 170 |
# Per __init__(), there aren't any others. |
| 171 |
|
| 172 |
|
| 173 |
class ChannelState(namedtuple('ChannelState', 'pending closed')): |
| 174 |
|
| 175 |
def __new__(cls, pending=0, *, closed=False): |
| 176 |
self = super().__new__(cls, pending, closed) |
| 177 |
return self |
| 178 |
|
| 179 |
def incr(self): |
| 180 |
return type(self)(self.pending + 1, closed=self.closed) |
| 181 |
|
| 182 |
def decr(self): |
| 183 |
return type(self)(self.pending - 1, closed=self.closed) |
| 184 |
|
| 185 |
def close(self, *, force=True): |
| 186 |
if self.closed: |
| 187 |
if not force or self.pending == 0: |
| 188 |
return self |
| 189 |
return type(self)(0 if force else self.pending, closed=True) |
| 190 |
|
| 191 |
|
| 192 |
def run_action(cid, action, end, state, *, hideclosed=True): |
| 193 |
if state.closed: |
| 194 |
if action == 'use' and end == 'recv' and state.pending: |
| 195 |
expectfail = False |
| 196 |
else: |
| 197 |
expectfail = True |
| 198 |
else: |
| 199 |
expectfail = False |
| 200 |
|
| 201 |
try: |
| 202 |
result = _run_action(cid, action, end, state) |
| 203 |
except _channels.ChannelClosedError: |
| 204 |
if not hideclosed and not expectfail: |
| 205 |
raise |
| 206 |
result = state.close() |
| 207 |
else: |
| 208 |
if expectfail: |
| 209 |
raise ... # XXX |
| 210 |
return result |
| 211 |
|
| 212 |
|
| 213 |
def _run_action(cid, action, end, state): |
| 214 |
if action == 'use': |
| 215 |
if end == 'send': |
| 216 |
_channels.send(cid, b'spam', blocking=False) |
| 217 |
return state.incr() |
| 218 |
elif end == 'recv': |
| 219 |
if not state.pending: |
| 220 |
try: |
| 221 |
_channels.recv(cid) |
| 222 |
except _channels.ChannelEmptyError: |
| 223 |
return state |
| 224 |
else: |
| 225 |
raise Exception('expected ChannelEmptyError') |
| 226 |
else: |
| 227 |
recv_nowait(cid) |
| 228 |
return state.decr() |
| 229 |
else: |
| 230 |
raise ValueError(end) |
| 231 |
elif action == 'close': |
| 232 |
kwargs = {} |
| 233 |
if end in ('recv', 'send'): |
| 234 |
kwargs[end] = True |
| 235 |
_channels.close(cid, **kwargs) |
| 236 |
return state.close() |
| 237 |
elif action == 'force-close': |
| 238 |
kwargs = { |
| 239 |
'force': True, |
| 240 |
} |
| 241 |
if end in ('recv', 'send'): |
| 242 |
kwargs[end] = True |
| 243 |
_channels.close(cid, **kwargs) |
| 244 |
return state.close(force=True) |
| 245 |
else: |
| 246 |
raise ValueError(action) |
| 247 |
|
| 248 |
|
| 249 |
def clean_up_channels(): |
| 250 |
for cid, _, _ in _channels.list_all(): |
| 251 |
try: |
| 252 |
_channels.destroy(cid) |
| 253 |
except _channels.ChannelNotFoundError: |
| 254 |
pass # already destroyed |
| 255 |
|
| 256 |
|
| 257 |
class TestBase(unittest.TestCase): |
| 258 |
|
| 259 |
def tearDown(self): |
| 260 |
clean_up_channels() |
| 261 |
clean_up_interpreters() |
| 262 |
|
| 263 |
|
| 264 |
################################## |
| 265 |
# channel tests |
| 266 |
|
| 267 |
class ChannelIDTests(TestBase): |
| 268 |
|
| 269 |
def test_default_kwargs(self): |
| 270 |
cid = _channels._channel_id(10, force=True) |
| 271 |
|
| 272 |
self.assertEqual(int(cid), 10) |
| 273 |
self.assertEqual(cid.end, 'both') |
| 274 |
|
| 275 |
def test_with_kwargs(self): |
| 276 |
cid = _channels._channel_id(10, send=True, force=True) |
| 277 |
self.assertEqual(cid.end, 'send') |
| 278 |
|
| 279 |
cid = _channels._channel_id(10, send=True, recv=False, force=True) |
| 280 |
self.assertEqual(cid.end, 'send') |
| 281 |
|
| 282 |
cid = _channels._channel_id(10, recv=True, force=True) |
| 283 |
self.assertEqual(cid.end, 'recv') |
| 284 |
|
| 285 |
cid = _channels._channel_id(10, recv=True, send=False, force=True) |
| 286 |
self.assertEqual(cid.end, 'recv') |
| 287 |
|
| 288 |
cid = _channels._channel_id(10, send=True, recv=True, force=True) |
| 289 |
self.assertEqual(cid.end, 'both') |
| 290 |
|
| 291 |
def test_coerce_id(self): |
| 292 |
class Int(str): |
| 293 |
def __index__(self): |
| 294 |
return 10 |
| 295 |
|
| 296 |
cid = _channels._channel_id(Int(), force=True) |
| 297 |
self.assertEqual(int(cid), 10) |
| 298 |
|
| 299 |
def test_bad_id(self): |
| 300 |
self.assertRaises(TypeError, _channels._channel_id, object()) |
| 301 |
self.assertRaises(TypeError, _channels._channel_id, 10.0) |
| 302 |
self.assertRaises(TypeError, _channels._channel_id, '10') |
| 303 |
self.assertRaises(TypeError, _channels._channel_id, b'10') |
| 304 |
self.assertRaises(ValueError, _channels._channel_id, -1) |
| 305 |
self.assertRaises(OverflowError, _channels._channel_id, 2**64) |
| 306 |
|
| 307 |
def test_bad_kwargs(self): |
| 308 |
with self.assertRaises(ValueError): |
| 309 |
_channels._channel_id(10, send=False, recv=False) |
| 310 |
|
| 311 |
def test_does_not_exist(self): |
| 312 |
cid = _channels.create(REPLACE) |
| 313 |
with self.assertRaises(_channels.ChannelNotFoundError): |
| 314 |
_channels._channel_id(int(cid) + 1) # unforced |
| 315 |
|
| 316 |
def test_str(self): |
| 317 |
cid = _channels._channel_id(10, force=True) |
| 318 |
self.assertEqual(str(cid), '10') |
| 319 |
|
| 320 |
def test_repr(self): |
| 321 |
cid = _channels._channel_id(10, force=True) |
| 322 |
self.assertEqual(repr(cid), 'ChannelID(10)') |
| 323 |
|
| 324 |
cid = _channels._channel_id(10, send=True, force=True) |
| 325 |
self.assertEqual(repr(cid), 'ChannelID(10, send=True)') |
| 326 |
|
| 327 |
cid = _channels._channel_id(10, recv=True, force=True) |
| 328 |
self.assertEqual(repr(cid), 'ChannelID(10, recv=True)') |
| 329 |
|
| 330 |
cid = _channels._channel_id(10, send=True, recv=True, force=True) |
| 331 |
self.assertEqual(repr(cid), 'ChannelID(10)') |
| 332 |
|
| 333 |
def test_equality(self): |
| 334 |
cid1 = _channels.create(REPLACE) |
| 335 |
cid2 = _channels._channel_id(int(cid1)) |
| 336 |
cid3 = _channels.create(REPLACE) |
| 337 |
|
| 338 |
self.assertTrue(cid1 == cid1) |
| 339 |
self.assertTrue(cid1 == cid2) |
| 340 |
self.assertTrue(cid1 == int(cid1)) |
| 341 |
self.assertTrue(int(cid1) == cid1) |
| 342 |
self.assertTrue(cid1 == float(int(cid1))) |
| 343 |
self.assertTrue(float(int(cid1)) == cid1) |
| 344 |
self.assertFalse(cid1 == float(int(cid1)) + 0.1) |
| 345 |
self.assertFalse(cid1 == str(int(cid1))) |
| 346 |
self.assertFalse(cid1 == 2**1000) |
| 347 |
self.assertFalse(cid1 == float('inf')) |
| 348 |
self.assertFalse(cid1 == 'spam') |
| 349 |
self.assertFalse(cid1 == cid3) |
| 350 |
|
| 351 |
self.assertFalse(cid1 != cid1) |
| 352 |
self.assertFalse(cid1 != cid2) |
| 353 |
self.assertTrue(cid1 != cid3) |
| 354 |
|
| 355 |
def test_shareable(self): |
| 356 |
chan = _channels.create(REPLACE) |
| 357 |
|
| 358 |
obj = _channels.create(REPLACE) |
| 359 |
_channels.send(chan, obj, blocking=False) |
| 360 |
got = recv_nowait(chan) |
| 361 |
|
| 362 |
self.assertEqual(got, obj) |
| 363 |
self.assertIs(type(got), type(obj)) |
| 364 |
# XXX Check the following in the channel tests? |
| 365 |
#self.assertIsNot(got, obj) |
| 366 |
|
| 367 |
|
| 368 |
class ChannelTests(TestBase): |
| 369 |
|
| 370 |
def test_create_cid(self): |
| 371 |
cid = _channels.create(REPLACE) |
| 372 |
self.assertIsInstance(cid, _channels.ChannelID) |
| 373 |
|
| 374 |
def test_sequential_ids(self): |
| 375 |
before = [cid for cid, _, _ in _channels.list_all()] |
| 376 |
id1 = _channels.create(REPLACE) |
| 377 |
id2 = _channels.create(REPLACE) |
| 378 |
id3 = _channels.create(REPLACE) |
| 379 |
after = [cid for cid, _, _ in _channels.list_all()] |
| 380 |
|
| 381 |
self.assertEqual(id2, int(id1) + 1) |
| 382 |
self.assertEqual(id3, int(id2) + 1) |
| 383 |
self.assertEqual(set(after) - set(before), {id1, id2, id3}) |
| 384 |
|
| 385 |
def test_channel_list_all_closed(self): |
| 386 |
id1 = _channels.create() |
| 387 |
id2 = _channels.create() |
| 388 |
id3 = _channels.create() |
| 389 |
before = _channels.list_all() |
| 390 |
expected = [info for info in before if info[0] != id2] |
| 391 |
_channels.close(id2, force=True) |
| 392 |
after = _channels.list_all() |
| 393 |
self.assertEqual(set(after), set(expected)) |
| 394 |
self.assertEqual(len(after), len(before) - 1) |
| 395 |
|
| 396 |
def test_channel_list_all_destroyed(self): |
| 397 |
id1 = _channels.create() |
| 398 |
id2 = _channels.create() |
| 399 |
id3 = _channels.create() |
| 400 |
before = _channels.list_all() |
| 401 |
expected = [info for info in before if info[0] != id2] |
| 402 |
_channels.destroy(id2) |
| 403 |
after = _channels.list_all() |
| 404 |
self.assertEqual(set(after), set(expected)) |
| 405 |
self.assertEqual(len(after), len(before) - 1) |
| 406 |
|
| 407 |
def test_channel_list_all_released(self): |
| 408 |
id1 = _channels.create() |
| 409 |
id2 = _channels.create() |
| 410 |
id3 = _channels.create() |
| 411 |
before = _channels.list_all() |
| 412 |
_channels.release(id2, send=True, recv=True) |
| 413 |
after = _channels.list_all() |
| 414 |
self.assertEqual(set(after), set(before)) |
| 415 |
self.assertEqual(len(after), len(before)) |
| 416 |
|
| 417 |
def test_ids_global(self): |
| 418 |
id1 = _interpreters.create() |
| 419 |
out = _run_output(id1, dedent(""" |
| 420 |
import _interpchannels as _channels |
| 421 |
cid = _channels.create(3) |
| 422 |
print(cid) |
| 423 |
""")) |
| 424 |
cid1 = int(out.strip()) |
| 425 |
|
| 426 |
id2 = _interpreters.create() |
| 427 |
out = _run_output(id2, dedent(""" |
| 428 |
import _interpchannels as _channels |
| 429 |
cid = _channels.create(3) |
| 430 |
print(cid) |
| 431 |
""")) |
| 432 |
cid2 = int(out.strip()) |
| 433 |
|
| 434 |
self.assertEqual(cid2, int(cid1) + 1) |
| 435 |
|
| 436 |
def test_channel_list_interpreters_none(self): |
| 437 |
"""Test listing interpreters for a channel with no associations.""" |
| 438 |
# Test for channel with no associated _interpreters. |
| 439 |
cid = _channels.create(REPLACE) |
| 440 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 441 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 442 |
self.assertEqual(send_interps, []) |
| 443 |
self.assertEqual(recv_interps, []) |
| 444 |
|
| 445 |
def test_channel_list_interpreters_basic(self): |
| 446 |
"""Test basic listing channel _interpreters.""" |
| 447 |
interp0, *_ = _interpreters.get_main() |
| 448 |
cid = _channels.create(REPLACE) |
| 449 |
_channels.send(cid, "send", blocking=False) |
| 450 |
# Test for a channel that has one end associated to an interpreter. |
| 451 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 452 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 453 |
self.assertEqual(send_interps, [interp0]) |
| 454 |
self.assertEqual(recv_interps, []) |
| 455 |
|
| 456 |
interp1 = _interpreters.create() |
| 457 |
_run_output(interp1, dedent(f""" |
| 458 |
import _interpchannels as _channels |
| 459 |
_channels.recv({cid}) |
| 460 |
""")) |
| 461 |
# Test for channel that has both ends associated to an interpreter. |
| 462 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 463 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 464 |
self.assertEqual(send_interps, [interp0]) |
| 465 |
self.assertEqual(recv_interps, [interp1]) |
| 466 |
|
| 467 |
def test_channel_list_interpreters_multiple(self): |
| 468 |
"""Test listing interpreters for a channel with many associations.""" |
| 469 |
interp0, *_ = _interpreters.get_main() |
| 470 |
interp1 = _interpreters.create() |
| 471 |
interp2 = _interpreters.create() |
| 472 |
interp3 = _interpreters.create() |
| 473 |
cid = _channels.create(REPLACE) |
| 474 |
|
| 475 |
_channels.send(cid, "send", blocking=False) |
| 476 |
_run_output(interp1, dedent(f""" |
| 477 |
import _interpchannels as _channels |
| 478 |
_channels.send({cid}, "send", blocking=False) |
| 479 |
""")) |
| 480 |
_run_output(interp2, dedent(f""" |
| 481 |
import _interpchannels as _channels |
| 482 |
_channels.recv({cid}) |
| 483 |
""")) |
| 484 |
_run_output(interp3, dedent(f""" |
| 485 |
import _interpchannels as _channels |
| 486 |
_channels.recv({cid}) |
| 487 |
""")) |
| 488 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 489 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 490 |
self.assertEqual(set(send_interps), {interp0, interp1}) |
| 491 |
self.assertEqual(set(recv_interps), {interp2, interp3}) |
| 492 |
|
| 493 |
def test_channel_list_interpreters_destroyed(self): |
| 494 |
"""Test listing channel interpreters with a destroyed interpreter.""" |
| 495 |
interp0, *_ = _interpreters.get_main() |
| 496 |
interp1 = _interpreters.create() |
| 497 |
cid = _channels.create(REPLACE) |
| 498 |
_channels.send(cid, "send", blocking=False) |
| 499 |
_run_output(interp1, dedent(f""" |
| 500 |
import _interpchannels as _channels |
| 501 |
_channels.recv({cid}) |
| 502 |
""")) |
| 503 |
# Should be one interpreter associated with each end. |
| 504 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 505 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 506 |
self.assertEqual(send_interps, [interp0]) |
| 507 |
self.assertEqual(recv_interps, [interp1]) |
| 508 |
|
| 509 |
_interpreters.destroy(interp1) |
| 510 |
# Destroyed interpreter should not be listed. |
| 511 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 512 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 513 |
self.assertEqual(send_interps, [interp0]) |
| 514 |
self.assertEqual(recv_interps, []) |
| 515 |
|
| 516 |
def test_channel_list_interpreters_released(self): |
| 517 |
"""Test listing channel interpreters with a released channel.""" |
| 518 |
# Set up one channel with main interpreter on the send end and two |
| 519 |
# subinterpreters on the receive end. |
| 520 |
interp0, *_ = _interpreters.get_main() |
| 521 |
interp1 = _interpreters.create() |
| 522 |
interp2 = _interpreters.create() |
| 523 |
cid = _channels.create(REPLACE) |
| 524 |
_channels.send(cid, "data", blocking=False) |
| 525 |
_run_output(interp1, dedent(f""" |
| 526 |
import _interpchannels as _channels |
| 527 |
_channels.recv({cid}) |
| 528 |
""")) |
| 529 |
_channels.send(cid, "data", blocking=False) |
| 530 |
_run_output(interp2, dedent(f""" |
| 531 |
import _interpchannels as _channels |
| 532 |
_channels.recv({cid}) |
| 533 |
""")) |
| 534 |
# Check the setup. |
| 535 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 536 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 537 |
self.assertEqual(len(send_interps), 1) |
| 538 |
self.assertEqual(len(recv_interps), 2) |
| 539 |
|
| 540 |
# Release the main interpreter from the send end. |
| 541 |
_channels.release(cid, send=True) |
| 542 |
# Send end should have no associated _interpreters. |
| 543 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 544 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 545 |
self.assertEqual(len(send_interps), 0) |
| 546 |
self.assertEqual(len(recv_interps), 2) |
| 547 |
|
| 548 |
# Release one of the subinterpreters from the receive end. |
| 549 |
_run_output(interp2, dedent(f""" |
| 550 |
import _interpchannels as _channels |
| 551 |
_channels.release({cid}) |
| 552 |
""")) |
| 553 |
# Receive end should have the released interpreter removed. |
| 554 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 555 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 556 |
self.assertEqual(len(send_interps), 0) |
| 557 |
self.assertEqual(recv_interps, [interp1]) |
| 558 |
|
| 559 |
def test_channel_list_interpreters_closed(self): |
| 560 |
"""Test listing channel interpreters with a closed channel.""" |
| 561 |
interp0, *_ = _interpreters.get_main() |
| 562 |
interp1 = _interpreters.create() |
| 563 |
cid = _channels.create(REPLACE) |
| 564 |
# Put something in the channel so that it's not empty. |
| 565 |
_channels.send(cid, "send", blocking=False) |
| 566 |
|
| 567 |
# Check initial state. |
| 568 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 569 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 570 |
self.assertEqual(len(send_interps), 1) |
| 571 |
self.assertEqual(len(recv_interps), 0) |
| 572 |
|
| 573 |
# Force close the channel. |
| 574 |
_channels.close(cid, force=True) |
| 575 |
# Both ends should raise an error. |
| 576 |
with self.assertRaises(_channels.ChannelClosedError): |
| 577 |
_channels.list_interpreters(cid, send=True) |
| 578 |
with self.assertRaises(_channels.ChannelClosedError): |
| 579 |
_channels.list_interpreters(cid, send=False) |
| 580 |
|
| 581 |
def test_channel_list_interpreters_closed_send_end(self): |
| 582 |
"""Test listing channel interpreters with a channel's send end closed.""" |
| 583 |
interp0, *_ = _interpreters.get_main() |
| 584 |
interp1 = _interpreters.create() |
| 585 |
cid = _channels.create(REPLACE) |
| 586 |
# Put something in the channel so that it's not empty. |
| 587 |
_channels.send(cid, "send", blocking=False) |
| 588 |
|
| 589 |
# Check initial state. |
| 590 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 591 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 592 |
self.assertEqual(len(send_interps), 1) |
| 593 |
self.assertEqual(len(recv_interps), 0) |
| 594 |
|
| 595 |
# Close the send end of the channel. |
| 596 |
_channels.close(cid, send=True) |
| 597 |
# Send end should raise an error. |
| 598 |
with self.assertRaises(_channels.ChannelClosedError): |
| 599 |
_channels.list_interpreters(cid, send=True) |
| 600 |
# Receive end should not be closed (since channel is not empty). |
| 601 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 602 |
self.assertEqual(len(recv_interps), 0) |
| 603 |
|
| 604 |
# Close the receive end of the channel from a subinterpreter. |
| 605 |
_run_output(interp1, dedent(f""" |
| 606 |
import _interpchannels as _channels |
| 607 |
_channels.close({cid}, force=True) |
| 608 |
""")) |
| 609 |
return |
| 610 |
# Both ends should raise an error. |
| 611 |
with self.assertRaises(_channels.ChannelClosedError): |
| 612 |
_channels.list_interpreters(cid, send=True) |
| 613 |
with self.assertRaises(_channels.ChannelClosedError): |
| 614 |
_channels.list_interpreters(cid, send=False) |
| 615 |
|
| 616 |
def test_allowed_types(self): |
| 617 |
cid = _channels.create(REPLACE) |
| 618 |
objects = [ |
| 619 |
None, |
| 620 |
'spam', |
| 621 |
b'spam', |
| 622 |
42, |
| 623 |
] |
| 624 |
for obj in objects: |
| 625 |
with self.subTest(obj): |
| 626 |
_channels.send(cid, obj, blocking=False) |
| 627 |
got = recv_nowait(cid) |
| 628 |
|
| 629 |
self.assertEqual(got, obj) |
| 630 |
self.assertIs(type(got), type(obj)) |
| 631 |
# XXX Check the following? |
| 632 |
#self.assertIsNot(got, obj) |
| 633 |
# XXX What about between interpreters? |
| 634 |
|
| 635 |
def test_run_string_arg_unresolved(self): |
| 636 |
cid = _channels.create(REPLACE) |
| 637 |
interp = _interpreters.create() |
| 638 |
|
| 639 |
_interpreters.set___main___attrs(interp, dict(cid=cid.send)) |
| 640 |
out = _run_output(interp, dedent(""" |
| 641 |
import _interpchannels as _channels |
| 642 |
print(cid.end) |
| 643 |
_channels.send(cid, b'spam', blocking=False) |
| 644 |
""")) |
| 645 |
obj = recv_nowait(cid) |
| 646 |
|
| 647 |
self.assertEqual(obj, b'spam') |
| 648 |
self.assertEqual(out.strip(), 'send') |
| 649 |
|
| 650 |
# XXX For now there is no high-level channel into which the |
| 651 |
# sent channel ID can be converted... |
| 652 |
# Note: this test caused crashes on some buildbots (bpo-33615). |
| 653 |
@unittest.skip('disabled until high-level channels exist') |
| 654 |
def test_run_string_arg_resolved(self): |
| 655 |
cid = _channels.create(REPLACE) |
| 656 |
cid = _channels._channel_id(cid, _resolve=True) |
| 657 |
interp = _interpreters.create() |
| 658 |
|
| 659 |
out = _run_output(interp, dedent(""" |
| 660 |
import _interpchannels as _channels |
| 661 |
print(chan.id.end) |
| 662 |
_channels.send(chan.id, b'spam', blocking=False) |
| 663 |
"""), |
| 664 |
dict(chan=cid.send)) |
| 665 |
obj = recv_nowait(cid) |
| 666 |
|
| 667 |
self.assertEqual(obj, b'spam') |
| 668 |
self.assertEqual(out.strip(), 'send') |
| 669 |
|
| 670 |
#------------------- |
| 671 |
# send/recv |
| 672 |
|
| 673 |
def test_send_recv_main(self): |
| 674 |
cid = _channels.create(REPLACE) |
| 675 |
orig = b'spam' |
| 676 |
_channels.send(cid, orig, blocking=False) |
| 677 |
obj = recv_nowait(cid) |
| 678 |
|
| 679 |
self.assertEqual(obj, orig) |
| 680 |
self.assertIsNot(obj, orig) |
| 681 |
|
| 682 |
def test_send_recv_same_interpreter(self): |
| 683 |
id1 = _interpreters.create() |
| 684 |
out = _run_output(id1, dedent(""" |
| 685 |
import _interpchannels as _channels |
| 686 |
cid = _channels.create(REPLACE) |
| 687 |
orig = b'spam' |
| 688 |
_channels.send(cid, orig, blocking=False) |
| 689 |
obj, _ = _channels.recv(cid) |
| 690 |
assert obj is not orig |
| 691 |
assert obj == orig |
| 692 |
""")) |
| 693 |
|
| 694 |
def test_send_recv_different_interpreters(self): |
| 695 |
cid = _channels.create(REPLACE) |
| 696 |
id1 = _interpreters.create() |
| 697 |
out = _run_output(id1, dedent(f""" |
| 698 |
import _interpchannels as _channels |
| 699 |
_channels.send({cid}, b'spam', blocking=False) |
| 700 |
""")) |
| 701 |
obj = recv_nowait(cid) |
| 702 |
|
| 703 |
self.assertEqual(obj, b'spam') |
| 704 |
|
| 705 |
def test_send_recv_different_threads(self): |
| 706 |
cid = _channels.create(REPLACE) |
| 707 |
|
| 708 |
def f(): |
| 709 |
obj = recv_wait(cid) |
| 710 |
_channels.send(cid, obj) |
| 711 |
t = threading.Thread(target=f) |
| 712 |
t.start() |
| 713 |
|
| 714 |
_channels.send(cid, b'spam') |
| 715 |
obj = recv_wait(cid) |
| 716 |
t.join() |
| 717 |
|
| 718 |
self.assertEqual(obj, b'spam') |
| 719 |
|
| 720 |
def test_send_recv_different_interpreters_and_threads(self): |
| 721 |
cid = _channels.create(REPLACE) |
| 722 |
id1 = _interpreters.create() |
| 723 |
out = None |
| 724 |
|
| 725 |
def f(): |
| 726 |
nonlocal out |
| 727 |
out = _run_output(id1, dedent(f""" |
| 728 |
import time |
| 729 |
import _interpchannels as _channels |
| 730 |
while True: |
| 731 |
try: |
| 732 |
obj, _ = _channels.recv({cid}) |
| 733 |
break |
| 734 |
except _channels.ChannelEmptyError: |
| 735 |
time.sleep(0.1) |
| 736 |
assert(obj == b'spam') |
| 737 |
_channels.send({cid}, b'eggs') |
| 738 |
""")) |
| 739 |
t = threading.Thread(target=f) |
| 740 |
t.start() |
| 741 |
|
| 742 |
_channels.send(cid, b'spam') |
| 743 |
obj = recv_wait(cid) |
| 744 |
t.join() |
| 745 |
|
| 746 |
self.assertEqual(obj, b'eggs') |
| 747 |
|
| 748 |
def test_send_not_found(self): |
| 749 |
with self.assertRaises(_channels.ChannelNotFoundError): |
| 750 |
_channels.send(10, b'spam') |
| 751 |
|
| 752 |
def test_recv_not_found(self): |
| 753 |
with self.assertRaises(_channels.ChannelNotFoundError): |
| 754 |
_channels.recv(10) |
| 755 |
|
| 756 |
def test_recv_empty(self): |
| 757 |
cid = _channels.create(REPLACE) |
| 758 |
with self.assertRaises(_channels.ChannelEmptyError): |
| 759 |
_channels.recv(cid) |
| 760 |
|
| 761 |
def test_recv_default(self): |
| 762 |
default = object() |
| 763 |
cid = _channels.create(REPLACE) |
| 764 |
obj1 = recv_nowait(cid, default) |
| 765 |
_channels.send(cid, None, blocking=False) |
| 766 |
_channels.send(cid, 1, blocking=False) |
| 767 |
_channels.send(cid, b'spam', blocking=False) |
| 768 |
_channels.send(cid, b'eggs', blocking=False) |
| 769 |
obj2 = recv_nowait(cid, default) |
| 770 |
obj3 = recv_nowait(cid, default) |
| 771 |
obj4 = recv_nowait(cid) |
| 772 |
obj5 = recv_nowait(cid, default) |
| 773 |
obj6 = recv_nowait(cid, default) |
| 774 |
|
| 775 |
self.assertIs(obj1, default) |
| 776 |
self.assertIs(obj2, None) |
| 777 |
self.assertEqual(obj3, 1) |
| 778 |
self.assertEqual(obj4, b'spam') |
| 779 |
self.assertEqual(obj5, b'eggs') |
| 780 |
self.assertIs(obj6, default) |
| 781 |
|
| 782 |
def test_recv_sending_interp_destroyed(self): |
| 783 |
with self.subTest('closed'): |
| 784 |
cid1 = _channels.create(REPLACE) |
| 785 |
interp = _interpreters.create() |
| 786 |
_interpreters.run_string(interp, dedent(f""" |
| 787 |
import _interpchannels as _channels |
| 788 |
_channels.send({cid1}, b'spam', blocking=False) |
| 789 |
""")) |
| 790 |
_interpreters.destroy(interp) |
| 791 |
|
| 792 |
with self.assertRaisesRegex(RuntimeError, |
| 793 |
f'channel {cid1} is closed'): |
| 794 |
_channels.recv(cid1) |
| 795 |
del cid1 |
| 796 |
with self.subTest('still open'): |
| 797 |
cid2 = _channels.create(REPLACE) |
| 798 |
interp = _interpreters.create() |
| 799 |
_interpreters.run_string(interp, dedent(f""" |
| 800 |
import _interpchannels as _channels |
| 801 |
_channels.send({cid2}, b'spam', blocking=False) |
| 802 |
""")) |
| 803 |
_channels.send(cid2, b'eggs', blocking=False) |
| 804 |
_interpreters.destroy(interp) |
| 805 |
|
| 806 |
recv_nowait(cid2, unbound=True) |
| 807 |
recv_nowait(cid2, unbound=False) |
| 808 |
with self.assertRaisesRegex(RuntimeError, |
| 809 |
f'channel {cid2} is empty'): |
| 810 |
_channels.recv(cid2) |
| 811 |
del cid2 |
| 812 |
|
| 813 |
#------------------- |
| 814 |
# send_buffer |
| 815 |
|
| 816 |
def test_send_buffer(self): |
| 817 |
buf = bytearray(b'spamspamspam') |
| 818 |
cid = _channels.create(REPLACE) |
| 819 |
_channels.send_buffer(cid, buf, blocking=False) |
| 820 |
obj = recv_nowait(cid) |
| 821 |
|
| 822 |
self.assertIsNot(obj, buf) |
| 823 |
self.assertIsInstance(obj, memoryview) |
| 824 |
self.assertEqual(obj, buf) |
| 825 |
|
| 826 |
buf[4:8] = b'eggs' |
| 827 |
self.assertEqual(obj, buf) |
| 828 |
obj[4:8] = b'ham.' |
| 829 |
self.assertEqual(obj, buf) |
| 830 |
|
| 831 |
#------------------- |
| 832 |
# send with waiting |
| 833 |
|
| 834 |
def build_send_waiter(self, obj, *, buffer=False): |
| 835 |
# We want a long enough sleep that send() actually has to wait. |
| 836 |
|
| 837 |
if buffer: |
| 838 |
send = _channels.send_buffer |
| 839 |
else: |
| 840 |
send = _channels.send |
| 841 |
|
| 842 |
cid = _channels.create(REPLACE) |
| 843 |
try: |
| 844 |
started = time.monotonic() |
| 845 |
send(cid, obj, blocking=False) |
| 846 |
stopped = time.monotonic() |
| 847 |
recv_nowait(cid) |
| 848 |
finally: |
| 849 |
_channels.destroy(cid) |
| 850 |
delay = stopped - started # seconds |
| 851 |
delay *= 3 |
| 852 |
|
| 853 |
def wait(): |
| 854 |
time.sleep(delay) |
| 855 |
return wait |
| 856 |
|
| 857 |
def test_send_blocking_waiting(self): |
| 858 |
received = None |
| 859 |
obj = b'spam' |
| 860 |
wait = self.build_send_waiter(obj) |
| 861 |
cid = _channels.create(REPLACE) |
| 862 |
def f(): |
| 863 |
nonlocal received |
| 864 |
wait() |
| 865 |
received = recv_wait(cid) |
| 866 |
t = threading.Thread(target=f) |
| 867 |
t.start() |
| 868 |
_channels.send(cid, obj, blocking=True) |
| 869 |
t.join() |
| 870 |
|
| 871 |
self.assertEqual(received, obj) |
| 872 |
|
| 873 |
def test_send_buffer_blocking_waiting(self): |
| 874 |
received = None |
| 875 |
obj = bytearray(b'spam') |
| 876 |
wait = self.build_send_waiter(obj, buffer=True) |
| 877 |
cid = _channels.create(REPLACE) |
| 878 |
def f(): |
| 879 |
nonlocal received |
| 880 |
wait() |
| 881 |
received = recv_wait(cid) |
| 882 |
t = threading.Thread(target=f) |
| 883 |
t.start() |
| 884 |
_channels.send_buffer(cid, obj, blocking=True) |
| 885 |
t.join() |
| 886 |
|
| 887 |
self.assertEqual(received, obj) |
| 888 |
|
| 889 |
def test_send_blocking_no_wait(self): |
| 890 |
received = None |
| 891 |
obj = b'spam' |
| 892 |
cid = _channels.create(REPLACE) |
| 893 |
def f(): |
| 894 |
nonlocal received |
| 895 |
received = recv_wait(cid) |
| 896 |
t = threading.Thread(target=f) |
| 897 |
t.start() |
| 898 |
_channels.send(cid, obj, blocking=True) |
| 899 |
t.join() |
| 900 |
|
| 901 |
self.assertEqual(received, obj) |
| 902 |
|
| 903 |
def test_send_buffer_blocking_no_wait(self): |
| 904 |
received = None |
| 905 |
obj = bytearray(b'spam') |
| 906 |
cid = _channels.create(REPLACE) |
| 907 |
def f(): |
| 908 |
nonlocal received |
| 909 |
received = recv_wait(cid) |
| 910 |
t = threading.Thread(target=f) |
| 911 |
t.start() |
| 912 |
_channels.send_buffer(cid, obj, blocking=True) |
| 913 |
t.join() |
| 914 |
|
| 915 |
self.assertEqual(received, obj) |
| 916 |
|
| 917 |
def test_send_timeout(self): |
| 918 |
obj = b'spam' |
| 919 |
|
| 920 |
with self.subTest('non-blocking with timeout'): |
| 921 |
cid = _channels.create(REPLACE) |
| 922 |
with self.assertRaises(ValueError): |
| 923 |
_channels.send(cid, obj, blocking=False, timeout=0.1) |
| 924 |
|
| 925 |
with self.subTest('timeout hit'): |
| 926 |
cid = _channels.create(REPLACE) |
| 927 |
with self.assertRaises(TimeoutError): |
| 928 |
_channels.send(cid, obj, blocking=True, timeout=0.1) |
| 929 |
with self.assertRaises(_channels.ChannelEmptyError): |
| 930 |
received = recv_nowait(cid) |
| 931 |
print(repr(received)) |
| 932 |
|
| 933 |
with self.subTest('timeout not hit'): |
| 934 |
cid = _channels.create(REPLACE) |
| 935 |
def f(): |
| 936 |
recv_wait(cid) |
| 937 |
t = threading.Thread(target=f) |
| 938 |
t.start() |
| 939 |
_channels.send(cid, obj, blocking=True, timeout=10) |
| 940 |
t.join() |
| 941 |
|
| 942 |
def test_send_buffer_timeout(self): |
| 943 |
try: |
| 944 |
self._has_run_once_timeout |
| 945 |
except AttributeError: |
| 946 |
# At the moment, this test leaks a few references. |
| 947 |
# It looks like the leak originates with the addition |
| 948 |
# of _channels.send_buffer() (gh-110246), whereas the |
| 949 |
# tests were added afterward. We want this test even |
| 950 |
# if the refleak isn't fixed yet, so we skip here. |
| 951 |
raise unittest.SkipTest('temporarily skipped due to refleaks') |
| 952 |
else: |
| 953 |
self._has_run_once_timeout = True |
| 954 |
|
| 955 |
obj = bytearray(b'spam') |
| 956 |
|
| 957 |
with self.subTest('non-blocking with timeout'): |
| 958 |
cid = _channels.create(REPLACE) |
| 959 |
with self.assertRaises(ValueError): |
| 960 |
_channels.send_buffer(cid, obj, blocking=False, timeout=0.1) |
| 961 |
|
| 962 |
with self.subTest('timeout hit'): |
| 963 |
cid = _channels.create(REPLACE) |
| 964 |
with self.assertRaises(TimeoutError): |
| 965 |
_channels.send_buffer(cid, obj, blocking=True, timeout=0.1) |
| 966 |
with self.assertRaises(_channels.ChannelEmptyError): |
| 967 |
received = recv_nowait(cid) |
| 968 |
print(repr(received)) |
| 969 |
|
| 970 |
with self.subTest('timeout not hit'): |
| 971 |
cid = _channels.create(REPLACE) |
| 972 |
def f(): |
| 973 |
recv_wait(cid) |
| 974 |
t = threading.Thread(target=f) |
| 975 |
t.start() |
| 976 |
_channels.send_buffer(cid, obj, blocking=True, timeout=10) |
| 977 |
t.join() |
| 978 |
|
| 979 |
def test_send_closed_while_waiting(self): |
| 980 |
obj = b'spam' |
| 981 |
wait = self.build_send_waiter(obj) |
| 982 |
|
| 983 |
with self.subTest('without timeout'): |
| 984 |
cid = _channels.create(REPLACE) |
| 985 |
def f(): |
| 986 |
wait() |
| 987 |
_channels.close(cid, force=True) |
| 988 |
t = threading.Thread(target=f) |
| 989 |
t.start() |
| 990 |
with self.assertRaises(_channels.ChannelClosedError): |
| 991 |
_channels.send(cid, obj, blocking=True) |
| 992 |
t.join() |
| 993 |
|
| 994 |
with self.subTest('with timeout'): |
| 995 |
cid = _channels.create(REPLACE) |
| 996 |
def f(): |
| 997 |
wait() |
| 998 |
_channels.close(cid, force=True) |
| 999 |
t = threading.Thread(target=f) |
| 1000 |
t.start() |
| 1001 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1002 |
_channels.send(cid, obj, blocking=True, timeout=30) |
| 1003 |
t.join() |
| 1004 |
|
| 1005 |
def test_send_buffer_closed_while_waiting(self): |
| 1006 |
try: |
| 1007 |
self._has_run_once_closed |
| 1008 |
except AttributeError: |
| 1009 |
# At the moment, this test leaks a few references. |
| 1010 |
# It looks like the leak originates with the addition |
| 1011 |
# of _channels.send_buffer() (gh-110246), whereas the |
| 1012 |
# tests were added afterward. We want this test even |
| 1013 |
# if the refleak isn't fixed yet, so we skip here. |
| 1014 |
raise unittest.SkipTest('temporarily skipped due to refleaks') |
| 1015 |
else: |
| 1016 |
self._has_run_once_closed = True |
| 1017 |
|
| 1018 |
obj = bytearray(b'spam') |
| 1019 |
wait = self.build_send_waiter(obj, buffer=True) |
| 1020 |
|
| 1021 |
with self.subTest('without timeout'): |
| 1022 |
cid = _channels.create(REPLACE) |
| 1023 |
def f(): |
| 1024 |
wait() |
| 1025 |
_channels.close(cid, force=True) |
| 1026 |
t = threading.Thread(target=f) |
| 1027 |
t.start() |
| 1028 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1029 |
_channels.send_buffer(cid, obj, blocking=True) |
| 1030 |
t.join() |
| 1031 |
|
| 1032 |
with self.subTest('with timeout'): |
| 1033 |
cid = _channels.create(REPLACE) |
| 1034 |
def f(): |
| 1035 |
wait() |
| 1036 |
_channels.close(cid, force=True) |
| 1037 |
t = threading.Thread(target=f) |
| 1038 |
t.start() |
| 1039 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1040 |
_channels.send_buffer(cid, obj, blocking=True, timeout=30) |
| 1041 |
t.join() |
| 1042 |
|
| 1043 |
#------------------- |
| 1044 |
# close |
| 1045 |
|
| 1046 |
def test_close_single_user(self): |
| 1047 |
cid = _channels.create(REPLACE) |
| 1048 |
_channels.send(cid, b'spam', blocking=False) |
| 1049 |
recv_nowait(cid) |
| 1050 |
_channels.close(cid) |
| 1051 |
|
| 1052 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1053 |
_channels.send(cid, b'eggs') |
| 1054 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1055 |
_channels.recv(cid) |
| 1056 |
|
| 1057 |
def test_close_multiple_users(self): |
| 1058 |
cid = _channels.create(REPLACE) |
| 1059 |
id1 = _interpreters.create() |
| 1060 |
id2 = _interpreters.create() |
| 1061 |
_interpreters.run_string(id1, dedent(f""" |
| 1062 |
import _interpchannels as _channels |
| 1063 |
_channels.send({cid}, b'spam', blocking=False) |
| 1064 |
""")) |
| 1065 |
_interpreters.run_string(id2, dedent(f""" |
| 1066 |
import _interpchannels as _channels |
| 1067 |
_channels.recv({cid}) |
| 1068 |
""")) |
| 1069 |
_channels.close(cid) |
| 1070 |
|
| 1071 |
excsnap = _interpreters.run_string(id1, dedent(f""" |
| 1072 |
_channels.send({cid}, b'spam') |
| 1073 |
""")) |
| 1074 |
self.assertEqual(excsnap.type.__name__, 'ChannelClosedError') |
| 1075 |
|
| 1076 |
excsnap = _interpreters.run_string(id2, dedent(f""" |
| 1077 |
_channels.send({cid}, b'spam') |
| 1078 |
""")) |
| 1079 |
self.assertEqual(excsnap.type.__name__, 'ChannelClosedError') |
| 1080 |
|
| 1081 |
def test_close_multiple_times(self): |
| 1082 |
cid = _channels.create(REPLACE) |
| 1083 |
_channels.send(cid, b'spam', blocking=False) |
| 1084 |
recv_nowait(cid) |
| 1085 |
_channels.close(cid) |
| 1086 |
|
| 1087 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1088 |
_channels.close(cid) |
| 1089 |
|
| 1090 |
def test_close_empty(self): |
| 1091 |
tests = [ |
| 1092 |
(False, False), |
| 1093 |
(True, False), |
| 1094 |
(False, True), |
| 1095 |
(True, True), |
| 1096 |
] |
| 1097 |
for send, recv in tests: |
| 1098 |
with self.subTest((send, recv)): |
| 1099 |
cid = _channels.create(REPLACE) |
| 1100 |
_channels.send(cid, b'spam', blocking=False) |
| 1101 |
recv_nowait(cid) |
| 1102 |
_channels.close(cid, send=send, recv=recv) |
| 1103 |
|
| 1104 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1105 |
_channels.send(cid, b'eggs') |
| 1106 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1107 |
_channels.recv(cid) |
| 1108 |
|
| 1109 |
def test_close_defaults_with_unused_items(self): |
| 1110 |
cid = _channels.create(REPLACE) |
| 1111 |
_channels.send(cid, b'spam', blocking=False) |
| 1112 |
_channels.send(cid, b'ham', blocking=False) |
| 1113 |
|
| 1114 |
with self.assertRaises(_channels.ChannelNotEmptyError): |
| 1115 |
_channels.close(cid) |
| 1116 |
recv_nowait(cid) |
| 1117 |
_channels.send(cid, b'eggs', blocking=False) |
| 1118 |
|
| 1119 |
def test_close_recv_with_unused_items_unforced(self): |
| 1120 |
cid = _channels.create(REPLACE) |
| 1121 |
_channels.send(cid, b'spam', blocking=False) |
| 1122 |
_channels.send(cid, b'ham', blocking=False) |
| 1123 |
|
| 1124 |
with self.assertRaises(_channels.ChannelNotEmptyError): |
| 1125 |
_channels.close(cid, recv=True) |
| 1126 |
recv_nowait(cid) |
| 1127 |
_channels.send(cid, b'eggs', blocking=False) |
| 1128 |
recv_nowait(cid) |
| 1129 |
recv_nowait(cid) |
| 1130 |
_channels.close(cid, recv=True) |
| 1131 |
|
| 1132 |
def test_close_send_with_unused_items_unforced(self): |
| 1133 |
cid = _channels.create(REPLACE) |
| 1134 |
_channels.send(cid, b'spam', blocking=False) |
| 1135 |
_channels.send(cid, b'ham', blocking=False) |
| 1136 |
_channels.close(cid, send=True) |
| 1137 |
|
| 1138 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1139 |
_channels.send(cid, b'eggs') |
| 1140 |
recv_nowait(cid) |
| 1141 |
recv_nowait(cid) |
| 1142 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1143 |
_channels.recv(cid) |
| 1144 |
|
| 1145 |
def test_close_both_with_unused_items_unforced(self): |
| 1146 |
cid = _channels.create(REPLACE) |
| 1147 |
_channels.send(cid, b'spam', blocking=False) |
| 1148 |
_channels.send(cid, b'ham', blocking=False) |
| 1149 |
|
| 1150 |
with self.assertRaises(_channels.ChannelNotEmptyError): |
| 1151 |
_channels.close(cid, recv=True, send=True) |
| 1152 |
recv_nowait(cid) |
| 1153 |
_channels.send(cid, b'eggs', blocking=False) |
| 1154 |
recv_nowait(cid) |
| 1155 |
recv_nowait(cid) |
| 1156 |
_channels.close(cid, recv=True) |
| 1157 |
|
| 1158 |
def test_close_recv_with_unused_items_forced(self): |
| 1159 |
cid = _channels.create(REPLACE) |
| 1160 |
_channels.send(cid, b'spam', blocking=False) |
| 1161 |
_channels.send(cid, b'ham', blocking=False) |
| 1162 |
_channels.close(cid, recv=True, force=True) |
| 1163 |
|
| 1164 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1165 |
_channels.send(cid, b'eggs') |
| 1166 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1167 |
_channels.recv(cid) |
| 1168 |
|
| 1169 |
def test_close_send_with_unused_items_forced(self): |
| 1170 |
cid = _channels.create(REPLACE) |
| 1171 |
_channels.send(cid, b'spam', blocking=False) |
| 1172 |
_channels.send(cid, b'ham', blocking=False) |
| 1173 |
_channels.close(cid, send=True, force=True) |
| 1174 |
|
| 1175 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1176 |
_channels.send(cid, b'eggs') |
| 1177 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1178 |
_channels.recv(cid) |
| 1179 |
|
| 1180 |
def test_close_both_with_unused_items_forced(self): |
| 1181 |
cid = _channels.create(REPLACE) |
| 1182 |
_channels.send(cid, b'spam', blocking=False) |
| 1183 |
_channels.send(cid, b'ham', blocking=False) |
| 1184 |
_channels.close(cid, send=True, recv=True, force=True) |
| 1185 |
|
| 1186 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1187 |
_channels.send(cid, b'eggs') |
| 1188 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1189 |
_channels.recv(cid) |
| 1190 |
|
| 1191 |
def test_close_never_used(self): |
| 1192 |
cid = _channels.create(REPLACE) |
| 1193 |
_channels.close(cid) |
| 1194 |
|
| 1195 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1196 |
_channels.send(cid, b'spam') |
| 1197 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1198 |
_channels.recv(cid) |
| 1199 |
|
| 1200 |
def test_close_by_unassociated_interp(self): |
| 1201 |
cid = _channels.create(REPLACE) |
| 1202 |
_channels.send(cid, b'spam', blocking=False) |
| 1203 |
interp = _interpreters.create() |
| 1204 |
_interpreters.run_string(interp, dedent(f""" |
| 1205 |
import _interpchannels as _channels |
| 1206 |
_channels.close({cid}, force=True) |
| 1207 |
""")) |
| 1208 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1209 |
_channels.recv(cid) |
| 1210 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1211 |
_channels.close(cid) |
| 1212 |
|
| 1213 |
def test_close_used_multiple_times_by_single_user(self): |
| 1214 |
cid = _channels.create(REPLACE) |
| 1215 |
_channels.send(cid, b'spam', blocking=False) |
| 1216 |
_channels.send(cid, b'spam', blocking=False) |
| 1217 |
_channels.send(cid, b'spam', blocking=False) |
| 1218 |
recv_nowait(cid) |
| 1219 |
_channels.close(cid, force=True) |
| 1220 |
|
| 1221 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1222 |
_channels.send(cid, b'eggs') |
| 1223 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1224 |
_channels.recv(cid) |
| 1225 |
|
| 1226 |
def test_channel_list_interpreters_invalid_channel(self): |
| 1227 |
cid = _channels.create(REPLACE) |
| 1228 |
# Test for invalid channel ID. |
| 1229 |
with self.assertRaises(_channels.ChannelNotFoundError): |
| 1230 |
_channels.list_interpreters(1000, send=True) |
| 1231 |
|
| 1232 |
_channels.close(cid) |
| 1233 |
# Test for a channel that has been closed. |
| 1234 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1235 |
_channels.list_interpreters(cid, send=True) |
| 1236 |
|
| 1237 |
def test_channel_list_interpreters_invalid_args(self): |
| 1238 |
# Tests for invalid arguments passed to the API. |
| 1239 |
cid = _channels.create(REPLACE) |
| 1240 |
with self.assertRaises(TypeError): |
| 1241 |
_channels.list_interpreters(cid) |
| 1242 |
|
| 1243 |
|
| 1244 |
class ChannelReleaseTests(TestBase): |
| 1245 |
|
| 1246 |
# XXX Add more test coverage a la the tests for close(). |
| 1247 |
|
| 1248 |
""" |
| 1249 |
- main / interp / other |
| 1250 |
- run in: current thread / new thread / other thread / different threads |
| 1251 |
- end / opposite |
| 1252 |
- force / no force |
| 1253 |
- used / not used (associated / not associated) |
| 1254 |
- empty / emptied / never emptied / partly emptied |
| 1255 |
- closed / not closed |
| 1256 |
- released / not released |
| 1257 |
- creator (interp) / other |
| 1258 |
- associated interpreter not running |
| 1259 |
- associated interpreter destroyed |
| 1260 |
""" |
| 1261 |
|
| 1262 |
""" |
| 1263 |
use |
| 1264 |
pre-release |
| 1265 |
release |
| 1266 |
after |
| 1267 |
check |
| 1268 |
""" |
| 1269 |
|
| 1270 |
""" |
| 1271 |
release in: main, interp1 |
| 1272 |
creator: same, other (incl. interp2) |
| 1273 |
|
| 1274 |
use: None,send,recv,send/recv in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1275 |
pre-release: None,send,recv,both in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1276 |
pre-release forced: None,send,recv,both in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1277 |
|
| 1278 |
release: same |
| 1279 |
release forced: same |
| 1280 |
|
| 1281 |
use after: None,send,recv,send/recv in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1282 |
release after: None,send,recv,send/recv in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1283 |
check released: send/recv for same/other(incl. interp2) |
| 1284 |
check closed: send/recv for same/other(incl. interp2) |
| 1285 |
""" |
| 1286 |
|
| 1287 |
def test_single_user(self): |
| 1288 |
cid = _channels.create(REPLACE) |
| 1289 |
_channels.send(cid, b'spam', blocking=False) |
| 1290 |
recv_nowait(cid) |
| 1291 |
_channels.release(cid, send=True, recv=True) |
| 1292 |
|
| 1293 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1294 |
_channels.send(cid, b'eggs') |
| 1295 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1296 |
_channels.recv(cid) |
| 1297 |
|
| 1298 |
def test_multiple_users(self): |
| 1299 |
cid = _channels.create(REPLACE) |
| 1300 |
id1 = _interpreters.create() |
| 1301 |
id2 = _interpreters.create() |
| 1302 |
_interpreters.run_string(id1, dedent(f""" |
| 1303 |
import _interpchannels as _channels |
| 1304 |
_channels.send({cid}, b'spam', blocking=False) |
| 1305 |
""")) |
| 1306 |
out = _run_output(id2, dedent(f""" |
| 1307 |
import _interpchannels as _channels |
| 1308 |
obj, _ = _channels.recv({cid}) |
| 1309 |
_channels.release({cid}) |
| 1310 |
print(repr(obj)) |
| 1311 |
""")) |
| 1312 |
_interpreters.run_string(id1, dedent(f""" |
| 1313 |
_channels.release({cid}) |
| 1314 |
""")) |
| 1315 |
|
| 1316 |
self.assertEqual(out.strip(), "b'spam'") |
| 1317 |
|
| 1318 |
def test_no_kwargs(self): |
| 1319 |
cid = _channels.create(REPLACE) |
| 1320 |
_channels.send(cid, b'spam', blocking=False) |
| 1321 |
recv_nowait(cid) |
| 1322 |
_channels.release(cid) |
| 1323 |
|
| 1324 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1325 |
_channels.send(cid, b'eggs') |
| 1326 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1327 |
_channels.recv(cid) |
| 1328 |
|
| 1329 |
def test_multiple_times(self): |
| 1330 |
cid = _channels.create(REPLACE) |
| 1331 |
_channels.send(cid, b'spam', blocking=False) |
| 1332 |
recv_nowait(cid) |
| 1333 |
_channels.release(cid, send=True, recv=True) |
| 1334 |
|
| 1335 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1336 |
_channels.release(cid, send=True, recv=True) |
| 1337 |
|
| 1338 |
def test_with_unused_items(self): |
| 1339 |
cid = _channels.create(REPLACE) |
| 1340 |
_channels.send(cid, b'spam', blocking=False) |
| 1341 |
_channels.send(cid, b'ham', blocking=False) |
| 1342 |
_channels.release(cid, send=True, recv=True) |
| 1343 |
|
| 1344 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1345 |
_channels.recv(cid) |
| 1346 |
|
| 1347 |
def test_never_used(self): |
| 1348 |
cid = _channels.create(REPLACE) |
| 1349 |
_channels.release(cid) |
| 1350 |
|
| 1351 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1352 |
_channels.send(cid, b'spam') |
| 1353 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1354 |
_channels.recv(cid) |
| 1355 |
|
| 1356 |
def test_by_unassociated_interp(self): |
| 1357 |
cid = _channels.create(REPLACE) |
| 1358 |
_channels.send(cid, b'spam', blocking=False) |
| 1359 |
interp = _interpreters.create() |
| 1360 |
_interpreters.run_string(interp, dedent(f""" |
| 1361 |
import _interpchannels as _channels |
| 1362 |
_channels.release({cid}) |
| 1363 |
""")) |
| 1364 |
obj = recv_nowait(cid) |
| 1365 |
_channels.release(cid) |
| 1366 |
|
| 1367 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1368 |
_channels.send(cid, b'eggs') |
| 1369 |
self.assertEqual(obj, b'spam') |
| 1370 |
|
| 1371 |
def test_close_if_unassociated(self): |
| 1372 |
# XXX Something's not right with this test... |
| 1373 |
cid = _channels.create(REPLACE) |
| 1374 |
interp = _interpreters.create() |
| 1375 |
_interpreters.run_string(interp, dedent(f""" |
| 1376 |
import _interpchannels as _channels |
| 1377 |
obj = _channels.send({cid}, b'spam', blocking=False) |
| 1378 |
_channels.release({cid}) |
| 1379 |
""")) |
| 1380 |
|
| 1381 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1382 |
_channels.recv(cid) |
| 1383 |
|
| 1384 |
def test_partially(self): |
| 1385 |
# XXX Is partial close too weird/confusing? |
| 1386 |
cid = _channels.create(REPLACE) |
| 1387 |
_channels.send(cid, None, blocking=False) |
| 1388 |
recv_nowait(cid) |
| 1389 |
_channels.send(cid, b'spam', blocking=False) |
| 1390 |
_channels.release(cid, send=True) |
| 1391 |
obj = recv_nowait(cid) |
| 1392 |
|
| 1393 |
self.assertEqual(obj, b'spam') |
| 1394 |
|
| 1395 |
def test_used_multiple_times_by_single_user(self): |
| 1396 |
cid = _channels.create(REPLACE) |
| 1397 |
_channels.send(cid, b'spam', blocking=False) |
| 1398 |
_channels.send(cid, b'spam', blocking=False) |
| 1399 |
_channels.send(cid, b'spam', blocking=False) |
| 1400 |
recv_nowait(cid) |
| 1401 |
_channels.release(cid, send=True, recv=True) |
| 1402 |
|
| 1403 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1404 |
_channels.send(cid, b'eggs') |
| 1405 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1406 |
_channels.recv(cid) |
| 1407 |
|
| 1408 |
|
| 1409 |
class ChannelCloseFixture(namedtuple('ChannelCloseFixture', |
| 1410 |
'end interp other extra creator')): |
| 1411 |
|
| 1412 |
# Set this to True to avoid creating interpreters, e.g. when |
| 1413 |
# scanning through test permutations without running them. |
| 1414 |
QUICK = False |
| 1415 |
|
| 1416 |
def __new__(cls, end, interp, other, extra, creator): |
| 1417 |
assert end in ('send', 'recv') |
| 1418 |
if cls.QUICK: |
| 1419 |
known = {} |
| 1420 |
else: |
| 1421 |
interp = Interpreter.from_raw(interp) |
| 1422 |
other = Interpreter.from_raw(other) |
| 1423 |
extra = Interpreter.from_raw(extra) |
| 1424 |
known = { |
| 1425 |
interp.name: interp, |
| 1426 |
other.name: other, |
| 1427 |
extra.name: extra, |
| 1428 |
} |
| 1429 |
if not creator: |
| 1430 |
creator = 'same' |
| 1431 |
self = super().__new__(cls, end, interp, other, extra, creator) |
| 1432 |
self._prepped = set() |
| 1433 |
self._state = ChannelState() |
| 1434 |
self._known = known |
| 1435 |
return self |
| 1436 |
|
| 1437 |
@property |
| 1438 |
def state(self): |
| 1439 |
return self._state |
| 1440 |
|
| 1441 |
@property |
| 1442 |
def cid(self): |
| 1443 |
try: |
| 1444 |
return self._cid |
| 1445 |
except AttributeError: |
| 1446 |
creator = self._get_interpreter(self.creator) |
| 1447 |
self._cid = self._new_channel(creator) |
| 1448 |
return self._cid |
| 1449 |
|
| 1450 |
def get_interpreter(self, interp): |
| 1451 |
interp = self._get_interpreter(interp) |
| 1452 |
self._prep_interpreter(interp) |
| 1453 |
return interp |
| 1454 |
|
| 1455 |
def expect_closed_error(self, end=None): |
| 1456 |
if end is None: |
| 1457 |
end = self.end |
| 1458 |
if end == 'recv' and self.state.closed == 'send': |
| 1459 |
return False |
| 1460 |
return bool(self.state.closed) |
| 1461 |
|
| 1462 |
def prep_interpreter(self, interp): |
| 1463 |
self._prep_interpreter(interp) |
| 1464 |
|
| 1465 |
def record_action(self, action, result): |
| 1466 |
self._state = result |
| 1467 |
|
| 1468 |
def clean_up(self): |
| 1469 |
clean_up_interpreters() |
| 1470 |
clean_up_channels() |
| 1471 |
|
| 1472 |
# internal methods |
| 1473 |
|
| 1474 |
def _new_channel(self, creator): |
| 1475 |
if creator.name == 'main': |
| 1476 |
return _channels.create(REPLACE) |
| 1477 |
else: |
| 1478 |
ch = _channels.create(REPLACE) |
| 1479 |
run_interp(creator.id, f""" |
| 1480 |
import _interpreters |
| 1481 |
cid = _xxsubchannels.create() |
| 1482 |
# We purposefully send back an int to avoid tying the |
| 1483 |
# channel to the other interpreter. |
| 1484 |
_xxsubchannels.send({ch}, int(cid), blocking=False) |
| 1485 |
del _interpreters |
| 1486 |
""") |
| 1487 |
self._cid = recv_nowait(ch) |
| 1488 |
return self._cid |
| 1489 |
|
| 1490 |
def _get_interpreter(self, interp): |
| 1491 |
if interp in ('same', 'interp'): |
| 1492 |
return self.interp |
| 1493 |
elif interp == 'other': |
| 1494 |
return self.other |
| 1495 |
elif interp == 'extra': |
| 1496 |
return self.extra |
| 1497 |
else: |
| 1498 |
name = interp |
| 1499 |
try: |
| 1500 |
interp = self._known[name] |
| 1501 |
except KeyError: |
| 1502 |
interp = self._known[name] = Interpreter(name) |
| 1503 |
return interp |
| 1504 |
|
| 1505 |
def _prep_interpreter(self, interp): |
| 1506 |
if interp.id in self._prepped: |
| 1507 |
return |
| 1508 |
self._prepped.add(interp.id) |
| 1509 |
if interp.name == 'main': |
| 1510 |
return |
| 1511 |
run_interp(interp.id, f""" |
| 1512 |
import _interpchannels as channels |
| 1513 |
import test.test__interpchannels as helpers |
| 1514 |
ChannelState = helpers.ChannelState |
| 1515 |
try: |
| 1516 |
cid |
| 1517 |
except NameError: |
| 1518 |
cid = _channels._channel_id({self.cid}) |
| 1519 |
""") |
| 1520 |
|
| 1521 |
|
| 1522 |
@unittest.skip('these tests take several hours to run') |
| 1523 |
class ExhaustiveChannelTests(TestBase): |
| 1524 |
|
| 1525 |
""" |
| 1526 |
- main / interp / other |
| 1527 |
- run in: current thread / new thread / other thread / different threads |
| 1528 |
- end / opposite |
| 1529 |
- force / no force |
| 1530 |
- used / not used (associated / not associated) |
| 1531 |
- empty / emptied / never emptied / partly emptied |
| 1532 |
- closed / not closed |
| 1533 |
- released / not released |
| 1534 |
- creator (interp) / other |
| 1535 |
- associated interpreter not running |
| 1536 |
- associated interpreter destroyed |
| 1537 |
|
| 1538 |
- close after unbound |
| 1539 |
""" |
| 1540 |
|
| 1541 |
""" |
| 1542 |
use |
| 1543 |
pre-close |
| 1544 |
close |
| 1545 |
after |
| 1546 |
check |
| 1547 |
""" |
| 1548 |
|
| 1549 |
""" |
| 1550 |
close in: main, interp1 |
| 1551 |
creator: same, other, extra |
| 1552 |
|
| 1553 |
use: None,send,recv,send/recv in None,same,other,same+other,all |
| 1554 |
pre-close: None,send,recv in None,same,other,same+other,all |
| 1555 |
pre-close forced: None,send,recv in None,same,other,same+other,all |
| 1556 |
|
| 1557 |
close: same |
| 1558 |
close forced: same |
| 1559 |
|
| 1560 |
use after: None,send,recv,send/recv in None,same,other,extra,same+other,all |
| 1561 |
close after: None,send,recv,send/recv in None,same,other,extra,same+other,all |
| 1562 |
check closed: send/recv for same/other(incl. interp2) |
| 1563 |
""" |
| 1564 |
|
| 1565 |
def iter_action_sets(self): |
| 1566 |
# - used / not used (associated / not associated) |
| 1567 |
# - empty / emptied / never emptied / partly emptied |
| 1568 |
# - closed / not closed |
| 1569 |
# - released / not released |
| 1570 |
|
| 1571 |
# never used |
| 1572 |
yield [] |
| 1573 |
|
| 1574 |
# only pre-closed (and possible used after) |
| 1575 |
for closeactions in self._iter_close_action_sets('same', 'other'): |
| 1576 |
yield closeactions |
| 1577 |
for postactions in self._iter_post_close_action_sets(): |
| 1578 |
yield closeactions + postactions |
| 1579 |
for closeactions in self._iter_close_action_sets('other', 'extra'): |
| 1580 |
yield closeactions |
| 1581 |
for postactions in self._iter_post_close_action_sets(): |
| 1582 |
yield closeactions + postactions |
| 1583 |
|
| 1584 |
# used |
| 1585 |
for useactions in self._iter_use_action_sets('same', 'other'): |
| 1586 |
yield useactions |
| 1587 |
for closeactions in self._iter_close_action_sets('same', 'other'): |
| 1588 |
actions = useactions + closeactions |
| 1589 |
yield actions |
| 1590 |
for postactions in self._iter_post_close_action_sets(): |
| 1591 |
yield actions + postactions |
| 1592 |
for closeactions in self._iter_close_action_sets('other', 'extra'): |
| 1593 |
actions = useactions + closeactions |
| 1594 |
yield actions |
| 1595 |
for postactions in self._iter_post_close_action_sets(): |
| 1596 |
yield actions + postactions |
| 1597 |
for useactions in self._iter_use_action_sets('other', 'extra'): |
| 1598 |
yield useactions |
| 1599 |
for closeactions in self._iter_close_action_sets('same', 'other'): |
| 1600 |
actions = useactions + closeactions |
| 1601 |
yield actions |
| 1602 |
for postactions in self._iter_post_close_action_sets(): |
| 1603 |
yield actions + postactions |
| 1604 |
for closeactions in self._iter_close_action_sets('other', 'extra'): |
| 1605 |
actions = useactions + closeactions |
| 1606 |
yield actions |
| 1607 |
for postactions in self._iter_post_close_action_sets(): |
| 1608 |
yield actions + postactions |
| 1609 |
|
| 1610 |
def _iter_use_action_sets(self, interp1, interp2): |
| 1611 |
interps = (interp1, interp2) |
| 1612 |
|
| 1613 |
# only recv end used |
| 1614 |
yield [ |
| 1615 |
ChannelAction('use', 'recv', interp1), |
| 1616 |
] |
| 1617 |
yield [ |
| 1618 |
ChannelAction('use', 'recv', interp2), |
| 1619 |
] |
| 1620 |
yield [ |
| 1621 |
ChannelAction('use', 'recv', interp1), |
| 1622 |
ChannelAction('use', 'recv', interp2), |
| 1623 |
] |
| 1624 |
|
| 1625 |
# never emptied |
| 1626 |
yield [ |
| 1627 |
ChannelAction('use', 'send', interp1), |
| 1628 |
] |
| 1629 |
yield [ |
| 1630 |
ChannelAction('use', 'send', interp2), |
| 1631 |
] |
| 1632 |
yield [ |
| 1633 |
ChannelAction('use', 'send', interp1), |
| 1634 |
ChannelAction('use', 'send', interp2), |
| 1635 |
] |
| 1636 |
|
| 1637 |
# partially emptied |
| 1638 |
for interp1 in interps: |
| 1639 |
for interp2 in interps: |
| 1640 |
for interp3 in interps: |
| 1641 |
yield [ |
| 1642 |
ChannelAction('use', 'send', interp1), |
| 1643 |
ChannelAction('use', 'send', interp2), |
| 1644 |
ChannelAction('use', 'recv', interp3), |
| 1645 |
] |
| 1646 |
|
| 1647 |
# fully emptied |
| 1648 |
for interp1 in interps: |
| 1649 |
for interp2 in interps: |
| 1650 |
for interp3 in interps: |
| 1651 |
for interp4 in interps: |
| 1652 |
yield [ |
| 1653 |
ChannelAction('use', 'send', interp1), |
| 1654 |
ChannelAction('use', 'send', interp2), |
| 1655 |
ChannelAction('use', 'recv', interp3), |
| 1656 |
ChannelAction('use', 'recv', interp4), |
| 1657 |
] |
| 1658 |
|
| 1659 |
def _iter_close_action_sets(self, interp1, interp2): |
| 1660 |
ends = ('recv', 'send') |
| 1661 |
interps = (interp1, interp2) |
| 1662 |
for force in (True, False): |
| 1663 |
op = 'force-close' if force else 'close' |
| 1664 |
for interp in interps: |
| 1665 |
for end in ends: |
| 1666 |
yield [ |
| 1667 |
ChannelAction(op, end, interp), |
| 1668 |
] |
| 1669 |
for recvop in ('close', 'force-close'): |
| 1670 |
for sendop in ('close', 'force-close'): |
| 1671 |
for recv in interps: |
| 1672 |
for send in interps: |
| 1673 |
yield [ |
| 1674 |
ChannelAction(recvop, 'recv', recv), |
| 1675 |
ChannelAction(sendop, 'send', send), |
| 1676 |
] |
| 1677 |
|
| 1678 |
def _iter_post_close_action_sets(self): |
| 1679 |
for interp in ('same', 'extra', 'other'): |
| 1680 |
yield [ |
| 1681 |
ChannelAction('use', 'recv', interp), |
| 1682 |
] |
| 1683 |
yield [ |
| 1684 |
ChannelAction('use', 'send', interp), |
| 1685 |
] |
| 1686 |
|
| 1687 |
def run_actions(self, fix, actions): |
| 1688 |
for action in actions: |
| 1689 |
self.run_action(fix, action) |
| 1690 |
|
| 1691 |
def run_action(self, fix, action, *, hideclosed=True): |
| 1692 |
end = action.resolve_end(fix.end) |
| 1693 |
interp = action.resolve_interp(fix.interp, fix.other, fix.extra) |
| 1694 |
fix.prep_interpreter(interp) |
| 1695 |
if interp.name == 'main': |
| 1696 |
result = run_action( |
| 1697 |
fix.cid, |
| 1698 |
action.action, |
| 1699 |
end, |
| 1700 |
fix.state, |
| 1701 |
hideclosed=hideclosed, |
| 1702 |
) |
| 1703 |
fix.record_action(action, result) |
| 1704 |
else: |
| 1705 |
_cid = _channels.create(REPLACE) |
| 1706 |
run_interp(interp.id, f""" |
| 1707 |
result = helpers.run_action( |
| 1708 |
{fix.cid}, |
| 1709 |
{repr(action.action)}, |
| 1710 |
{repr(end)}, |
| 1711 |
{repr(fix.state)}, |
| 1712 |
hideclosed={hideclosed}, |
| 1713 |
) |
| 1714 |
_channels.send({_cid}, result.pending.to_bytes(1, 'little'), blocking=False) |
| 1715 |
_channels.send({_cid}, b'X' if result.closed else b'', blocking=False) |
| 1716 |
""") |
| 1717 |
result = ChannelState( |
| 1718 |
pending=int.from_bytes(recv_nowait(_cid), 'little'), |
| 1719 |
closed=bool(recv_nowait(_cid)), |
| 1720 |
) |
| 1721 |
fix.record_action(action, result) |
| 1722 |
|
| 1723 |
def iter_fixtures(self): |
| 1724 |
# XXX threads? |
| 1725 |
interpreters = [ |
| 1726 |
('main', 'interp', 'extra'), |
| 1727 |
('interp', 'main', 'extra'), |
| 1728 |
('interp1', 'interp2', 'extra'), |
| 1729 |
('interp1', 'interp2', 'main'), |
| 1730 |
] |
| 1731 |
for interp, other, extra in interpreters: |
| 1732 |
for creator in ('same', 'other', 'creator'): |
| 1733 |
for end in ('send', 'recv'): |
| 1734 |
yield ChannelCloseFixture(end, interp, other, extra, creator) |
| 1735 |
|
| 1736 |
def _close(self, fix, *, force): |
| 1737 |
op = 'force-close' if force else 'close' |
| 1738 |
close = ChannelAction(op, fix.end, 'same') |
| 1739 |
if not fix.expect_closed_error(): |
| 1740 |
self.run_action(fix, close, hideclosed=False) |
| 1741 |
else: |
| 1742 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1743 |
self.run_action(fix, close, hideclosed=False) |
| 1744 |
|
| 1745 |
def _assert_closed_in_interp(self, fix, interp=None): |
| 1746 |
if interp is None or interp.name == 'main': |
| 1747 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1748 |
_channels.recv(fix.cid) |
| 1749 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1750 |
_channels.send(fix.cid, b'spam') |
| 1751 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1752 |
_channels.close(fix.cid) |
| 1753 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1754 |
_channels.close(fix.cid, force=True) |
| 1755 |
else: |
| 1756 |
run_interp(interp.id, """ |
| 1757 |
with helpers.expect_channel_closed(): |
| 1758 |
_channels.recv(cid) |
| 1759 |
""") |
| 1760 |
run_interp(interp.id, """ |
| 1761 |
with helpers.expect_channel_closed(): |
| 1762 |
_channels.send(cid, b'spam', blocking=False) |
| 1763 |
""") |
| 1764 |
run_interp(interp.id, """ |
| 1765 |
with helpers.expect_channel_closed(): |
| 1766 |
_channels.close(cid) |
| 1767 |
""") |
| 1768 |
run_interp(interp.id, """ |
| 1769 |
with helpers.expect_channel_closed(): |
| 1770 |
_channels.close(cid, force=True) |
| 1771 |
""") |
| 1772 |
|
| 1773 |
def _assert_closed(self, fix): |
| 1774 |
self.assertTrue(fix.state.closed) |
| 1775 |
|
| 1776 |
for _ in range(fix.state.pending): |
| 1777 |
recv_nowait(fix.cid) |
| 1778 |
self._assert_closed_in_interp(fix) |
| 1779 |
|
| 1780 |
for interp in ('same', 'other'): |
| 1781 |
interp = fix.get_interpreter(interp) |
| 1782 |
if interp.name == 'main': |
| 1783 |
continue |
| 1784 |
self._assert_closed_in_interp(fix, interp) |
| 1785 |
|
| 1786 |
interp = fix.get_interpreter('fresh') |
| 1787 |
self._assert_closed_in_interp(fix, interp) |
| 1788 |
|
| 1789 |
def _iter_close_tests(self, verbose=False): |
| 1790 |
i = 0 |
| 1791 |
for actions in self.iter_action_sets(): |
| 1792 |
print() |
| 1793 |
for fix in self.iter_fixtures(): |
| 1794 |
i += 1 |
| 1795 |
if i > 1000: |
| 1796 |
return |
| 1797 |
if verbose: |
| 1798 |
if (i - 1) % 6 == 0: |
| 1799 |
print() |
| 1800 |
print(i, fix, '({} actions)'.format(len(actions))) |
| 1801 |
else: |
| 1802 |
if (i - 1) % 6 == 0: |
| 1803 |
print(' ', end='') |
| 1804 |
print('.', end=''); sys.stdout.flush() |
| 1805 |
yield i, fix, actions |
| 1806 |
if verbose: |
| 1807 |
print('---') |
| 1808 |
print() |
| 1809 |
|
| 1810 |
# This is useful for scanning through the possible tests. |
| 1811 |
def _skim_close_tests(self): |
| 1812 |
ChannelCloseFixture.QUICK = True |
| 1813 |
for i, fix, actions in self._iter_close_tests(): |
| 1814 |
pass |
| 1815 |
|
| 1816 |
def test_close(self): |
| 1817 |
for i, fix, actions in self._iter_close_tests(): |
| 1818 |
with self.subTest('{} {} {}'.format(i, fix, actions)): |
| 1819 |
fix.prep_interpreter(fix.interp) |
| 1820 |
self.run_actions(fix, actions) |
| 1821 |
|
| 1822 |
self._close(fix, force=False) |
| 1823 |
|
| 1824 |
self._assert_closed(fix) |
| 1825 |
# XXX Things slow down if we have too many interpreters. |
| 1826 |
fix.clean_up() |
| 1827 |
|
| 1828 |
def test_force_close(self): |
| 1829 |
for i, fix, actions in self._iter_close_tests(): |
| 1830 |
with self.subTest('{} {} {}'.format(i, fix, actions)): |
| 1831 |
fix.prep_interpreter(fix.interp) |
| 1832 |
self.run_actions(fix, actions) |
| 1833 |
|
| 1834 |
self._close(fix, force=True) |
| 1835 |
|
| 1836 |
self._assert_closed(fix) |
| 1837 |
# XXX Things slow down if we have too many interpreters. |
| 1838 |
fix.clean_up() |
| 1839 |
|
| 1840 |
|
| 1841 |
if __name__ == '__main__': |
| 1842 |
unittest.main() |