From 174c9a55f128a9d0df771b774b47a38602bafd2d Mon Sep 17 00:00:00 2001 From: Alex Carney Date: Sun, 27 Oct 2024 17:05:15 +0000 Subject: [PATCH] test: update test cases to align with event loop changes --- tests/ls_setup.py | 11 +++-------- tests/test_server_connection.py | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/tests/ls_setup.py b/tests/ls_setup.py index 1ad5b736..471d52f7 100644 --- a/tests/ls_setup.py +++ b/tests/ls_setup.py @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # # limitations under the License. # ############################################################################ -import asyncio import json import os import threading @@ -27,13 +26,12 @@ ClientCapabilities, InitializeParams, ) -from pygls.lsp.server import LanguageServer +from pygls.lsp.server import LanguageServer from . import CMD_ASYNC, CMD_SYNC, CMD_THREAD from ._init_server_stall_fix_hack import retry_stalled_init_fix_hack - CALL_TIMEOUT = 3 @@ -121,7 +119,7 @@ def __init__(self, LS=LanguageServer): self.server_thread.daemon = True # Setup client - self.client = LS("client", "v1", asyncio.new_event_loop()) + self.client = LS("client", "v1") self.client_thread = threading.Thread( name="Client Thread", target=self.client.start_io, @@ -145,10 +143,7 @@ def stop(self): self.client.protocol.notify(EXIT) self.server_thread.join() self.client._stop_event.set() - try: - self.client.loop._signal_handlers.clear() # HACK ? - except AttributeError: - pass + self.client_thread.join() @retry_stalled_init_fix_hack() diff --git a/tests/test_server_connection.py b/tests/test_server_connection.py index c5aaaf56..1cbfaf99 100644 --- a/tests/test_server_connection.py +++ b/tests/test_server_connection.py @@ -10,7 +10,7 @@ from pygls.lsp.server import LanguageServer try: - import websockets + from websockets.asyncio.client import connect WEBSOCKETS_AVAILABLE = True except ImportError: @@ -25,7 +25,6 @@ async def test_tcp_connection_lost(): server = LanguageServer("pygls-test", "v1", loop=loop) server.protocol.connection_made = Mock() - server.protocol.connection_lost = Mock() # Run the server over TCP in a separate thread server_thread = Thread( @@ -44,16 +43,18 @@ async def test_tcp_connection_lost(): # Simulate client's connection port = server._server.sockets[0].getsockname()[1] - reader, writer = await asyncio.open_connection("127.0.0.1", port) + _, writer = await asyncio.open_connection("127.0.0.1", port) await asyncio.sleep(1) assert server.protocol.connection_made.called # Socket is closed (client's process is terminated) writer.close() - await asyncio.sleep(1) + await writer.wait_closed() - assert server.protocol.connection_lost.called + # Give the server chance to shutdown. + await asyncio.sleep(1) + assert server._stop_event.is_set() @pytest.mark.asyncio @@ -64,7 +65,7 @@ async def test_io_connection_lost(): # Server to client pipe. scr, scw = os.pipe() - server = LanguageServer("pygls-test", "v1", loop=asyncio.new_event_loop()) + server = LanguageServer("pygls-test", "v1") server.protocol.connection_made = Mock() server_thread = Thread( target=server.start_io, args=(os.fdopen(csr, "rb"), os.fdopen(scw, "wb")) @@ -89,8 +90,7 @@ async def test_io_connection_lost(): async def test_ws_server(): """Smoke test to ensure we can send/receive messages over websockets""" - loop = asyncio.new_event_loop() - server = LanguageServer("pygls-test", "v1", loop=loop) + server = LanguageServer("pygls-test", "v1") # Run the server over Websockets in a separate thread server_thread = Thread( @@ -107,9 +107,9 @@ async def test_ws_server(): while server._server is None: await asyncio.sleep(0.5) - port = server._server.sockets[0].getsockname()[1] + port = list(server._server.sockets)[0].getsockname()[1] # Simulate client's connection - async with websockets.connect(f"ws://127.0.0.1:{port}") as connection: + async with connect(f"ws://127.0.0.1:{port}") as connection: # Send an 'initialize' request msg = dict( jsonrpc="2.0", id=1, method="initialize", params=dict(capabilities=dict())