|
3 | 3 |
|
4 | 4 | import os
|
5 | 5 | import time
|
6 |
| -import multiprocessing |
7 | 6 | import sys
|
8 |
| -from threading import Thread |
| 7 | + |
| 8 | +from test.test_utils import ClientServerPair |
9 | 9 |
|
10 | 10 | from flaky import flaky
|
11 | 11 | from pylsp_jsonrpc.exceptions import JsonRpcMethodNotFound
|
12 | 12 | import pytest
|
13 | 13 |
|
14 |
| -from pylsp.python_lsp import start_io_lang_server, PythonLSPServer |
15 |
| - |
16 |
| -CALL_TIMEOUT = 10 |
17 |
| -RUNNING_IN_CI = bool(os.environ.get('CI')) |
18 |
| - |
19 |
| - |
20 |
| -def start_client(client): |
21 |
| - client.start() |
22 |
| - |
23 |
| - |
24 |
| -class _ClientServer: |
25 |
| - """ A class to setup a client/server pair """ |
26 |
| - def __init__(self, check_parent_process=False): |
27 |
| - # Client to Server pipe |
28 |
| - csr, csw = os.pipe() |
29 |
| - # Server to client pipe |
30 |
| - scr, scw = os.pipe() |
31 |
| - |
32 |
| - if os.name == 'nt': |
33 |
| - ParallelKind = Thread |
34 |
| - else: |
35 |
| - if sys.version_info[:2] >= (3, 8): |
36 |
| - ParallelKind = multiprocessing.get_context("fork").Process |
37 |
| - else: |
38 |
| - ParallelKind = multiprocessing.Process |
39 |
| - |
40 |
| - self.process = ParallelKind(target=start_io_lang_server, args=( |
41 |
| - os.fdopen(csr, 'rb'), os.fdopen(scw, 'wb'), check_parent_process, PythonLSPServer |
42 |
| - )) |
43 |
| - self.process.start() |
44 |
| - |
45 |
| - self.client = PythonLSPServer(os.fdopen(scr, 'rb'), os.fdopen(csw, 'wb'), start_io_lang_server) |
46 |
| - self.client_thread = Thread(target=start_client, args=[self.client]) |
47 |
| - self.client_thread.daemon = True |
48 |
| - self.client_thread.start() |
49 |
| - |
50 |
| - |
51 |
| -@pytest.fixture |
52 |
| -def client_server(): |
53 |
| - """ A fixture that sets up a client/server pair and shuts down the server |
54 |
| - This client/server pair does not support checking parent process aliveness |
55 |
| - """ |
56 |
| - client_server_pair = _ClientServer() |
57 | 14 |
|
58 |
| - yield client_server_pair.client |
| 15 | +RUNNING_IN_CI = bool(os.environ.get("CI")) |
59 | 16 |
|
60 |
| - shutdown_response = client_server_pair.client._endpoint.request('shutdown').result(timeout=CALL_TIMEOUT) |
61 |
| - assert shutdown_response is None |
62 |
| - client_server_pair.client._endpoint.notify('exit') |
| 17 | +CALL_TIMEOUT_IN_SECONDS = 10 |
63 | 18 |
|
64 | 19 |
|
65 | 20 | @pytest.fixture
|
66 | 21 | def client_exited_server():
|
67 |
| - """ A fixture that sets up a client/server pair that support checking parent process aliveness |
| 22 | + """A fixture that sets up a client/server pair that support checking parent process aliveness |
68 | 23 | and assert the server has already exited
|
69 | 24 | """
|
70 |
| - client_server_pair = _ClientServer(True) |
| 25 | + client_server_pair_obj = ClientServerPair(True, True) |
71 | 26 |
|
72 |
| - # yield client_server_pair.client |
73 |
| - yield client_server_pair |
| 27 | + yield client_server_pair_obj |
74 | 28 |
|
75 |
| - assert client_server_pair.process.is_alive() is False |
| 29 | + assert client_server_pair_obj.server_process.is_alive() is False |
76 | 30 |
|
77 | 31 |
|
78 | 32 | @flaky(max_runs=10, min_passes=1)
|
79 |
| -@pytest.mark.skipif(sys.platform == 'darwin', reason='Too flaky on Mac') |
80 |
| -def test_initialize(client_server): # pylint: disable=redefined-outer-name |
81 |
| - response = client_server._endpoint.request('initialize', { |
82 |
| - 'rootPath': os.path.dirname(__file__), |
83 |
| - 'initializationOptions': {} |
84 |
| - }).result(timeout=CALL_TIMEOUT) |
85 |
| - assert 'capabilities' in response |
| 33 | +@pytest.mark.skipif(sys.platform == "darwin", reason="Too flaky on Mac") |
| 34 | +def test_initialize(client_server_pair): |
| 35 | + client, _ = client_server_pair |
| 36 | + response = client._endpoint.request( |
| 37 | + "initialize", |
| 38 | + {"rootPath": os.path.dirname(__file__), "initializationOptions": {}}, |
| 39 | + ).result(timeout=CALL_TIMEOUT_IN_SECONDS) |
| 40 | + assert "capabilities" in response |
86 | 41 |
|
87 | 42 |
|
88 | 43 | @flaky(max_runs=10, min_passes=1)
|
89 |
| -@pytest.mark.skipif(not sys.platform.startswith('Linux'), reason='Skipped on win and flaky on mac') |
90 |
| -def test_exit_with_parent_process_died(client_exited_server): # pylint: disable=redefined-outer-name |
| 44 | +@pytest.mark.skipif( |
| 45 | + not sys.platform.startswith("Linux"), reason="Skipped on win and flaky on mac" |
| 46 | +) |
| 47 | +def test_exit_with_parent_process_died( |
| 48 | + client_exited_server, |
| 49 | +): # pylint: disable=redefined-outer-name |
91 | 50 | # language server should have already exited before responding
|
92 |
| - lsp_server, mock_process = client_exited_server.client, client_exited_server.process |
| 51 | + lsp_server, mock_process = ( |
| 52 | + client_exited_server.client, |
| 53 | + client_exited_server.server_process, |
| 54 | + ) |
93 | 55 | # with pytest.raises(Exception):
|
94 |
| - lsp_server._endpoint.request('initialize', { |
95 |
| - 'processId': mock_process.pid, |
96 |
| - 'rootPath': os.path.dirname(__file__), |
97 |
| - 'initializationOptions': {} |
98 |
| - }).result(timeout=CALL_TIMEOUT) |
| 56 | + lsp_server._endpoint.request( |
| 57 | + "initialize", |
| 58 | + { |
| 59 | + "processId": mock_process.pid, |
| 60 | + "rootPath": os.path.dirname(__file__), |
| 61 | + "initializationOptions": {}, |
| 62 | + }, |
| 63 | + ).result(timeout=CALL_TIMEOUT_IN_SECONDS) |
99 | 64 |
|
100 | 65 | mock_process.terminate()
|
101 |
| - time.sleep(CALL_TIMEOUT) |
| 66 | + time.sleep(CALL_TIMEOUT_IN_SECONDS) |
102 | 67 | assert not client_exited_server.client_thread.is_alive()
|
103 | 68 |
|
104 | 69 |
|
105 | 70 | @flaky(max_runs=10, min_passes=1)
|
106 |
| -@pytest.mark.skipif(sys.platform.startswith('linux'), reason='Fails on linux') |
107 |
| -def test_not_exit_without_check_parent_process_flag(client_server): # pylint: disable=redefined-outer-name |
108 |
| - response = client_server._endpoint.request('initialize', { |
109 |
| - 'processId': 1234, |
110 |
| - 'rootPath': os.path.dirname(__file__), |
111 |
| - 'initializationOptions': {} |
112 |
| - }).result(timeout=CALL_TIMEOUT) |
113 |
| - assert 'capabilities' in response |
| 71 | +@pytest.mark.skipif(sys.platform.startswith("linux"), reason="Fails on linux") |
| 72 | +def test_not_exit_without_check_parent_process_flag( |
| 73 | + client_server_pair, |
| 74 | +): |
| 75 | + client, _ = client_server_pair |
| 76 | + response = client._endpoint.request( |
| 77 | + "initialize", |
| 78 | + { |
| 79 | + "processId": 1234, |
| 80 | + "rootPath": os.path.dirname(__file__), |
| 81 | + "initializationOptions": {}, |
| 82 | + }, |
| 83 | + ).result(timeout=CALL_TIMEOUT_IN_SECONDS) |
| 84 | + assert "capabilities" in response |
114 | 85 |
|
115 | 86 |
|
116 | 87 | @flaky(max_runs=10, min_passes=1)
|
117 |
| -@pytest.mark.skipif(RUNNING_IN_CI, reason='This test is hanging on CI') |
118 |
| -def test_missing_message(client_server): # pylint: disable=redefined-outer-name |
| 88 | +@pytest.mark.skipif(RUNNING_IN_CI, reason="This test is hanging on CI") |
| 89 | +def test_missing_message(client_server_pair): |
| 90 | + client, _ = client_server_pair |
119 | 91 | with pytest.raises(JsonRpcMethodNotFound):
|
120 |
| - client_server._endpoint.request('unknown_method').result(timeout=CALL_TIMEOUT) |
| 92 | + client._endpoint.request("unknown_method").result( |
| 93 | + timeout=CALL_TIMEOUT_IN_SECONDS |
| 94 | + ) |
0 commit comments