(self)
| 1491 | |
| 1492 | @pytest.mark.threading |
| 1493 | def test_cancellation(self): |
| 1494 | if (threading.current_thread().ident != |
| 1495 | threading.main_thread().ident): |
| 1496 | pytest.skip("test only works from main Python thread") |
| 1497 | |
| 1498 | def signal_from_thread(): |
| 1499 | # Give our workload a chance to start up |
| 1500 | time.sleep(0.2) |
| 1501 | signal.raise_signal(signal.SIGINT) |
| 1502 | |
| 1503 | # We start with a small CSV reading workload and increase its size |
| 1504 | # until it's large enough to get an interruption during it, even in |
| 1505 | # release mode on fast machines. |
| 1506 | last_duration = 0.0 |
| 1507 | workload_size = 100_000 |
| 1508 | attempts = 0 |
| 1509 | |
| 1510 | while last_duration < 5.0 and attempts < 10: |
| 1511 | print("workload size:", workload_size) |
| 1512 | large_csv = b"a,b,c\n" + b"1,2,3\n" * workload_size |
| 1513 | exc_info = None |
| 1514 | |
| 1515 | try: |
| 1516 | # We use a signal fd to reliably ensure that the signal |
| 1517 | # has been delivered to Python, regardless of how exactly |
| 1518 | # it was caught. |
| 1519 | with util.signal_wakeup_fd() as sigfd: |
| 1520 | try: |
| 1521 | t = threading.Thread(target=signal_from_thread) |
| 1522 | t.start() |
| 1523 | t1 = time.time() |
| 1524 | try: |
| 1525 | self.read_bytes(large_csv) |
| 1526 | except KeyboardInterrupt as e: |
| 1527 | exc_info = e |
| 1528 | last_duration = time.time() - t1 |
| 1529 | finally: |
| 1530 | # Wait for signal to arrive if it didn't already, |
| 1531 | # to avoid getting a KeyboardInterrupt after the |
| 1532 | # `except` block below. |
| 1533 | select.select([sigfd], [], [sigfd], 10.0) |
| 1534 | |
| 1535 | except KeyboardInterrupt: |
| 1536 | # KeyboardInterrupt didn't interrupt `read_bytes` above. |
| 1537 | pass |
| 1538 | |
| 1539 | if exc_info is not None: |
| 1540 | # We managed to get `self.read_bytes` interrupted, see if it |
| 1541 | # was actually interrupted inside Arrow C++ or in the Python |
| 1542 | # scaffolding. |
| 1543 | if exc_info.__context__ is not None: |
| 1544 | # Interrupted inside Arrow C++, we're satisfied now |
| 1545 | break |
| 1546 | |
| 1547 | # Increase workload size to get a better chance |
| 1548 | workload_size = workload_size * 3 |
| 1549 | |
| 1550 | if exc_info is None: |
nothing calls this directly
no test coverage detected