Skip to content

Commit

Permalink
test: update test cases to align with event loop changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Oct 28, 2024
1 parent 18b7e68 commit 174c9a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
11 changes: 3 additions & 8 deletions tests/ls_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down
20 changes: 10 additions & 10 deletions tests/test_server_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pygls.lsp.server import LanguageServer

try:
import websockets
from websockets.asyncio.client import connect

WEBSOCKETS_AVAILABLE = True
except ImportError:
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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"))
Expand All @@ -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(
Expand All @@ -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())
Expand Down

0 comments on commit 174c9a5

Please sign in to comment.