|
| 1 | +# Copyright 2024 The MathWorks, Inc. |
| 2 | + |
| 3 | +import uuid |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from jupyter_matlab_kernel.mpm_kernel import MATLABKernelUsingMPM |
| 8 | +from jupyter_matlab_kernel.mwi_exceptions import MATLABConnectionError |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mpm_kernel_instance(mocker) -> MATLABKernelUsingMPM: |
| 13 | + # Mock logger |
| 14 | + mock_logger = mocker.Mock() |
| 15 | + |
| 16 | + # Use pytest-mock's mocker fixture to patch the function and log attribute |
| 17 | + mocker.patch( |
| 18 | + "jupyter_matlab_kernel.base_kernel.BaseMATLABKernel._extract_kernel_id_from_sys_args", |
| 19 | + return_value=uuid.uuid4().hex, |
| 20 | + ) |
| 21 | + mocker.patch( |
| 22 | + "jupyter_matlab_kernel.base_kernel.BaseMATLABKernel.log", new=mock_logger |
| 23 | + ) |
| 24 | + |
| 25 | + return MATLABKernelUsingMPM() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.asyncio |
| 29 | +async def test_initialize_matlab_proxy_with_mpm_success(mocker, mpm_kernel_instance): |
| 30 | + mpm_lib_start_matlab_proxy_response = { |
| 31 | + "absolute_url": "dummyURL", |
| 32 | + "mwi_base_url": "/matlab/dummy", |
| 33 | + "headers": "dummy_header", |
| 34 | + "mpm_auth_token": "dummy_token", |
| 35 | + } |
| 36 | + |
| 37 | + # Use pytest-mock's mocker fixture to patch the function |
| 38 | + mocker.patch( |
| 39 | + "matlab_proxy_manager.lib.api.start_matlab_proxy_for_kernel", |
| 40 | + return_value=mpm_lib_start_matlab_proxy_response, |
| 41 | + ) |
| 42 | + |
| 43 | + # Call the method and assert the result |
| 44 | + result = await mpm_kernel_instance._initialize_matlab_proxy_with_mpm( |
| 45 | + mpm_kernel_instance.log |
| 46 | + ) |
| 47 | + expected_response = tuple(mpm_lib_start_matlab_proxy_response.values()) |
| 48 | + assert result == expected_response |
| 49 | + |
| 50 | + |
| 51 | +async def test_initialize_matlab_proxy_with_mpm_exception(mocker, mpm_kernel_instance): |
| 52 | + # Use pytest-mock's mocker fixture to patch the function |
| 53 | + mocker.patch( |
| 54 | + "matlab_proxy_manager.lib.api.start_matlab_proxy_for_kernel", |
| 55 | + side_effect=Exception("Simulated failure"), |
| 56 | + ) |
| 57 | + |
| 58 | + with pytest.raises(MATLABConnectionError) as exc_info: |
| 59 | + await mpm_kernel_instance._initialize_matlab_proxy_with_mpm( |
| 60 | + mpm_kernel_instance.log |
| 61 | + ) |
| 62 | + assert ( |
| 63 | + "Error: MATLAB Kernel could not start the MATLAB proxy process via proxy manager." |
| 64 | + in str(exc_info.value) |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +async def test_initialize_mwi_comm_helper(mocker, mpm_kernel_instance): |
| 69 | + # Mock the necessary attributes |
| 70 | + mpm_kernel_instance.io_loop = mocker.Mock() |
| 71 | + mpm_kernel_instance.io_loop.asyncio_loop = mocker.Mock() |
| 72 | + |
| 73 | + mpm_kernel_instance.control_thread = mocker.Mock() |
| 74 | + mpm_kernel_instance.control_thread.io_loop = mocker.Mock() |
| 75 | + mpm_kernel_instance.control_thread.io_loop.asyncio_loop = mocker.Mock() |
| 76 | + |
| 77 | + # Mock MWICommHelper |
| 78 | + mock_mwi_comm_helper = mocker.patch( |
| 79 | + "jupyter_matlab_kernel.mpm_kernel.MWICommHelper", autospec=True |
| 80 | + ) |
| 81 | + mock_mwi_comm_helper_instance = mock_mwi_comm_helper.return_value |
| 82 | + mock_mwi_comm_helper_instance.connect = mocker.AsyncMock() |
| 83 | + |
| 84 | + # Test parameters |
| 85 | + murl = "http://proxy-url.com" |
| 86 | + headers = {"MWI_AUTH_TOKEN": "test_token"} |
| 87 | + |
| 88 | + # Run the test method |
| 89 | + await mpm_kernel_instance._initialize_mwi_comm_helper(murl, headers) |
| 90 | + |
| 91 | + # Assertions |
| 92 | + mock_mwi_comm_helper.assert_called_once_with( |
| 93 | + mpm_kernel_instance.kernel_id, |
| 94 | + murl, |
| 95 | + mpm_kernel_instance.io_loop.asyncio_loop, |
| 96 | + mpm_kernel_instance.control_thread.io_loop.asyncio_loop, |
| 97 | + headers, |
| 98 | + mpm_kernel_instance.log, |
| 99 | + ) |
| 100 | + mock_mwi_comm_helper_instance.connect.assert_awaited_once() |
| 101 | + |
| 102 | + # Verify that the mwi_comm_helper instance variable is set |
| 103 | + assert mpm_kernel_instance.mwi_comm_helper == mock_mwi_comm_helper_instance |
| 104 | + |
| 105 | + |
| 106 | +def test_process_children_return_empty_list(mpm_kernel_instance): |
| 107 | + assert mpm_kernel_instance._process_children() == [] |
| 108 | + |
| 109 | + |
| 110 | +async def test_do_shutdown_success(mocker, mpm_kernel_instance): |
| 111 | + mpm_kernel_instance.is_matlab_assigned = True |
| 112 | + |
| 113 | + # Mock the mwi_comm_helper and its methods |
| 114 | + mpm_kernel_instance.mwi_comm_helper = mocker.Mock() |
| 115 | + mpm_kernel_instance.mwi_comm_helper.send_shutdown_request_to_matlab = ( |
| 116 | + mocker.AsyncMock() |
| 117 | + ) |
| 118 | + mpm_kernel_instance.mwi_comm_helper.disconnect = mocker.AsyncMock() |
| 119 | + |
| 120 | + # Mock the mpm_lib.shutdown function |
| 121 | + mock_shutdown = mocker.patch( |
| 122 | + "matlab_proxy_manager.lib.api.shutdown", return_value=mocker.AsyncMock() |
| 123 | + ) |
| 124 | + # Call the method |
| 125 | + restart = False |
| 126 | + await mpm_kernel_instance.do_shutdown(restart) |
| 127 | + |
| 128 | + # Assertions |
| 129 | + mpm_kernel_instance.mwi_comm_helper.send_shutdown_request_to_matlab.assert_awaited_once() |
| 130 | + mpm_kernel_instance.mwi_comm_helper.disconnect.assert_awaited_once() |
| 131 | + mock_shutdown.assert_awaited_once_with( |
| 132 | + mpm_kernel_instance.parent_pid, |
| 133 | + mpm_kernel_instance.kernel_id, |
| 134 | + mpm_kernel_instance.mpm_auth_token, |
| 135 | + ) |
| 136 | + assert not mpm_kernel_instance.is_matlab_assigned |
| 137 | + mpm_kernel_instance.log.debug.assert_any_call( |
| 138 | + "Received shutdown request from Jupyter" |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +async def test_do_shutdown_exception(mocker, mpm_kernel_instance): |
| 143 | + mpm_kernel_instance.is_matlab_assigned = True |
| 144 | + |
| 145 | + # Mock the mwi_comm_helper and its methods |
| 146 | + mpm_kernel_instance.mwi_comm_helper = mocker.Mock() |
| 147 | + mpm_kernel_instance.mwi_comm_helper.send_shutdown_request_to_matlab = ( |
| 148 | + mocker.AsyncMock(side_effect=MATLABConnectionError("Test connection error")) |
| 149 | + ) |
| 150 | + mpm_kernel_instance.mwi_comm_helper.disconnect = mocker.AsyncMock() |
| 151 | + |
| 152 | + # Mock the mpm_lib.shutdown function |
| 153 | + mock_shutdown = mocker.patch( |
| 154 | + "matlab_proxy_manager.lib.api.shutdown", return_value=mocker.AsyncMock() |
| 155 | + ) |
| 156 | + # Call the method |
| 157 | + restart = False |
| 158 | + await mpm_kernel_instance.do_shutdown(restart) |
| 159 | + |
| 160 | + # Assertions |
| 161 | + mpm_kernel_instance.mwi_comm_helper.send_shutdown_request_to_matlab.assert_awaited_once() |
| 162 | + |
| 163 | + # Not awaited since there was an exception right above |
| 164 | + mpm_kernel_instance.mwi_comm_helper.disconnect.assert_not_awaited() |
| 165 | + mock_shutdown.assert_awaited_once_with( |
| 166 | + mpm_kernel_instance.parent_pid, |
| 167 | + mpm_kernel_instance.kernel_id, |
| 168 | + mpm_kernel_instance.mpm_auth_token, |
| 169 | + ) |
| 170 | + assert not mpm_kernel_instance.is_matlab_assigned |
0 commit comments