Skip to content

Commit 8de3cfd

Browse files
committed
Reimplement test without threads
1 parent 2152eae commit 8de3cfd

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

python_packages/jupyter_lsp/jupyter_lsp/tests/test_stdio.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import asyncio
22
import subprocess
3-
import time
4-
from threading import Thread
53

64
import pytest
75
from tornado.queues import Queue
@@ -34,9 +32,7 @@ def spawn_writer(self, message: str, repeats: int = 1, interval=None):
3432
)
3533
)
3634
return subprocess.Popen(
37-
["python", "-u", str(commands_file)],
38-
stdin=subprocess.PIPE,
39-
stdout=subprocess.PIPE,
35+
["python", "-u", str(commands_file)], stdout=subprocess.PIPE
4036
)
4137

4238

@@ -45,13 +41,12 @@ def communicator_spawner(tmp_path):
4541
return CommunicatorSpawner(tmp_path)
4642

4743

48-
def communicate_and_close(process, wait=1):
49-
def communicate_and_close():
50-
time.sleep(wait)
51-
process.communicate()
52-
53-
thread = Thread(target=communicate_and_close)
54-
thread.start()
44+
async def join_process(process: subprocess.Popen, headstart=1, timeout=1):
45+
await asyncio.sleep(headstart)
46+
result = process.wait(timeout=timeout)
47+
if process.stdout:
48+
process.stdout.close()
49+
return result
5550

5651

5752
@pytest.mark.parametrize(
@@ -72,10 +67,8 @@ async def test_reader(message, repeats, interval, communicator_spawner):
7267
message=message, repeats=repeats, interval=interval
7368
)
7469
reader = LspStdIoReader(stream=process.stdout, queue=queue)
75-
timeout = 3 + reader.max_wait * repeats * 10
7670

77-
communicate_and_close(process)
78-
await asyncio.wait_for(reader.read(), timeout=timeout)
71+
await asyncio.gather(join_process(process, headstart=3, timeout=1), reader.read())
7972

8073
result = queue.get_nowait()
8174
assert result == message * repeats

0 commit comments

Comments
 (0)