|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import datachain as dc |
| 6 | +from datachain.catalog import ( |
| 7 | + QUERY_SCRIPT_ABORTED_EXIT_CODE, |
| 8 | + QUERY_SCRIPT_CANCELED_EXIT_CODE, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def _make_mock_popen(poll_return_value=0, communicate_side_effect=None): |
| 13 | + mock_process = MagicMock() |
| 14 | + mock_process.__enter__ = MagicMock(return_value=mock_process) |
| 15 | + mock_process.__exit__ = MagicMock(return_value=False) |
| 16 | + if communicate_side_effect: |
| 17 | + mock_process.communicate.side_effect = communicate_side_effect |
| 18 | + else: |
| 19 | + mock_process.communicate.return_value = (b"", b"") |
| 20 | + mock_process.poll.return_value = poll_return_value |
| 21 | + return mock_process |
| 22 | + |
| 23 | + |
| 24 | +def _build_parallel_chain(session): |
| 25 | + def identity(x: int) -> int: |
| 26 | + return x |
| 27 | + |
| 28 | + return ( |
| 29 | + dc.read_values(x=list(range(5)), session=session) |
| 30 | + .settings(parallel=2) |
| 31 | + .map(identity, output={"result": int}) |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def test_aborted_exit_code_causes_sys_exit(test_session_tmpfile, monkeypatch): |
| 36 | + monkeypatch.delenv("DATACHAIN_DISTRIBUTED", raising=False) |
| 37 | + chain = _build_parallel_chain(test_session_tmpfile) |
| 38 | + |
| 39 | + mock_process = _make_mock_popen(poll_return_value=QUERY_SCRIPT_ABORTED_EXIT_CODE) |
| 40 | + |
| 41 | + with patch("datachain.query.dataset.subprocess.Popen", return_value=mock_process): |
| 42 | + with pytest.raises(SystemExit) as exc_info: |
| 43 | + chain.to_list() |
| 44 | + |
| 45 | + assert exc_info.value.code == QUERY_SCRIPT_ABORTED_EXIT_CODE |
| 46 | + |
| 47 | + |
| 48 | +def test_canceled_exit_code_causes_sys_exit(test_session_tmpfile, monkeypatch): |
| 49 | + monkeypatch.delenv("DATACHAIN_DISTRIBUTED", raising=False) |
| 50 | + chain = _build_parallel_chain(test_session_tmpfile) |
| 51 | + |
| 52 | + mock_process = _make_mock_popen(poll_return_value=QUERY_SCRIPT_CANCELED_EXIT_CODE) |
| 53 | + |
| 54 | + with patch("datachain.query.dataset.subprocess.Popen", return_value=mock_process): |
| 55 | + with pytest.raises(SystemExit) as exc_info: |
| 56 | + chain.to_list() |
| 57 | + |
| 58 | + assert exc_info.value.code == QUERY_SCRIPT_CANCELED_EXIT_CODE |
| 59 | + |
| 60 | + |
| 61 | +def test_unknown_exit_code_raises_runtime_error(test_session_tmpfile, monkeypatch): |
| 62 | + monkeypatch.delenv("DATACHAIN_DISTRIBUTED", raising=False) |
| 63 | + chain = _build_parallel_chain(test_session_tmpfile) |
| 64 | + |
| 65 | + mock_process = _make_mock_popen(poll_return_value=42) |
| 66 | + |
| 67 | + with patch("datachain.query.dataset.subprocess.Popen", return_value=mock_process): |
| 68 | + with pytest.raises(RuntimeError, match="UDF Execution Failed! Exit code: 42"): |
| 69 | + chain.to_list() |
| 70 | + |
| 71 | + |
| 72 | +def test_keyboard_interrupt_causes_cancel_sys_exit(test_session_tmpfile, monkeypatch): |
| 73 | + monkeypatch.delenv("DATACHAIN_DISTRIBUTED", raising=False) |
| 74 | + chain = _build_parallel_chain(test_session_tmpfile) |
| 75 | + |
| 76 | + mock_process = _make_mock_popen( |
| 77 | + communicate_side_effect=KeyboardInterrupt("simulated ctrl-c") |
| 78 | + ) |
| 79 | + |
| 80 | + with patch("datachain.query.dataset.subprocess.Popen", return_value=mock_process): |
| 81 | + with pytest.raises(SystemExit) as exc_info: |
| 82 | + chain.to_list() |
| 83 | + |
| 84 | + assert exc_info.value.code == QUERY_SCRIPT_CANCELED_EXIT_CODE |
| 85 | + |
| 86 | + |
| 87 | +def test_aborted_exit_code_closes_warehouse(test_session_tmpfile, monkeypatch): |
| 88 | + monkeypatch.delenv("DATACHAIN_DISTRIBUTED", raising=False) |
| 89 | + chain = _build_parallel_chain(test_session_tmpfile) |
| 90 | + |
| 91 | + mock_process = _make_mock_popen(poll_return_value=QUERY_SCRIPT_ABORTED_EXIT_CODE) |
| 92 | + warehouse = test_session_tmpfile.catalog.warehouse |
| 93 | + original_close = warehouse.close |
| 94 | + close_called = False |
| 95 | + |
| 96 | + def tracking_close(): |
| 97 | + nonlocal close_called |
| 98 | + close_called = True |
| 99 | + original_close() |
| 100 | + |
| 101 | + monkeypatch.setattr(warehouse, "close", tracking_close) |
| 102 | + |
| 103 | + with patch("datachain.query.dataset.subprocess.Popen", return_value=mock_process): |
| 104 | + with pytest.raises(SystemExit): |
| 105 | + chain.to_list() |
| 106 | + |
| 107 | + assert close_called |
| 108 | + |
| 109 | + |
| 110 | +def test_canceled_exit_code_closes_warehouse(test_session_tmpfile, monkeypatch): |
| 111 | + monkeypatch.delenv("DATACHAIN_DISTRIBUTED", raising=False) |
| 112 | + chain = _build_parallel_chain(test_session_tmpfile) |
| 113 | + |
| 114 | + mock_process = _make_mock_popen(poll_return_value=QUERY_SCRIPT_CANCELED_EXIT_CODE) |
| 115 | + warehouse = test_session_tmpfile.catalog.warehouse |
| 116 | + original_close = warehouse.close |
| 117 | + close_called = False |
| 118 | + |
| 119 | + def tracking_close(): |
| 120 | + nonlocal close_called |
| 121 | + close_called = True |
| 122 | + original_close() |
| 123 | + |
| 124 | + monkeypatch.setattr(warehouse, "close", tracking_close) |
| 125 | + |
| 126 | + with patch("datachain.query.dataset.subprocess.Popen", return_value=mock_process): |
| 127 | + with pytest.raises(SystemExit): |
| 128 | + chain.to_list() |
| 129 | + |
| 130 | + assert close_called |
0 commit comments