| 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 test.support.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_ids_global(self): |
| 386 |
id1 = _interpreters.create() |
| 387 |
out = _run_output(id1, dedent(""" |
| 388 |
import _interpchannels as _channels |
| 389 |
cid = _channels.create(3) |
| 390 |
print(cid) |
| 391 |
""")) |
| 392 |
cid1 = int(out.strip()) |
| 393 |
|
| 394 |
id2 = _interpreters.create() |
| 395 |
out = _run_output(id2, dedent(""" |
| 396 |
import _interpchannels as _channels |
| 397 |
cid = _channels.create(3) |
| 398 |
print(cid) |
| 399 |
""")) |
| 400 |
cid2 = int(out.strip()) |
| 401 |
|
| 402 |
self.assertEqual(cid2, int(cid1) + 1) |
| 403 |
|
| 404 |
def test_channel_list_interpreters_none(self): |
| 405 |
"""Test listing interpreters for a channel with no associations.""" |
| 406 |
# Test for channel with no associated _interpreters. |
| 407 |
cid = _channels.create(REPLACE) |
| 408 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 409 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 410 |
self.assertEqual(send_interps, []) |
| 411 |
self.assertEqual(recv_interps, []) |
| 412 |
|
| 413 |
def test_channel_list_interpreters_basic(self): |
| 414 |
"""Test basic listing channel _interpreters.""" |
| 415 |
interp0, *_ = _interpreters.get_main() |
| 416 |
cid = _channels.create(REPLACE) |
| 417 |
_channels.send(cid, "send", blocking=False) |
| 418 |
# Test for a channel that has one end associated to an interpreter. |
| 419 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 420 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 421 |
self.assertEqual(send_interps, [interp0]) |
| 422 |
self.assertEqual(recv_interps, []) |
| 423 |
|
| 424 |
interp1 = _interpreters.create() |
| 425 |
_run_output(interp1, dedent(f""" |
| 426 |
import _interpchannels as _channels |
| 427 |
_channels.recv({cid}) |
| 428 |
""")) |
| 429 |
# Test for channel that has both ends associated to an interpreter. |
| 430 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 431 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 432 |
self.assertEqual(send_interps, [interp0]) |
| 433 |
self.assertEqual(recv_interps, [interp1]) |
| 434 |
|
| 435 |
def test_channel_list_interpreters_multiple(self): |
| 436 |
"""Test listing interpreters for a channel with many associations.""" |
| 437 |
interp0, *_ = _interpreters.get_main() |
| 438 |
interp1 = _interpreters.create() |
| 439 |
interp2 = _interpreters.create() |
| 440 |
interp3 = _interpreters.create() |
| 441 |
cid = _channels.create(REPLACE) |
| 442 |
|
| 443 |
_channels.send(cid, "send", blocking=False) |
| 444 |
_run_output(interp1, dedent(f""" |
| 445 |
import _interpchannels as _channels |
| 446 |
_channels.send({cid}, "send", blocking=False) |
| 447 |
""")) |
| 448 |
_run_output(interp2, dedent(f""" |
| 449 |
import _interpchannels as _channels |
| 450 |
_channels.recv({cid}) |
| 451 |
""")) |
| 452 |
_run_output(interp3, dedent(f""" |
| 453 |
import _interpchannels as _channels |
| 454 |
_channels.recv({cid}) |
| 455 |
""")) |
| 456 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 457 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 458 |
self.assertEqual(set(send_interps), {interp0, interp1}) |
| 459 |
self.assertEqual(set(recv_interps), {interp2, interp3}) |
| 460 |
|
| 461 |
def test_channel_list_interpreters_destroyed(self): |
| 462 |
"""Test listing channel interpreters with a destroyed interpreter.""" |
| 463 |
interp0, *_ = _interpreters.get_main() |
| 464 |
interp1 = _interpreters.create() |
| 465 |
cid = _channels.create(REPLACE) |
| 466 |
_channels.send(cid, "send", blocking=False) |
| 467 |
_run_output(interp1, dedent(f""" |
| 468 |
import _interpchannels as _channels |
| 469 |
_channels.recv({cid}) |
| 470 |
""")) |
| 471 |
# Should be one interpreter associated with each end. |
| 472 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 473 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 474 |
self.assertEqual(send_interps, [interp0]) |
| 475 |
self.assertEqual(recv_interps, [interp1]) |
| 476 |
|
| 477 |
_interpreters.destroy(interp1) |
| 478 |
# Destroyed interpreter should not be listed. |
| 479 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 480 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 481 |
self.assertEqual(send_interps, [interp0]) |
| 482 |
self.assertEqual(recv_interps, []) |
| 483 |
|
| 484 |
def test_channel_list_interpreters_released(self): |
| 485 |
"""Test listing channel interpreters with a released channel.""" |
| 486 |
# Set up one channel with main interpreter on the send end and two |
| 487 |
# subinterpreters on the receive end. |
| 488 |
interp0, *_ = _interpreters.get_main() |
| 489 |
interp1 = _interpreters.create() |
| 490 |
interp2 = _interpreters.create() |
| 491 |
cid = _channels.create(REPLACE) |
| 492 |
_channels.send(cid, "data", blocking=False) |
| 493 |
_run_output(interp1, dedent(f""" |
| 494 |
import _interpchannels as _channels |
| 495 |
_channels.recv({cid}) |
| 496 |
""")) |
| 497 |
_channels.send(cid, "data", blocking=False) |
| 498 |
_run_output(interp2, dedent(f""" |
| 499 |
import _interpchannels as _channels |
| 500 |
_channels.recv({cid}) |
| 501 |
""")) |
| 502 |
# Check the setup. |
| 503 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 504 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 505 |
self.assertEqual(len(send_interps), 1) |
| 506 |
self.assertEqual(len(recv_interps), 2) |
| 507 |
|
| 508 |
# Release the main interpreter from the send end. |
| 509 |
_channels.release(cid, send=True) |
| 510 |
# Send end should have no associated _interpreters. |
| 511 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 512 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 513 |
self.assertEqual(len(send_interps), 0) |
| 514 |
self.assertEqual(len(recv_interps), 2) |
| 515 |
|
| 516 |
# Release one of the subinterpreters from the receive end. |
| 517 |
_run_output(interp2, dedent(f""" |
| 518 |
import _interpchannels as _channels |
| 519 |
_channels.release({cid}) |
| 520 |
""")) |
| 521 |
# Receive end should have the released interpreter removed. |
| 522 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 523 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 524 |
self.assertEqual(len(send_interps), 0) |
| 525 |
self.assertEqual(recv_interps, [interp1]) |
| 526 |
|
| 527 |
def test_channel_list_interpreters_closed(self): |
| 528 |
"""Test listing channel interpreters with a closed channel.""" |
| 529 |
interp0, *_ = _interpreters.get_main() |
| 530 |
interp1 = _interpreters.create() |
| 531 |
cid = _channels.create(REPLACE) |
| 532 |
# Put something in the channel so that it's not empty. |
| 533 |
_channels.send(cid, "send", blocking=False) |
| 534 |
|
| 535 |
# Check initial state. |
| 536 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 537 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 538 |
self.assertEqual(len(send_interps), 1) |
| 539 |
self.assertEqual(len(recv_interps), 0) |
| 540 |
|
| 541 |
# Force close the channel. |
| 542 |
_channels.close(cid, force=True) |
| 543 |
# Both ends should raise an error. |
| 544 |
with self.assertRaises(_channels.ChannelClosedError): |
| 545 |
_channels.list_interpreters(cid, send=True) |
| 546 |
with self.assertRaises(_channels.ChannelClosedError): |
| 547 |
_channels.list_interpreters(cid, send=False) |
| 548 |
|
| 549 |
def test_channel_list_interpreters_closed_send_end(self): |
| 550 |
"""Test listing channel interpreters with a channel's send end closed.""" |
| 551 |
interp0, *_ = _interpreters.get_main() |
| 552 |
interp1 = _interpreters.create() |
| 553 |
cid = _channels.create(REPLACE) |
| 554 |
# Put something in the channel so that it's not empty. |
| 555 |
_channels.send(cid, "send", blocking=False) |
| 556 |
|
| 557 |
# Check initial state. |
| 558 |
send_interps = _channels.list_interpreters(cid, send=True) |
| 559 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 560 |
self.assertEqual(len(send_interps), 1) |
| 561 |
self.assertEqual(len(recv_interps), 0) |
| 562 |
|
| 563 |
# Close the send end of the channel. |
| 564 |
_channels.close(cid, send=True) |
| 565 |
# Send end should raise an error. |
| 566 |
with self.assertRaises(_channels.ChannelClosedError): |
| 567 |
_channels.list_interpreters(cid, send=True) |
| 568 |
# Receive end should not be closed (since channel is not empty). |
| 569 |
recv_interps = _channels.list_interpreters(cid, send=False) |
| 570 |
self.assertEqual(len(recv_interps), 0) |
| 571 |
|
| 572 |
# Close the receive end of the channel from a subinterpreter. |
| 573 |
_run_output(interp1, dedent(f""" |
| 574 |
import _interpchannels as _channels |
| 575 |
_channels.close({cid}, force=True) |
| 576 |
""")) |
| 577 |
return |
| 578 |
# Both ends should raise an error. |
| 579 |
with self.assertRaises(_channels.ChannelClosedError): |
| 580 |
_channels.list_interpreters(cid, send=True) |
| 581 |
with self.assertRaises(_channels.ChannelClosedError): |
| 582 |
_channels.list_interpreters(cid, send=False) |
| 583 |
|
| 584 |
def test_allowed_types(self): |
| 585 |
cid = _channels.create(REPLACE) |
| 586 |
objects = [ |
| 587 |
None, |
| 588 |
'spam', |
| 589 |
b'spam', |
| 590 |
42, |
| 591 |
] |
| 592 |
for obj in objects: |
| 593 |
with self.subTest(obj): |
| 594 |
_channels.send(cid, obj, blocking=False) |
| 595 |
got = recv_nowait(cid) |
| 596 |
|
| 597 |
self.assertEqual(got, obj) |
| 598 |
self.assertIs(type(got), type(obj)) |
| 599 |
# XXX Check the following? |
| 600 |
#self.assertIsNot(got, obj) |
| 601 |
# XXX What about between interpreters? |
| 602 |
|
| 603 |
def test_run_string_arg_unresolved(self): |
| 604 |
cid = _channels.create(REPLACE) |
| 605 |
interp = _interpreters.create() |
| 606 |
|
| 607 |
_interpreters.set___main___attrs(interp, dict(cid=cid.send)) |
| 608 |
out = _run_output(interp, dedent(""" |
| 609 |
import _interpchannels as _channels |
| 610 |
print(cid.end) |
| 611 |
_channels.send(cid, b'spam', blocking=False) |
| 612 |
""")) |
| 613 |
obj = recv_nowait(cid) |
| 614 |
|
| 615 |
self.assertEqual(obj, b'spam') |
| 616 |
self.assertEqual(out.strip(), 'send') |
| 617 |
|
| 618 |
# XXX For now there is no high-level channel into which the |
| 619 |
# sent channel ID can be converted... |
| 620 |
# Note: this test caused crashes on some buildbots (bpo-33615). |
| 621 |
@unittest.skip('disabled until high-level channels exist') |
| 622 |
def test_run_string_arg_resolved(self): |
| 623 |
cid = _channels.create(REPLACE) |
| 624 |
cid = _channels._channel_id(cid, _resolve=True) |
| 625 |
interp = _interpreters.create() |
| 626 |
|
| 627 |
out = _run_output(interp, dedent(""" |
| 628 |
import _interpchannels as _channels |
| 629 |
print(chan.id.end) |
| 630 |
_channels.send(chan.id, b'spam', blocking=False) |
| 631 |
"""), |
| 632 |
dict(chan=cid.send)) |
| 633 |
obj = recv_nowait(cid) |
| 634 |
|
| 635 |
self.assertEqual(obj, b'spam') |
| 636 |
self.assertEqual(out.strip(), 'send') |
| 637 |
|
| 638 |
#------------------- |
| 639 |
# send/recv |
| 640 |
|
| 641 |
def test_send_recv_main(self): |
| 642 |
cid = _channels.create(REPLACE) |
| 643 |
orig = b'spam' |
| 644 |
_channels.send(cid, orig, blocking=False) |
| 645 |
obj = recv_nowait(cid) |
| 646 |
|
| 647 |
self.assertEqual(obj, orig) |
| 648 |
self.assertIsNot(obj, orig) |
| 649 |
|
| 650 |
def test_send_recv_same_interpreter(self): |
| 651 |
id1 = _interpreters.create() |
| 652 |
out = _run_output(id1, dedent(""" |
| 653 |
import _interpchannels as _channels |
| 654 |
cid = _channels.create(REPLACE) |
| 655 |
orig = b'spam' |
| 656 |
_channels.send(cid, orig, blocking=False) |
| 657 |
obj, _ = _channels.recv(cid) |
| 658 |
assert obj is not orig |
| 659 |
assert obj == orig |
| 660 |
""")) |
| 661 |
|
| 662 |
def test_send_recv_different_interpreters(self): |
| 663 |
cid = _channels.create(REPLACE) |
| 664 |
id1 = _interpreters.create() |
| 665 |
out = _run_output(id1, dedent(f""" |
| 666 |
import _interpchannels as _channels |
| 667 |
_channels.send({cid}, b'spam', blocking=False) |
| 668 |
""")) |
| 669 |
obj = recv_nowait(cid) |
| 670 |
|
| 671 |
self.assertEqual(obj, b'spam') |
| 672 |
|
| 673 |
def test_send_recv_different_threads(self): |
| 674 |
cid = _channels.create(REPLACE) |
| 675 |
|
| 676 |
def f(): |
| 677 |
obj = recv_wait(cid) |
| 678 |
_channels.send(cid, obj) |
| 679 |
t = threading.Thread(target=f) |
| 680 |
t.start() |
| 681 |
|
| 682 |
_channels.send(cid, b'spam') |
| 683 |
obj = recv_wait(cid) |
| 684 |
t.join() |
| 685 |
|
| 686 |
self.assertEqual(obj, b'spam') |
| 687 |
|
| 688 |
def test_send_recv_different_interpreters_and_threads(self): |
| 689 |
cid = _channels.create(REPLACE) |
| 690 |
id1 = _interpreters.create() |
| 691 |
out = None |
| 692 |
|
| 693 |
def f(): |
| 694 |
nonlocal out |
| 695 |
out = _run_output(id1, dedent(f""" |
| 696 |
import time |
| 697 |
import _interpchannels as _channels |
| 698 |
while True: |
| 699 |
try: |
| 700 |
obj, _ = _channels.recv({cid}) |
| 701 |
break |
| 702 |
except _channels.ChannelEmptyError: |
| 703 |
time.sleep(0.1) |
| 704 |
assert(obj == b'spam') |
| 705 |
_channels.send({cid}, b'eggs') |
| 706 |
""")) |
| 707 |
t = threading.Thread(target=f) |
| 708 |
t.start() |
| 709 |
|
| 710 |
_channels.send(cid, b'spam') |
| 711 |
obj = recv_wait(cid) |
| 712 |
t.join() |
| 713 |
|
| 714 |
self.assertEqual(obj, b'eggs') |
| 715 |
|
| 716 |
def test_send_not_found(self): |
| 717 |
with self.assertRaises(_channels.ChannelNotFoundError): |
| 718 |
_channels.send(10, b'spam') |
| 719 |
|
| 720 |
def test_recv_not_found(self): |
| 721 |
with self.assertRaises(_channels.ChannelNotFoundError): |
| 722 |
_channels.recv(10) |
| 723 |
|
| 724 |
def test_recv_empty(self): |
| 725 |
cid = _channels.create(REPLACE) |
| 726 |
with self.assertRaises(_channels.ChannelEmptyError): |
| 727 |
_channels.recv(cid) |
| 728 |
|
| 729 |
def test_recv_default(self): |
| 730 |
default = object() |
| 731 |
cid = _channels.create(REPLACE) |
| 732 |
obj1 = recv_nowait(cid, default) |
| 733 |
_channels.send(cid, None, blocking=False) |
| 734 |
_channels.send(cid, 1, blocking=False) |
| 735 |
_channels.send(cid, b'spam', blocking=False) |
| 736 |
_channels.send(cid, b'eggs', blocking=False) |
| 737 |
obj2 = recv_nowait(cid, default) |
| 738 |
obj3 = recv_nowait(cid, default) |
| 739 |
obj4 = recv_nowait(cid) |
| 740 |
obj5 = recv_nowait(cid, default) |
| 741 |
obj6 = recv_nowait(cid, default) |
| 742 |
|
| 743 |
self.assertIs(obj1, default) |
| 744 |
self.assertIs(obj2, None) |
| 745 |
self.assertEqual(obj3, 1) |
| 746 |
self.assertEqual(obj4, b'spam') |
| 747 |
self.assertEqual(obj5, b'eggs') |
| 748 |
self.assertIs(obj6, default) |
| 749 |
|
| 750 |
def test_recv_sending_interp_destroyed(self): |
| 751 |
with self.subTest('closed'): |
| 752 |
cid1 = _channels.create(REPLACE) |
| 753 |
interp = _interpreters.create() |
| 754 |
_interpreters.run_string(interp, dedent(f""" |
| 755 |
import _interpchannels as _channels |
| 756 |
_channels.send({cid1}, b'spam', blocking=False) |
| 757 |
""")) |
| 758 |
_interpreters.destroy(interp) |
| 759 |
|
| 760 |
with self.assertRaisesRegex(RuntimeError, |
| 761 |
f'channel {cid1} is closed'): |
| 762 |
_channels.recv(cid1) |
| 763 |
del cid1 |
| 764 |
with self.subTest('still open'): |
| 765 |
cid2 = _channels.create(REPLACE) |
| 766 |
interp = _interpreters.create() |
| 767 |
_interpreters.run_string(interp, dedent(f""" |
| 768 |
import _interpchannels as _channels |
| 769 |
_channels.send({cid2}, b'spam', blocking=False) |
| 770 |
""")) |
| 771 |
_channels.send(cid2, b'eggs', blocking=False) |
| 772 |
_interpreters.destroy(interp) |
| 773 |
|
| 774 |
recv_nowait(cid2, unbound=True) |
| 775 |
recv_nowait(cid2, unbound=False) |
| 776 |
with self.assertRaisesRegex(RuntimeError, |
| 777 |
f'channel {cid2} is empty'): |
| 778 |
_channels.recv(cid2) |
| 779 |
del cid2 |
| 780 |
|
| 781 |
#------------------- |
| 782 |
# send_buffer |
| 783 |
|
| 784 |
def test_send_buffer(self): |
| 785 |
buf = bytearray(b'spamspamspam') |
| 786 |
cid = _channels.create(REPLACE) |
| 787 |
_channels.send_buffer(cid, buf, blocking=False) |
| 788 |
obj = recv_nowait(cid) |
| 789 |
|
| 790 |
self.assertIsNot(obj, buf) |
| 791 |
self.assertIsInstance(obj, memoryview) |
| 792 |
self.assertEqual(obj, buf) |
| 793 |
|
| 794 |
buf[4:8] = b'eggs' |
| 795 |
self.assertEqual(obj, buf) |
| 796 |
obj[4:8] = b'ham.' |
| 797 |
self.assertEqual(obj, buf) |
| 798 |
|
| 799 |
#------------------- |
| 800 |
# send with waiting |
| 801 |
|
| 802 |
def build_send_waiter(self, obj, *, buffer=False): |
| 803 |
# We want a long enough sleep that send() actually has to wait. |
| 804 |
|
| 805 |
if buffer: |
| 806 |
send = _channels.send_buffer |
| 807 |
else: |
| 808 |
send = _channels.send |
| 809 |
|
| 810 |
cid = _channels.create(REPLACE) |
| 811 |
try: |
| 812 |
started = time.monotonic() |
| 813 |
send(cid, obj, blocking=False) |
| 814 |
stopped = time.monotonic() |
| 815 |
recv_nowait(cid) |
| 816 |
finally: |
| 817 |
_channels.destroy(cid) |
| 818 |
delay = stopped - started # seconds |
| 819 |
delay *= 3 |
| 820 |
|
| 821 |
def wait(): |
| 822 |
time.sleep(delay) |
| 823 |
return wait |
| 824 |
|
| 825 |
def test_send_blocking_waiting(self): |
| 826 |
received = None |
| 827 |
obj = b'spam' |
| 828 |
wait = self.build_send_waiter(obj) |
| 829 |
cid = _channels.create(REPLACE) |
| 830 |
def f(): |
| 831 |
nonlocal received |
| 832 |
wait() |
| 833 |
received = recv_wait(cid) |
| 834 |
t = threading.Thread(target=f) |
| 835 |
t.start() |
| 836 |
_channels.send(cid, obj, blocking=True) |
| 837 |
t.join() |
| 838 |
|
| 839 |
self.assertEqual(received, obj) |
| 840 |
|
| 841 |
def test_send_buffer_blocking_waiting(self): |
| 842 |
received = None |
| 843 |
obj = bytearray(b'spam') |
| 844 |
wait = self.build_send_waiter(obj, buffer=True) |
| 845 |
cid = _channels.create(REPLACE) |
| 846 |
def f(): |
| 847 |
nonlocal received |
| 848 |
wait() |
| 849 |
received = recv_wait(cid) |
| 850 |
t = threading.Thread(target=f) |
| 851 |
t.start() |
| 852 |
_channels.send_buffer(cid, obj, blocking=True) |
| 853 |
t.join() |
| 854 |
|
| 855 |
self.assertEqual(received, obj) |
| 856 |
|
| 857 |
def test_send_blocking_no_wait(self): |
| 858 |
received = None |
| 859 |
obj = b'spam' |
| 860 |
cid = _channels.create(REPLACE) |
| 861 |
def f(): |
| 862 |
nonlocal received |
| 863 |
received = recv_wait(cid) |
| 864 |
t = threading.Thread(target=f) |
| 865 |
t.start() |
| 866 |
_channels.send(cid, obj, blocking=True) |
| 867 |
t.join() |
| 868 |
|
| 869 |
self.assertEqual(received, obj) |
| 870 |
|
| 871 |
def test_send_buffer_blocking_no_wait(self): |
| 872 |
received = None |
| 873 |
obj = bytearray(b'spam') |
| 874 |
cid = _channels.create(REPLACE) |
| 875 |
def f(): |
| 876 |
nonlocal received |
| 877 |
received = recv_wait(cid) |
| 878 |
t = threading.Thread(target=f) |
| 879 |
t.start() |
| 880 |
_channels.send_buffer(cid, obj, blocking=True) |
| 881 |
t.join() |
| 882 |
|
| 883 |
self.assertEqual(received, obj) |
| 884 |
|
| 885 |
def test_send_timeout(self): |
| 886 |
obj = b'spam' |
| 887 |
|
| 888 |
with self.subTest('non-blocking with timeout'): |
| 889 |
cid = _channels.create(REPLACE) |
| 890 |
with self.assertRaises(ValueError): |
| 891 |
_channels.send(cid, obj, blocking=False, timeout=0.1) |
| 892 |
|
| 893 |
with self.subTest('timeout hit'): |
| 894 |
cid = _channels.create(REPLACE) |
| 895 |
with self.assertRaises(TimeoutError): |
| 896 |
_channels.send(cid, obj, blocking=True, timeout=0.1) |
| 897 |
with self.assertRaises(_channels.ChannelEmptyError): |
| 898 |
received = recv_nowait(cid) |
| 899 |
print(repr(received)) |
| 900 |
|
| 901 |
with self.subTest('timeout not hit'): |
| 902 |
cid = _channels.create(REPLACE) |
| 903 |
def f(): |
| 904 |
recv_wait(cid) |
| 905 |
t = threading.Thread(target=f) |
| 906 |
t.start() |
| 907 |
_channels.send(cid, obj, blocking=True, timeout=10) |
| 908 |
t.join() |
| 909 |
|
| 910 |
def test_send_buffer_timeout(self): |
| 911 |
try: |
| 912 |
self._has_run_once_timeout |
| 913 |
except AttributeError: |
| 914 |
# At the moment, this test leaks a few references. |
| 915 |
# It looks like the leak originates with the addition |
| 916 |
# of _channels.send_buffer() (gh-110246), whereas the |
| 917 |
# tests were added afterward. We want this test even |
| 918 |
# if the refleak isn't fixed yet, so we skip here. |
| 919 |
raise unittest.SkipTest('temporarily skipped due to refleaks') |
| 920 |
else: |
| 921 |
self._has_run_once_timeout = True |
| 922 |
|
| 923 |
obj = bytearray(b'spam') |
| 924 |
|
| 925 |
with self.subTest('non-blocking with timeout'): |
| 926 |
cid = _channels.create(REPLACE) |
| 927 |
with self.assertRaises(ValueError): |
| 928 |
_channels.send_buffer(cid, obj, blocking=False, timeout=0.1) |
| 929 |
|
| 930 |
with self.subTest('timeout hit'): |
| 931 |
cid = _channels.create(REPLACE) |
| 932 |
with self.assertRaises(TimeoutError): |
| 933 |
_channels.send_buffer(cid, obj, blocking=True, timeout=0.1) |
| 934 |
with self.assertRaises(_channels.ChannelEmptyError): |
| 935 |
received = recv_nowait(cid) |
| 936 |
print(repr(received)) |
| 937 |
|
| 938 |
with self.subTest('timeout not hit'): |
| 939 |
cid = _channels.create(REPLACE) |
| 940 |
def f(): |
| 941 |
recv_wait(cid) |
| 942 |
t = threading.Thread(target=f) |
| 943 |
t.start() |
| 944 |
_channels.send_buffer(cid, obj, blocking=True, timeout=10) |
| 945 |
t.join() |
| 946 |
|
| 947 |
def test_send_closed_while_waiting(self): |
| 948 |
obj = b'spam' |
| 949 |
wait = self.build_send_waiter(obj) |
| 950 |
|
| 951 |
with self.subTest('without timeout'): |
| 952 |
cid = _channels.create(REPLACE) |
| 953 |
def f(): |
| 954 |
wait() |
| 955 |
_channels.close(cid, force=True) |
| 956 |
t = threading.Thread(target=f) |
| 957 |
t.start() |
| 958 |
with self.assertRaises(_channels.ChannelClosedError): |
| 959 |
_channels.send(cid, obj, blocking=True) |
| 960 |
t.join() |
| 961 |
|
| 962 |
with self.subTest('with timeout'): |
| 963 |
cid = _channels.create(REPLACE) |
| 964 |
def f(): |
| 965 |
wait() |
| 966 |
_channels.close(cid, force=True) |
| 967 |
t = threading.Thread(target=f) |
| 968 |
t.start() |
| 969 |
with self.assertRaises(_channels.ChannelClosedError): |
| 970 |
_channels.send(cid, obj, blocking=True, timeout=30) |
| 971 |
t.join() |
| 972 |
|
| 973 |
def test_send_buffer_closed_while_waiting(self): |
| 974 |
try: |
| 975 |
self._has_run_once_closed |
| 976 |
except AttributeError: |
| 977 |
# At the moment, this test leaks a few references. |
| 978 |
# It looks like the leak originates with the addition |
| 979 |
# of _channels.send_buffer() (gh-110246), whereas the |
| 980 |
# tests were added afterward. We want this test even |
| 981 |
# if the refleak isn't fixed yet, so we skip here. |
| 982 |
raise unittest.SkipTest('temporarily skipped due to refleaks') |
| 983 |
else: |
| 984 |
self._has_run_once_closed = True |
| 985 |
|
| 986 |
obj = bytearray(b'spam') |
| 987 |
wait = self.build_send_waiter(obj, buffer=True) |
| 988 |
|
| 989 |
with self.subTest('without timeout'): |
| 990 |
cid = _channels.create(REPLACE) |
| 991 |
def f(): |
| 992 |
wait() |
| 993 |
_channels.close(cid, force=True) |
| 994 |
t = threading.Thread(target=f) |
| 995 |
t.start() |
| 996 |
with self.assertRaises(_channels.ChannelClosedError): |
| 997 |
_channels.send_buffer(cid, obj, blocking=True) |
| 998 |
t.join() |
| 999 |
|
| 1000 |
with self.subTest('with timeout'): |
| 1001 |
cid = _channels.create(REPLACE) |
| 1002 |
def f(): |
| 1003 |
wait() |
| 1004 |
_channels.close(cid, force=True) |
| 1005 |
t = threading.Thread(target=f) |
| 1006 |
t.start() |
| 1007 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1008 |
_channels.send_buffer(cid, obj, blocking=True, timeout=30) |
| 1009 |
t.join() |
| 1010 |
|
| 1011 |
#------------------- |
| 1012 |
# close |
| 1013 |
|
| 1014 |
def test_close_single_user(self): |
| 1015 |
cid = _channels.create(REPLACE) |
| 1016 |
_channels.send(cid, b'spam', blocking=False) |
| 1017 |
recv_nowait(cid) |
| 1018 |
_channels.close(cid) |
| 1019 |
|
| 1020 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1021 |
_channels.send(cid, b'eggs') |
| 1022 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1023 |
_channels.recv(cid) |
| 1024 |
|
| 1025 |
def test_close_multiple_users(self): |
| 1026 |
cid = _channels.create(REPLACE) |
| 1027 |
id1 = _interpreters.create() |
| 1028 |
id2 = _interpreters.create() |
| 1029 |
_interpreters.run_string(id1, dedent(f""" |
| 1030 |
import _interpchannels as _channels |
| 1031 |
_channels.send({cid}, b'spam', blocking=False) |
| 1032 |
""")) |
| 1033 |
_interpreters.run_string(id2, dedent(f""" |
| 1034 |
import _interpchannels as _channels |
| 1035 |
_channels.recv({cid}) |
| 1036 |
""")) |
| 1037 |
_channels.close(cid) |
| 1038 |
|
| 1039 |
excsnap = _interpreters.run_string(id1, dedent(f""" |
| 1040 |
_channels.send({cid}, b'spam') |
| 1041 |
""")) |
| 1042 |
self.assertEqual(excsnap.type.__name__, 'ChannelClosedError') |
| 1043 |
|
| 1044 |
excsnap = _interpreters.run_string(id2, dedent(f""" |
| 1045 |
_channels.send({cid}, b'spam') |
| 1046 |
""")) |
| 1047 |
self.assertEqual(excsnap.type.__name__, 'ChannelClosedError') |
| 1048 |
|
| 1049 |
def test_close_multiple_times(self): |
| 1050 |
cid = _channels.create(REPLACE) |
| 1051 |
_channels.send(cid, b'spam', blocking=False) |
| 1052 |
recv_nowait(cid) |
| 1053 |
_channels.close(cid) |
| 1054 |
|
| 1055 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1056 |
_channels.close(cid) |
| 1057 |
|
| 1058 |
def test_close_empty(self): |
| 1059 |
tests = [ |
| 1060 |
(False, False), |
| 1061 |
(True, False), |
| 1062 |
(False, True), |
| 1063 |
(True, True), |
| 1064 |
] |
| 1065 |
for send, recv in tests: |
| 1066 |
with self.subTest((send, recv)): |
| 1067 |
cid = _channels.create(REPLACE) |
| 1068 |
_channels.send(cid, b'spam', blocking=False) |
| 1069 |
recv_nowait(cid) |
| 1070 |
_channels.close(cid, send=send, recv=recv) |
| 1071 |
|
| 1072 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1073 |
_channels.send(cid, b'eggs') |
| 1074 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1075 |
_channels.recv(cid) |
| 1076 |
|
| 1077 |
def test_close_defaults_with_unused_items(self): |
| 1078 |
cid = _channels.create(REPLACE) |
| 1079 |
_channels.send(cid, b'spam', blocking=False) |
| 1080 |
_channels.send(cid, b'ham', blocking=False) |
| 1081 |
|
| 1082 |
with self.assertRaises(_channels.ChannelNotEmptyError): |
| 1083 |
_channels.close(cid) |
| 1084 |
recv_nowait(cid) |
| 1085 |
_channels.send(cid, b'eggs', blocking=False) |
| 1086 |
|
| 1087 |
def test_close_recv_with_unused_items_unforced(self): |
| 1088 |
cid = _channels.create(REPLACE) |
| 1089 |
_channels.send(cid, b'spam', blocking=False) |
| 1090 |
_channels.send(cid, b'ham', blocking=False) |
| 1091 |
|
| 1092 |
with self.assertRaises(_channels.ChannelNotEmptyError): |
| 1093 |
_channels.close(cid, recv=True) |
| 1094 |
recv_nowait(cid) |
| 1095 |
_channels.send(cid, b'eggs', blocking=False) |
| 1096 |
recv_nowait(cid) |
| 1097 |
recv_nowait(cid) |
| 1098 |
_channels.close(cid, recv=True) |
| 1099 |
|
| 1100 |
def test_close_send_with_unused_items_unforced(self): |
| 1101 |
cid = _channels.create(REPLACE) |
| 1102 |
_channels.send(cid, b'spam', blocking=False) |
| 1103 |
_channels.send(cid, b'ham', blocking=False) |
| 1104 |
_channels.close(cid, send=True) |
| 1105 |
|
| 1106 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1107 |
_channels.send(cid, b'eggs') |
| 1108 |
recv_nowait(cid) |
| 1109 |
recv_nowait(cid) |
| 1110 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1111 |
_channels.recv(cid) |
| 1112 |
|
| 1113 |
def test_close_both_with_unused_items_unforced(self): |
| 1114 |
cid = _channels.create(REPLACE) |
| 1115 |
_channels.send(cid, b'spam', blocking=False) |
| 1116 |
_channels.send(cid, b'ham', blocking=False) |
| 1117 |
|
| 1118 |
with self.assertRaises(_channels.ChannelNotEmptyError): |
| 1119 |
_channels.close(cid, recv=True, send=True) |
| 1120 |
recv_nowait(cid) |
| 1121 |
_channels.send(cid, b'eggs', blocking=False) |
| 1122 |
recv_nowait(cid) |
| 1123 |
recv_nowait(cid) |
| 1124 |
_channels.close(cid, recv=True) |
| 1125 |
|
| 1126 |
def test_close_recv_with_unused_items_forced(self): |
| 1127 |
cid = _channels.create(REPLACE) |
| 1128 |
_channels.send(cid, b'spam', blocking=False) |
| 1129 |
_channels.send(cid, b'ham', blocking=False) |
| 1130 |
_channels.close(cid, recv=True, force=True) |
| 1131 |
|
| 1132 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1133 |
_channels.send(cid, b'eggs') |
| 1134 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1135 |
_channels.recv(cid) |
| 1136 |
|
| 1137 |
def test_close_send_with_unused_items_forced(self): |
| 1138 |
cid = _channels.create(REPLACE) |
| 1139 |
_channels.send(cid, b'spam', blocking=False) |
| 1140 |
_channels.send(cid, b'ham', blocking=False) |
| 1141 |
_channels.close(cid, send=True, force=True) |
| 1142 |
|
| 1143 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1144 |
_channels.send(cid, b'eggs') |
| 1145 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1146 |
_channels.recv(cid) |
| 1147 |
|
| 1148 |
def test_close_both_with_unused_items_forced(self): |
| 1149 |
cid = _channels.create(REPLACE) |
| 1150 |
_channels.send(cid, b'spam', blocking=False) |
| 1151 |
_channels.send(cid, b'ham', blocking=False) |
| 1152 |
_channels.close(cid, send=True, recv=True, force=True) |
| 1153 |
|
| 1154 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1155 |
_channels.send(cid, b'eggs') |
| 1156 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1157 |
_channels.recv(cid) |
| 1158 |
|
| 1159 |
def test_close_never_used(self): |
| 1160 |
cid = _channels.create(REPLACE) |
| 1161 |
_channels.close(cid) |
| 1162 |
|
| 1163 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1164 |
_channels.send(cid, b'spam') |
| 1165 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1166 |
_channels.recv(cid) |
| 1167 |
|
| 1168 |
def test_close_by_unassociated_interp(self): |
| 1169 |
cid = _channels.create(REPLACE) |
| 1170 |
_channels.send(cid, b'spam', blocking=False) |
| 1171 |
interp = _interpreters.create() |
| 1172 |
_interpreters.run_string(interp, dedent(f""" |
| 1173 |
import _interpchannels as _channels |
| 1174 |
_channels.close({cid}, force=True) |
| 1175 |
""")) |
| 1176 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1177 |
_channels.recv(cid) |
| 1178 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1179 |
_channels.close(cid) |
| 1180 |
|
| 1181 |
def test_close_used_multiple_times_by_single_user(self): |
| 1182 |
cid = _channels.create(REPLACE) |
| 1183 |
_channels.send(cid, b'spam', blocking=False) |
| 1184 |
_channels.send(cid, b'spam', blocking=False) |
| 1185 |
_channels.send(cid, b'spam', blocking=False) |
| 1186 |
recv_nowait(cid) |
| 1187 |
_channels.close(cid, force=True) |
| 1188 |
|
| 1189 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1190 |
_channels.send(cid, b'eggs') |
| 1191 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1192 |
_channels.recv(cid) |
| 1193 |
|
| 1194 |
def test_channel_list_interpreters_invalid_channel(self): |
| 1195 |
cid = _channels.create(REPLACE) |
| 1196 |
# Test for invalid channel ID. |
| 1197 |
with self.assertRaises(_channels.ChannelNotFoundError): |
| 1198 |
_channels.list_interpreters(1000, send=True) |
| 1199 |
|
| 1200 |
_channels.close(cid) |
| 1201 |
# Test for a channel that has been closed. |
| 1202 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1203 |
_channels.list_interpreters(cid, send=True) |
| 1204 |
|
| 1205 |
def test_channel_list_interpreters_invalid_args(self): |
| 1206 |
# Tests for invalid arguments passed to the API. |
| 1207 |
cid = _channels.create(REPLACE) |
| 1208 |
with self.assertRaises(TypeError): |
| 1209 |
_channels.list_interpreters(cid) |
| 1210 |
|
| 1211 |
|
| 1212 |
class ChannelReleaseTests(TestBase): |
| 1213 |
|
| 1214 |
# XXX Add more test coverage a la the tests for close(). |
| 1215 |
|
| 1216 |
""" |
| 1217 |
- main / interp / other |
| 1218 |
- run in: current thread / new thread / other thread / different threads |
| 1219 |
- end / opposite |
| 1220 |
- force / no force |
| 1221 |
- used / not used (associated / not associated) |
| 1222 |
- empty / emptied / never emptied / partly emptied |
| 1223 |
- closed / not closed |
| 1224 |
- released / not released |
| 1225 |
- creator (interp) / other |
| 1226 |
- associated interpreter not running |
| 1227 |
- associated interpreter destroyed |
| 1228 |
""" |
| 1229 |
|
| 1230 |
""" |
| 1231 |
use |
| 1232 |
pre-release |
| 1233 |
release |
| 1234 |
after |
| 1235 |
check |
| 1236 |
""" |
| 1237 |
|
| 1238 |
""" |
| 1239 |
release in: main, interp1 |
| 1240 |
creator: same, other (incl. interp2) |
| 1241 |
|
| 1242 |
use: None,send,recv,send/recv in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1243 |
pre-release: None,send,recv,both in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1244 |
pre-release forced: None,send,recv,both in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1245 |
|
| 1246 |
release: same |
| 1247 |
release forced: same |
| 1248 |
|
| 1249 |
use after: None,send,recv,send/recv in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1250 |
release after: None,send,recv,send/recv in None,same,other(incl. interp2),same+other(incl. interp2),all |
| 1251 |
check released: send/recv for same/other(incl. interp2) |
| 1252 |
check closed: send/recv for same/other(incl. interp2) |
| 1253 |
""" |
| 1254 |
|
| 1255 |
def test_single_user(self): |
| 1256 |
cid = _channels.create(REPLACE) |
| 1257 |
_channels.send(cid, b'spam', blocking=False) |
| 1258 |
recv_nowait(cid) |
| 1259 |
_channels.release(cid, send=True, recv=True) |
| 1260 |
|
| 1261 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1262 |
_channels.send(cid, b'eggs') |
| 1263 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1264 |
_channels.recv(cid) |
| 1265 |
|
| 1266 |
def test_multiple_users(self): |
| 1267 |
cid = _channels.create(REPLACE) |
| 1268 |
id1 = _interpreters.create() |
| 1269 |
id2 = _interpreters.create() |
| 1270 |
_interpreters.run_string(id1, dedent(f""" |
| 1271 |
import _interpchannels as _channels |
| 1272 |
_channels.send({cid}, b'spam', blocking=False) |
| 1273 |
""")) |
| 1274 |
out = _run_output(id2, dedent(f""" |
| 1275 |
import _interpchannels as _channels |
| 1276 |
obj, _ = _channels.recv({cid}) |
| 1277 |
_channels.release({cid}) |
| 1278 |
print(repr(obj)) |
| 1279 |
""")) |
| 1280 |
_interpreters.run_string(id1, dedent(f""" |
| 1281 |
_channels.release({cid}) |
| 1282 |
""")) |
| 1283 |
|
| 1284 |
self.assertEqual(out.strip(), "b'spam'") |
| 1285 |
|
| 1286 |
def test_no_kwargs(self): |
| 1287 |
cid = _channels.create(REPLACE) |
| 1288 |
_channels.send(cid, b'spam', blocking=False) |
| 1289 |
recv_nowait(cid) |
| 1290 |
_channels.release(cid) |
| 1291 |
|
| 1292 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1293 |
_channels.send(cid, b'eggs') |
| 1294 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1295 |
_channels.recv(cid) |
| 1296 |
|
| 1297 |
def test_multiple_times(self): |
| 1298 |
cid = _channels.create(REPLACE) |
| 1299 |
_channels.send(cid, b'spam', blocking=False) |
| 1300 |
recv_nowait(cid) |
| 1301 |
_channels.release(cid, send=True, recv=True) |
| 1302 |
|
| 1303 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1304 |
_channels.release(cid, send=True, recv=True) |
| 1305 |
|
| 1306 |
def test_with_unused_items(self): |
| 1307 |
cid = _channels.create(REPLACE) |
| 1308 |
_channels.send(cid, b'spam', blocking=False) |
| 1309 |
_channels.send(cid, b'ham', blocking=False) |
| 1310 |
_channels.release(cid, send=True, recv=True) |
| 1311 |
|
| 1312 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1313 |
_channels.recv(cid) |
| 1314 |
|
| 1315 |
def test_never_used(self): |
| 1316 |
cid = _channels.create(REPLACE) |
| 1317 |
_channels.release(cid) |
| 1318 |
|
| 1319 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1320 |
_channels.send(cid, b'spam') |
| 1321 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1322 |
_channels.recv(cid) |
| 1323 |
|
| 1324 |
def test_by_unassociated_interp(self): |
| 1325 |
cid = _channels.create(REPLACE) |
| 1326 |
_channels.send(cid, b'spam', blocking=False) |
| 1327 |
interp = _interpreters.create() |
| 1328 |
_interpreters.run_string(interp, dedent(f""" |
| 1329 |
import _interpchannels as _channels |
| 1330 |
_channels.release({cid}) |
| 1331 |
""")) |
| 1332 |
obj = recv_nowait(cid) |
| 1333 |
_channels.release(cid) |
| 1334 |
|
| 1335 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1336 |
_channels.send(cid, b'eggs') |
| 1337 |
self.assertEqual(obj, b'spam') |
| 1338 |
|
| 1339 |
def test_close_if_unassociated(self): |
| 1340 |
# XXX Something's not right with this test... |
| 1341 |
cid = _channels.create(REPLACE) |
| 1342 |
interp = _interpreters.create() |
| 1343 |
_interpreters.run_string(interp, dedent(f""" |
| 1344 |
import _interpchannels as _channels |
| 1345 |
obj = _channels.send({cid}, b'spam', blocking=False) |
| 1346 |
_channels.release({cid}) |
| 1347 |
""")) |
| 1348 |
|
| 1349 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1350 |
_channels.recv(cid) |
| 1351 |
|
| 1352 |
def test_partially(self): |
| 1353 |
# XXX Is partial close too weird/confusing? |
| 1354 |
cid = _channels.create(REPLACE) |
| 1355 |
_channels.send(cid, None, blocking=False) |
| 1356 |
recv_nowait(cid) |
| 1357 |
_channels.send(cid, b'spam', blocking=False) |
| 1358 |
_channels.release(cid, send=True) |
| 1359 |
obj = recv_nowait(cid) |
| 1360 |
|
| 1361 |
self.assertEqual(obj, b'spam') |
| 1362 |
|
| 1363 |
def test_used_multiple_times_by_single_user(self): |
| 1364 |
cid = _channels.create(REPLACE) |
| 1365 |
_channels.send(cid, b'spam', blocking=False) |
| 1366 |
_channels.send(cid, b'spam', blocking=False) |
| 1367 |
_channels.send(cid, b'spam', blocking=False) |
| 1368 |
recv_nowait(cid) |
| 1369 |
_channels.release(cid, send=True, recv=True) |
| 1370 |
|
| 1371 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1372 |
_channels.send(cid, b'eggs') |
| 1373 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1374 |
_channels.recv(cid) |
| 1375 |
|
| 1376 |
|
| 1377 |
class ChannelCloseFixture(namedtuple('ChannelCloseFixture', |
| 1378 |
'end interp other extra creator')): |
| 1379 |
|
| 1380 |
# Set this to True to avoid creating interpreters, e.g. when |
| 1381 |
# scanning through test permutations without running them. |
| 1382 |
QUICK = False |
| 1383 |
|
| 1384 |
def __new__(cls, end, interp, other, extra, creator): |
| 1385 |
assert end in ('send', 'recv') |
| 1386 |
if cls.QUICK: |
| 1387 |
known = {} |
| 1388 |
else: |
| 1389 |
interp = Interpreter.from_raw(interp) |
| 1390 |
other = Interpreter.from_raw(other) |
| 1391 |
extra = Interpreter.from_raw(extra) |
| 1392 |
known = { |
| 1393 |
interp.name: interp, |
| 1394 |
other.name: other, |
| 1395 |
extra.name: extra, |
| 1396 |
} |
| 1397 |
if not creator: |
| 1398 |
creator = 'same' |
| 1399 |
self = super().__new__(cls, end, interp, other, extra, creator) |
| 1400 |
self._prepped = set() |
| 1401 |
self._state = ChannelState() |
| 1402 |
self._known = known |
| 1403 |
return self |
| 1404 |
|
| 1405 |
@property |
| 1406 |
def state(self): |
| 1407 |
return self._state |
| 1408 |
|
| 1409 |
@property |
| 1410 |
def cid(self): |
| 1411 |
try: |
| 1412 |
return self._cid |
| 1413 |
except AttributeError: |
| 1414 |
creator = self._get_interpreter(self.creator) |
| 1415 |
self._cid = self._new_channel(creator) |
| 1416 |
return self._cid |
| 1417 |
|
| 1418 |
def get_interpreter(self, interp): |
| 1419 |
interp = self._get_interpreter(interp) |
| 1420 |
self._prep_interpreter(interp) |
| 1421 |
return interp |
| 1422 |
|
| 1423 |
def expect_closed_error(self, end=None): |
| 1424 |
if end is None: |
| 1425 |
end = self.end |
| 1426 |
if end == 'recv' and self.state.closed == 'send': |
| 1427 |
return False |
| 1428 |
return bool(self.state.closed) |
| 1429 |
|
| 1430 |
def prep_interpreter(self, interp): |
| 1431 |
self._prep_interpreter(interp) |
| 1432 |
|
| 1433 |
def record_action(self, action, result): |
| 1434 |
self._state = result |
| 1435 |
|
| 1436 |
def clean_up(self): |
| 1437 |
clean_up_interpreters() |
| 1438 |
clean_up_channels() |
| 1439 |
|
| 1440 |
# internal methods |
| 1441 |
|
| 1442 |
def _new_channel(self, creator): |
| 1443 |
if creator.name == 'main': |
| 1444 |
return _channels.create(REPLACE) |
| 1445 |
else: |
| 1446 |
ch = _channels.create(REPLACE) |
| 1447 |
run_interp(creator.id, f""" |
| 1448 |
import _interpreters |
| 1449 |
cid = _xxsubchannels.create() |
| 1450 |
# We purposefully send back an int to avoid tying the |
| 1451 |
# channel to the other interpreter. |
| 1452 |
_xxsubchannels.send({ch}, int(cid), blocking=False) |
| 1453 |
del _interpreters |
| 1454 |
""") |
| 1455 |
self._cid = recv_nowait(ch) |
| 1456 |
return self._cid |
| 1457 |
|
| 1458 |
def _get_interpreter(self, interp): |
| 1459 |
if interp in ('same', 'interp'): |
| 1460 |
return self.interp |
| 1461 |
elif interp == 'other': |
| 1462 |
return self.other |
| 1463 |
elif interp == 'extra': |
| 1464 |
return self.extra |
| 1465 |
else: |
| 1466 |
name = interp |
| 1467 |
try: |
| 1468 |
interp = self._known[name] |
| 1469 |
except KeyError: |
| 1470 |
interp = self._known[name] = Interpreter(name) |
| 1471 |
return interp |
| 1472 |
|
| 1473 |
def _prep_interpreter(self, interp): |
| 1474 |
if interp.id in self._prepped: |
| 1475 |
return |
| 1476 |
self._prepped.add(interp.id) |
| 1477 |
if interp.name == 'main': |
| 1478 |
return |
| 1479 |
run_interp(interp.id, f""" |
| 1480 |
import _interpchannels as channels |
| 1481 |
import test.test__interpchannels as helpers |
| 1482 |
ChannelState = helpers.ChannelState |
| 1483 |
try: |
| 1484 |
cid |
| 1485 |
except NameError: |
| 1486 |
cid = _channels._channel_id({self.cid}) |
| 1487 |
""") |
| 1488 |
|
| 1489 |
|
| 1490 |
@unittest.skip('these tests take several hours to run') |
| 1491 |
class ExhaustiveChannelTests(TestBase): |
| 1492 |
|
| 1493 |
""" |
| 1494 |
- main / interp / other |
| 1495 |
- run in: current thread / new thread / other thread / different threads |
| 1496 |
- end / opposite |
| 1497 |
- force / no force |
| 1498 |
- used / not used (associated / not associated) |
| 1499 |
- empty / emptied / never emptied / partly emptied |
| 1500 |
- closed / not closed |
| 1501 |
- released / not released |
| 1502 |
- creator (interp) / other |
| 1503 |
- associated interpreter not running |
| 1504 |
- associated interpreter destroyed |
| 1505 |
|
| 1506 |
- close after unbound |
| 1507 |
""" |
| 1508 |
|
| 1509 |
""" |
| 1510 |
use |
| 1511 |
pre-close |
| 1512 |
close |
| 1513 |
after |
| 1514 |
check |
| 1515 |
""" |
| 1516 |
|
| 1517 |
""" |
| 1518 |
close in: main, interp1 |
| 1519 |
creator: same, other, extra |
| 1520 |
|
| 1521 |
use: None,send,recv,send/recv in None,same,other,same+other,all |
| 1522 |
pre-close: None,send,recv in None,same,other,same+other,all |
| 1523 |
pre-close forced: None,send,recv in None,same,other,same+other,all |
| 1524 |
|
| 1525 |
close: same |
| 1526 |
close forced: same |
| 1527 |
|
| 1528 |
use after: None,send,recv,send/recv in None,same,other,extra,same+other,all |
| 1529 |
close after: None,send,recv,send/recv in None,same,other,extra,same+other,all |
| 1530 |
check closed: send/recv for same/other(incl. interp2) |
| 1531 |
""" |
| 1532 |
|
| 1533 |
def iter_action_sets(self): |
| 1534 |
# - used / not used (associated / not associated) |
| 1535 |
# - empty / emptied / never emptied / partly emptied |
| 1536 |
# - closed / not closed |
| 1537 |
# - released / not released |
| 1538 |
|
| 1539 |
# never used |
| 1540 |
yield [] |
| 1541 |
|
| 1542 |
# only pre-closed (and possible used after) |
| 1543 |
for closeactions in self._iter_close_action_sets('same', 'other'): |
| 1544 |
yield closeactions |
| 1545 |
for postactions in self._iter_post_close_action_sets(): |
| 1546 |
yield closeactions + postactions |
| 1547 |
for closeactions in self._iter_close_action_sets('other', 'extra'): |
| 1548 |
yield closeactions |
| 1549 |
for postactions in self._iter_post_close_action_sets(): |
| 1550 |
yield closeactions + postactions |
| 1551 |
|
| 1552 |
# used |
| 1553 |
for useactions in self._iter_use_action_sets('same', 'other'): |
| 1554 |
yield useactions |
| 1555 |
for closeactions in self._iter_close_action_sets('same', 'other'): |
| 1556 |
actions = useactions + closeactions |
| 1557 |
yield actions |
| 1558 |
for postactions in self._iter_post_close_action_sets(): |
| 1559 |
yield actions + postactions |
| 1560 |
for closeactions in self._iter_close_action_sets('other', 'extra'): |
| 1561 |
actions = useactions + closeactions |
| 1562 |
yield actions |
| 1563 |
for postactions in self._iter_post_close_action_sets(): |
| 1564 |
yield actions + postactions |
| 1565 |
for useactions in self._iter_use_action_sets('other', 'extra'): |
| 1566 |
yield useactions |
| 1567 |
for closeactions in self._iter_close_action_sets('same', 'other'): |
| 1568 |
actions = useactions + closeactions |
| 1569 |
yield actions |
| 1570 |
for postactions in self._iter_post_close_action_sets(): |
| 1571 |
yield actions + postactions |
| 1572 |
for closeactions in self._iter_close_action_sets('other', 'extra'): |
| 1573 |
actions = useactions + closeactions |
| 1574 |
yield actions |
| 1575 |
for postactions in self._iter_post_close_action_sets(): |
| 1576 |
yield actions + postactions |
| 1577 |
|
| 1578 |
def _iter_use_action_sets(self, interp1, interp2): |
| 1579 |
interps = (interp1, interp2) |
| 1580 |
|
| 1581 |
# only recv end used |
| 1582 |
yield [ |
| 1583 |
ChannelAction('use', 'recv', interp1), |
| 1584 |
] |
| 1585 |
yield [ |
| 1586 |
ChannelAction('use', 'recv', interp2), |
| 1587 |
] |
| 1588 |
yield [ |
| 1589 |
ChannelAction('use', 'recv', interp1), |
| 1590 |
ChannelAction('use', 'recv', interp2), |
| 1591 |
] |
| 1592 |
|
| 1593 |
# never emptied |
| 1594 |
yield [ |
| 1595 |
ChannelAction('use', 'send', interp1), |
| 1596 |
] |
| 1597 |
yield [ |
| 1598 |
ChannelAction('use', 'send', interp2), |
| 1599 |
] |
| 1600 |
yield [ |
| 1601 |
ChannelAction('use', 'send', interp1), |
| 1602 |
ChannelAction('use', 'send', interp2), |
| 1603 |
] |
| 1604 |
|
| 1605 |
# partially emptied |
| 1606 |
for interp1 in interps: |
| 1607 |
for interp2 in interps: |
| 1608 |
for interp3 in interps: |
| 1609 |
yield [ |
| 1610 |
ChannelAction('use', 'send', interp1), |
| 1611 |
ChannelAction('use', 'send', interp2), |
| 1612 |
ChannelAction('use', 'recv', interp3), |
| 1613 |
] |
| 1614 |
|
| 1615 |
# fully emptied |
| 1616 |
for interp1 in interps: |
| 1617 |
for interp2 in interps: |
| 1618 |
for interp3 in interps: |
| 1619 |
for interp4 in interps: |
| 1620 |
yield [ |
| 1621 |
ChannelAction('use', 'send', interp1), |
| 1622 |
ChannelAction('use', 'send', interp2), |
| 1623 |
ChannelAction('use', 'recv', interp3), |
| 1624 |
ChannelAction('use', 'recv', interp4), |
| 1625 |
] |
| 1626 |
|
| 1627 |
def _iter_close_action_sets(self, interp1, interp2): |
| 1628 |
ends = ('recv', 'send') |
| 1629 |
interps = (interp1, interp2) |
| 1630 |
for force in (True, False): |
| 1631 |
op = 'force-close' if force else 'close' |
| 1632 |
for interp in interps: |
| 1633 |
for end in ends: |
| 1634 |
yield [ |
| 1635 |
ChannelAction(op, end, interp), |
| 1636 |
] |
| 1637 |
for recvop in ('close', 'force-close'): |
| 1638 |
for sendop in ('close', 'force-close'): |
| 1639 |
for recv in interps: |
| 1640 |
for send in interps: |
| 1641 |
yield [ |
| 1642 |
ChannelAction(recvop, 'recv', recv), |
| 1643 |
ChannelAction(sendop, 'send', send), |
| 1644 |
] |
| 1645 |
|
| 1646 |
def _iter_post_close_action_sets(self): |
| 1647 |
for interp in ('same', 'extra', 'other'): |
| 1648 |
yield [ |
| 1649 |
ChannelAction('use', 'recv', interp), |
| 1650 |
] |
| 1651 |
yield [ |
| 1652 |
ChannelAction('use', 'send', interp), |
| 1653 |
] |
| 1654 |
|
| 1655 |
def run_actions(self, fix, actions): |
| 1656 |
for action in actions: |
| 1657 |
self.run_action(fix, action) |
| 1658 |
|
| 1659 |
def run_action(self, fix, action, *, hideclosed=True): |
| 1660 |
end = action.resolve_end(fix.end) |
| 1661 |
interp = action.resolve_interp(fix.interp, fix.other, fix.extra) |
| 1662 |
fix.prep_interpreter(interp) |
| 1663 |
if interp.name == 'main': |
| 1664 |
result = run_action( |
| 1665 |
fix.cid, |
| 1666 |
action.action, |
| 1667 |
end, |
| 1668 |
fix.state, |
| 1669 |
hideclosed=hideclosed, |
| 1670 |
) |
| 1671 |
fix.record_action(action, result) |
| 1672 |
else: |
| 1673 |
_cid = _channels.create(REPLACE) |
| 1674 |
run_interp(interp.id, f""" |
| 1675 |
result = helpers.run_action( |
| 1676 |
{fix.cid}, |
| 1677 |
{repr(action.action)}, |
| 1678 |
{repr(end)}, |
| 1679 |
{repr(fix.state)}, |
| 1680 |
hideclosed={hideclosed}, |
| 1681 |
) |
| 1682 |
_channels.send({_cid}, result.pending.to_bytes(1, 'little'), blocking=False) |
| 1683 |
_channels.send({_cid}, b'X' if result.closed else b'', blocking=False) |
| 1684 |
""") |
| 1685 |
result = ChannelState( |
| 1686 |
pending=int.from_bytes(recv_nowait(_cid), 'little'), |
| 1687 |
closed=bool(recv_nowait(_cid)), |
| 1688 |
) |
| 1689 |
fix.record_action(action, result) |
| 1690 |
|
| 1691 |
def iter_fixtures(self): |
| 1692 |
# XXX threads? |
| 1693 |
interpreters = [ |
| 1694 |
('main', 'interp', 'extra'), |
| 1695 |
('interp', 'main', 'extra'), |
| 1696 |
('interp1', 'interp2', 'extra'), |
| 1697 |
('interp1', 'interp2', 'main'), |
| 1698 |
] |
| 1699 |
for interp, other, extra in interpreters: |
| 1700 |
for creator in ('same', 'other', 'creator'): |
| 1701 |
for end in ('send', 'recv'): |
| 1702 |
yield ChannelCloseFixture(end, interp, other, extra, creator) |
| 1703 |
|
| 1704 |
def _close(self, fix, *, force): |
| 1705 |
op = 'force-close' if force else 'close' |
| 1706 |
close = ChannelAction(op, fix.end, 'same') |
| 1707 |
if not fix.expect_closed_error(): |
| 1708 |
self.run_action(fix, close, hideclosed=False) |
| 1709 |
else: |
| 1710 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1711 |
self.run_action(fix, close, hideclosed=False) |
| 1712 |
|
| 1713 |
def _assert_closed_in_interp(self, fix, interp=None): |
| 1714 |
if interp is None or interp.name == 'main': |
| 1715 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1716 |
_channels.recv(fix.cid) |
| 1717 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1718 |
_channels.send(fix.cid, b'spam') |
| 1719 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1720 |
_channels.close(fix.cid) |
| 1721 |
with self.assertRaises(_channels.ChannelClosedError): |
| 1722 |
_channels.close(fix.cid, force=True) |
| 1723 |
else: |
| 1724 |
run_interp(interp.id, """ |
| 1725 |
with helpers.expect_channel_closed(): |
| 1726 |
_channels.recv(cid) |
| 1727 |
""") |
| 1728 |
run_interp(interp.id, """ |
| 1729 |
with helpers.expect_channel_closed(): |
| 1730 |
_channels.send(cid, b'spam', blocking=False) |
| 1731 |
""") |
| 1732 |
run_interp(interp.id, """ |
| 1733 |
with helpers.expect_channel_closed(): |
| 1734 |
_channels.close(cid) |
| 1735 |
""") |
| 1736 |
run_interp(interp.id, """ |
| 1737 |
with helpers.expect_channel_closed(): |
| 1738 |
_channels.close(cid, force=True) |
| 1739 |
""") |
| 1740 |
|
| 1741 |
def _assert_closed(self, fix): |
| 1742 |
self.assertTrue(fix.state.closed) |
| 1743 |
|
| 1744 |
for _ in range(fix.state.pending): |
| 1745 |
recv_nowait(fix.cid) |
| 1746 |
self._assert_closed_in_interp(fix) |
| 1747 |
|
| 1748 |
for interp in ('same', 'other'): |
| 1749 |
interp = fix.get_interpreter(interp) |
| 1750 |
if interp.name == 'main': |
| 1751 |
continue |
| 1752 |
self._assert_closed_in_interp(fix, interp) |
| 1753 |
|
| 1754 |
interp = fix.get_interpreter('fresh') |
| 1755 |
self._assert_closed_in_interp(fix, interp) |
| 1756 |
|
| 1757 |
def _iter_close_tests(self, verbose=False): |
| 1758 |
i = 0 |
| 1759 |
for actions in self.iter_action_sets(): |
| 1760 |
print() |
| 1761 |
for fix in self.iter_fixtures(): |
| 1762 |
i += 1 |
| 1763 |
if i > 1000: |
| 1764 |
return |
| 1765 |
if verbose: |
| 1766 |
if (i - 1) % 6 == 0: |
| 1767 |
print() |
| 1768 |
print(i, fix, '({} actions)'.format(len(actions))) |
| 1769 |
else: |
| 1770 |
if (i - 1) % 6 == 0: |
| 1771 |
print(' ', end='') |
| 1772 |
print('.', end=''); sys.stdout.flush() |
| 1773 |
yield i, fix, actions |
| 1774 |
if verbose: |
| 1775 |
print('---') |
| 1776 |
print() |
| 1777 |
|
| 1778 |
# This is useful for scanning through the possible tests. |
| 1779 |
def _skim_close_tests(self): |
| 1780 |
ChannelCloseFixture.QUICK = True |
| 1781 |
for i, fix, actions in self._iter_close_tests(): |
| 1782 |
pass |
| 1783 |
|
| 1784 |
def test_close(self): |
| 1785 |
for i, fix, actions in self._iter_close_tests(): |
| 1786 |
with self.subTest('{} {} {}'.format(i, fix, actions)): |
| 1787 |
fix.prep_interpreter(fix.interp) |
| 1788 |
self.run_actions(fix, actions) |
| 1789 |
|
| 1790 |
self._close(fix, force=False) |
| 1791 |
|
| 1792 |
self._assert_closed(fix) |
| 1793 |
# XXX Things slow down if we have too many interpreters. |
| 1794 |
fix.clean_up() |
| 1795 |
|
| 1796 |
def test_force_close(self): |
| 1797 |
for i, fix, actions in self._iter_close_tests(): |
| 1798 |
with self.subTest('{} {} {}'.format(i, fix, actions)): |
| 1799 |
fix.prep_interpreter(fix.interp) |
| 1800 |
self.run_actions(fix, actions) |
| 1801 |
|
| 1802 |
self._close(fix, force=True) |
| 1803 |
|
| 1804 |
self._assert_closed(fix) |
| 1805 |
# XXX Things slow down if we have too many interpreters. |
| 1806 |
fix.clean_up() |
| 1807 |
|
| 1808 |
|
| 1809 |
if __name__ == '__main__': |
| 1810 |
unittest.main() |