-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lsp-devtools: Ensure the agent stops once the server process exits
- Loading branch information
Showing
5 changed files
with
159 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The `lsp-devtools agent` now watches for the when the server process exits and closes itself down also. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""A very simple language server.""" | ||
from lsprotocol import types | ||
from pygls.server import LanguageServer | ||
|
||
server = LanguageServer("simple-server", "v1") | ||
|
||
|
||
@server.feature(types.INITIALIZED) | ||
def _(ls: LanguageServer, params: types.InitializedParams): | ||
ls.show_message("Hello, world!") | ||
|
||
|
||
if __name__ == "__main__": | ||
server.start_io() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import asyncio | ||
import json | ||
import os | ||
import pathlib | ||
import subprocess | ||
import sys | ||
|
||
import pytest | ||
|
||
from lsp_devtools.agent import Agent | ||
|
||
SERVER_DIR = pathlib.Path(__file__).parent / "servers" | ||
|
||
|
||
def format_message(obj): | ||
content = json.dumps(obj) | ||
message = "".join( | ||
[ | ||
f"Content-Length: {len(content)}\r\n", | ||
"\r\n", | ||
f"{content}", | ||
] | ||
) | ||
return message.encode() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_agent_exits(): | ||
"""Ensure that when the client closes down the lsp session and the server process | ||
exits, the agent does also.""" | ||
|
||
(stdin_read, stdin_write) = os.pipe() | ||
(stdout_read, stdout_write) = os.pipe() | ||
|
||
server = await asyncio.create_subprocess_exec( | ||
sys.executable, | ||
str(SERVER_DIR / "simple.py"), | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
|
||
agent = Agent( | ||
server, | ||
os.fdopen(stdin_read, mode="rb"), | ||
os.fdopen(stdout_write, mode="wb"), | ||
) | ||
|
||
os.write( | ||
stdin_write, | ||
format_message( | ||
dict(jsonrpc="2.0", id=1, method="initialize", params=dict(capabilities={})) | ||
), | ||
) | ||
|
||
os.write( | ||
stdin_write, | ||
format_message(dict(jsonrpc="2.0", id=2, method="shutdown", params=None)), | ||
) | ||
|
||
os.write( | ||
stdin_write, | ||
format_message(dict(jsonrpc="2.0", method="exit", params=None)), | ||
) | ||
|
||
try: | ||
await asyncio.wait_for( | ||
# asyncio.gather(server.wait(), agent.start()), | ||
agent.start(), | ||
timeout=10, # s | ||
) | ||
except asyncio.CancelledError: | ||
pass # The agent's tasks should be cancelled | ||
|
||
except TimeoutError as exc: | ||
# Make sure this timed out for the right reason. | ||
if server.returncode is None: | ||
raise RuntimeError("Server process did not exit") | ||
else: | ||
exc.add_note("lsp-devtools agent did not stop") | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters