|
| 1 | + |
| 2 | +import pytest |
| 3 | + |
| 4 | +from qcodes.dataset.dond.do_nd_utils import BreakConditionInterrupt, catch_interrupts |
| 5 | + |
| 6 | + |
| 7 | +def test_catch_interrupts(): |
| 8 | + # Test normal execution (no interrupt) |
| 9 | + with catch_interrupts() as get_interrupt: |
| 10 | + assert get_interrupt() is None |
| 11 | + |
| 12 | + # Test KeyboardInterrupt |
| 13 | + with pytest.raises(KeyboardInterrupt): |
| 14 | + with catch_interrupts() as get_interrupt: |
| 15 | + raise KeyboardInterrupt() |
| 16 | + |
| 17 | + # Test BreakConditionInterrupt |
| 18 | + with catch_interrupts() as get_interrupt: |
| 19 | + raise BreakConditionInterrupt() |
| 20 | + assert isinstance(get_interrupt(), BreakConditionInterrupt) |
| 21 | + |
| 22 | + # Test that cleanup code runs for KeyboardInterrupt |
| 23 | + cleanup_ran = False |
| 24 | + with pytest.raises(KeyboardInterrupt): |
| 25 | + with catch_interrupts(): |
| 26 | + try: |
| 27 | + raise KeyboardInterrupt() |
| 28 | + finally: |
| 29 | + cleanup_ran = True |
| 30 | + assert cleanup_ran |
| 31 | + |
| 32 | + # Test that cleanup code runs for BreakConditionInterrupt |
| 33 | + cleanup_ran = False |
| 34 | + with catch_interrupts(): |
| 35 | + try: |
| 36 | + raise BreakConditionInterrupt() |
| 37 | + finally: |
| 38 | + cleanup_ran = True |
| 39 | + assert cleanup_ran |
| 40 | + |
| 41 | + # Test that BreakConditionInterrupt is caught and doesn't raise |
| 42 | + with catch_interrupts() as get_interrupt: |
| 43 | + raise BreakConditionInterrupt() |
| 44 | + assert isinstance(get_interrupt(), BreakConditionInterrupt) |
| 45 | + |
| 46 | + |
| 47 | +def test_catch_interrupts_in_loops(): |
| 48 | + # Test interruption in a simple loop |
| 49 | + loop_count = 0 |
| 50 | + with pytest.raises(KeyboardInterrupt): |
| 51 | + for i in range(5): |
| 52 | + with catch_interrupts(): |
| 53 | + loop_count += 1 |
| 54 | + if i == 2: |
| 55 | + raise KeyboardInterrupt() |
| 56 | + assert loop_count == 3 # Loop should stop at the third iteration |
| 57 | + |
| 58 | + # Test interruption in nested loops |
| 59 | + outer_count = 0 |
| 60 | + inner_count = 0 |
| 61 | + with pytest.raises(KeyboardInterrupt): |
| 62 | + for i in range(3): |
| 63 | + with catch_interrupts(): |
| 64 | + outer_count += 1 |
| 65 | + for j in range(3): |
| 66 | + with catch_interrupts(): |
| 67 | + inner_count += 1 |
| 68 | + if i == 1 and j == 1: |
| 69 | + raise KeyboardInterrupt() |
| 70 | + assert outer_count == 2 |
| 71 | + assert inner_count == 5 |
| 72 | + |
| 73 | + |
| 74 | +def test_catch_interrupts_simulated_sweeps(): |
| 75 | + def simulated_sweep(interrupt_at=None): |
| 76 | + for i in range(5): |
| 77 | + with catch_interrupts(): |
| 78 | + if i == interrupt_at: |
| 79 | + raise KeyboardInterrupt() |
| 80 | + yield i |
| 81 | + |
| 82 | + # Test interruption in a single sweep |
| 83 | + results = [] |
| 84 | + with pytest.raises(KeyboardInterrupt): |
| 85 | + for value in simulated_sweep(interrupt_at=3): |
| 86 | + results.append(value) |
| 87 | + assert results == [0, 1, 2] |
| 88 | + |
| 89 | + # Test interruption in nested sweeps |
| 90 | + outer_results = [] |
| 91 | + inner_results = [] |
| 92 | + with pytest.raises(KeyboardInterrupt): |
| 93 | + for outer_value in simulated_sweep(interrupt_at=None): |
| 94 | + outer_results.append(outer_value) |
| 95 | + for inner_value in simulated_sweep( |
| 96 | + interrupt_at=2 if outer_value == 1 else None |
| 97 | + ): |
| 98 | + inner_results.append(inner_value) |
| 99 | + assert outer_results == [0, 1] |
| 100 | + assert inner_results == [0, 1, 2, 3, 4, 0, 1] |
0 commit comments