def test_allocation_error(self, kwargs, mocker): """Test an error when no TCP port can be allocated from the given range""" mock_socket = mocker.patch("socket.socket") mock_socket.return_value.connect_ex.return_value = 0 # any port is unavailable pattern = "Cannot allocate an open TCP port for Kedro-Viz" with pytest.raises(ValueError, match=re.escape(pattern)): _allocate_port(**kwargs)
def test_allocate_from_viz_processes(self, mocker, viz_processes, kwargs, expected_port): """Test allocation of the port from the one that was already captured in _VIZ_PROCESSES""" mocker.patch.dict("kedro_viz.server._VIZ_PROCESSES", {k: None for k in viz_processes}) allocated_port = _allocate_port(**kwargs) assert allocated_port == expected_port
def test_allocate_from_available_ports(self, mocker, kwargs, expected_port): """Test allocation of one of unoccupied ports""" def _mock_even_port_available(host_and_port): # Mock availability of every even port number port = host_and_port[1] return 1 - port % 2 mock_socket = mocker.patch("socket.socket") mock_socket.return_value.connect_ex.side_effect = _mock_even_port_available allocated_port = _allocate_port(**kwargs) assert allocated_port == expected_port