|
| 1 | +# SPDX-License-Identifier: AGPL-3.0 |
| 2 | + |
| 3 | +from unittest.mock import Mock |
| 4 | + |
| 5 | +from halmos.processes import PopenFuture, get_global_executor |
| 6 | + |
| 7 | + |
| 8 | +def test_popen_future_with_tag(): |
| 9 | + """Test that PopenFuture accepts and stores tag parameter.""" |
| 10 | + cmd = ["echo", "hello"] |
| 11 | + tag = "test-tag" |
| 12 | + |
| 13 | + future = PopenFuture(cmd, tag) |
| 14 | + |
| 15 | + assert future.cmd == cmd |
| 16 | + assert future.tag == tag |
| 17 | + |
| 18 | + |
| 19 | +def test_popen_future_with_minimal_args(): |
| 20 | + """Test that PopenFuture works with minimal required parameters.""" |
| 21 | + cmd = ["echo", "hello"] |
| 22 | + tag = "test-minimal" |
| 23 | + |
| 24 | + future = PopenFuture(cmd, tag) |
| 25 | + |
| 26 | + assert future.cmd == cmd |
| 27 | + assert future.tag == tag |
| 28 | + |
| 29 | + |
| 30 | +def test_popen_future_empty_tag_assertion(): |
| 31 | + """Test that PopenFuture raises assertion error for empty tag.""" |
| 32 | + cmd = ["echo", "hello"] |
| 33 | + |
| 34 | + try: |
| 35 | + PopenFuture(cmd, "") |
| 36 | + raise AssertionError("Expected AssertionError for empty tag") |
| 37 | + except AssertionError: |
| 38 | + pass # Expected |
| 39 | + |
| 40 | + |
| 41 | +def test_interrupt_by_tag(): |
| 42 | + """Test that interrupt() cancels futures with matching tags.""" |
| 43 | + executor = get_global_executor() |
| 44 | + |
| 45 | + # Create mock futures with different tags |
| 46 | + future1 = Mock(spec=PopenFuture) |
| 47 | + future1.tag = "tag1" |
| 48 | + future2 = Mock(spec=PopenFuture) |
| 49 | + future2.tag = "tag2" |
| 50 | + future3 = Mock(spec=PopenFuture) |
| 51 | + future3.tag = "tag1" |
| 52 | + future4 = Mock(spec=PopenFuture) |
| 53 | + future4.tag = "tag3" |
| 54 | + |
| 55 | + # Add to executor's futures list |
| 56 | + executor._futures = [future1, future2, future3, future4] |
| 57 | + |
| 58 | + # Interrupt tag1 |
| 59 | + executor.interrupt("tag1") |
| 60 | + |
| 61 | + # Check that only futures with tag1 were cancelled |
| 62 | + future1.cancel.assert_called_once() |
| 63 | + future2.cancel.assert_not_called() |
| 64 | + future3.cancel.assert_called_once() |
| 65 | + future4.cancel.assert_not_called() |
| 66 | + |
| 67 | + |
| 68 | +def test_interrupt_nonexistent_tag(): |
| 69 | + """Test that interrupt() with non-existent tag does nothing.""" |
| 70 | + executor = get_global_executor() |
| 71 | + |
| 72 | + # Create mock future |
| 73 | + future = Mock(spec=PopenFuture) |
| 74 | + future.tag = "existing-tag" |
| 75 | + executor._futures = [future] |
| 76 | + |
| 77 | + # Interrupt with non-existent tag |
| 78 | + executor.interrupt("nonexistent-tag") |
| 79 | + |
| 80 | + # No futures should be cancelled |
| 81 | + future.cancel.assert_not_called() |
0 commit comments