Skip to content

Commit 9755840

Browse files
authored
Merge pull request #221 from Zsailer/terminal-test-failures
add fixture to kill all terminals after each test
2 parents 15dd628 + dfd6ab0 commit 9755840

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

tests/test_terminal.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
pytest.skip("Terminal API tests time out on Windows.", allow_module_level=True)
1212

1313

14+
# Kill all running terminals after each test to avoid cross-test issues
15+
# with still running terminals.
16+
@pytest.fixture
17+
def kill_all(serverapp):
18+
async def _():
19+
await serverapp.web_app.settings["terminal_manager"].kill_all()
20+
return _
21+
22+
1423
@pytest.fixture
1524
def terminal_path(tmp_path):
1625
subdir = tmp_path.joinpath('terminal_path')
@@ -21,7 +30,7 @@ def terminal_path(tmp_path):
2130
shutil.rmtree(str(subdir), ignore_errors=True)
2231

2332

24-
async def test_terminal_create(fetch):
33+
async def test_terminal_create(fetch, kill_all):
2534
await fetch(
2635
'api', 'terminals',
2736
method='POST',
@@ -37,9 +46,10 @@ async def test_terminal_create(fetch):
3746
data = json.loads(resp_list.body.decode())
3847

3948
assert len(data) == 1
49+
await kill_all()
4050

4151

42-
async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path):
52+
async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path, kill_all):
4353
resp_create = await fetch(
4454
'api', 'terminals',
4555
method='POST',
@@ -59,9 +69,15 @@ async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path):
5969
data = json.loads(resp_get.body.decode())
6070

6171
assert data['name'] == term_name
72+
await kill_all()
6273

6374

64-
async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
75+
async def test_terminal_create_with_cwd(
76+
fetch,
77+
ws_fetch,
78+
terminal_path,
79+
kill_all
80+
):
6581
resp = await fetch(
6682
'api', 'terminals',
6783
method='POST',
@@ -75,21 +91,19 @@ async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
7591
ws = await ws_fetch(
7692
'terminals', 'websocket', term_name
7793
)
94+
await ws.write_message(json.dumps(['stdin', 'pwd\r']))
7895

79-
ws.write_message(json.dumps(['stdin', 'pwd\r\n']))
80-
81-
message_stdout = ''
96+
messages = ""
8297
while True:
8398
try:
84-
message = await asyncio.wait_for(ws.read_message(), timeout=1.0)
99+
response = await asyncio.wait_for(ws.read_message(), timeout=1.0)
85100
except asyncio.TimeoutError:
86-
break
87-
88-
message = json.loads(message)
101+
return messages
89102

90-
if message[0] == 'stdout':
91-
message_stdout += message[1]
103+
response = json.loads(response)
104+
if response[0] == "stdout":
105+
messages += response[1]
92106

93107
ws.close()
94-
95-
assert str(terminal_path) in message_stdout
108+
assert str(terminal_path) in messages
109+
await kill_all()

0 commit comments

Comments
 (0)