1
1
import asyncio
2
2
import subprocess
3
- import time
4
- from threading import Thread
5
3
6
4
import pytest
7
5
from tornado .queues import Queue
@@ -34,9 +32,7 @@ def spawn_writer(self, message: str, repeats: int = 1, interval=None):
34
32
)
35
33
)
36
34
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
40
36
)
41
37
42
38
@@ -45,13 +41,12 @@ def communicator_spawner(tmp_path):
45
41
return CommunicatorSpawner (tmp_path )
46
42
47
43
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
55
50
56
51
57
52
@pytest .mark .parametrize (
@@ -72,10 +67,8 @@ async def test_reader(message, repeats, interval, communicator_spawner):
72
67
message = message , repeats = repeats , interval = interval
73
68
)
74
69
reader = LspStdIoReader (stream = process .stdout , queue = queue )
75
- timeout = 3 + reader .max_wait * repeats * 10
76
70
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 ())
79
72
80
73
result = queue .get_nowait ()
81
74
assert result == message * repeats
0 commit comments