Skip to content

Commit d0b1501

Browse files
committed
test: add unit and integration tests for close_session
1 parent dd25e29 commit d0b1501

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

tests/integration/sdk/sandbox/test_close_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
@pytest.mark.need_admin
13-
# @SKIP_IF_NO_DOCKER
13+
@SKIP_IF_NO_DOCKER
1414
@pytest.mark.asyncio
1515
async def test_sdk_close_session(admin_remote_server):
1616
# Use low resource config to avoid Ray scheduling issues on local machine
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""
2+
Unit test for close_session - tests the core logic without Docker
3+
"""
4+
import pytest
5+
from rock.rocklet.local_sandbox import LocalSandboxRuntime, BashSession
6+
from rock.admin.proto.request import SandboxBashAction as BashAction
7+
from rock.admin.proto.request import SandboxCloseBashSessionRequest as CloseBashSessionRequest
8+
from rock.admin.proto.request import SandboxCreateBashSessionRequest as CreateBashSessionRequest
9+
10+
11+
@pytest.mark.asyncio
12+
async def test_close_session_logic():
13+
"""Test close_session logic directly with LocalSandboxRuntime"""
14+
15+
# Create runtime (no extra parameters allowed)
16+
runtime = LocalSandboxRuntime()
17+
18+
# 1. Create a session
19+
session_name = "test-session"
20+
create_req = CreateBashSessionRequest(session=session_name, session_type="bash")
21+
create_resp = await runtime.create_session(create_req)
22+
assert create_resp.session_type == "bash"
23+
assert session_name in runtime.sessions
24+
print(f"✅ Session '{session_name}' created successfully")
25+
26+
# 2. Run a command in the session
27+
action = BashAction(command="echo 'hello'", session=session_name)
28+
obs = await runtime.run_in_session(action)
29+
assert "hello" in obs.output
30+
print(f"✅ Command executed in session: {obs.output.strip()}")
31+
32+
# 3. Close the session
33+
close_req = CloseBashSessionRequest(session=session_name, session_type="bash")
34+
close_resp = await runtime.close_session(close_req)
35+
assert close_resp.session_type == "bash"
36+
assert session_name not in runtime.sessions
37+
print(f"✅ Session '{session_name}' closed successfully")
38+
39+
# 4. Verify session is closed (should raise exception)
40+
with pytest.raises(Exception) as exc_info:
41+
await runtime.run_in_session(action)
42+
43+
assert "does not exist" in str(exc_info.value)
44+
print(f"✅ Verified: Closed session cannot be used")
45+
46+
print("\n" + "=" * 60)
47+
print("✅ All unit tests passed!")
48+
print("=" * 60)
49+
50+
51+
@pytest.mark.asyncio
52+
async def test_close_nonexistent_session():
53+
"""Test closing a session that doesn't exist raises SessionDoesNotExistError"""
54+
from rock.rocklet.local_sandbox import SessionDoesNotExistError
55+
56+
runtime = LocalSandboxRuntime()
57+
58+
# Try to close a session that was never created
59+
close_req = CloseBashSessionRequest(session="nonexistent-session", session_type="bash")
60+
61+
with pytest.raises(SessionDoesNotExistError) as exc_info:
62+
await runtime.close_session(close_req)
63+
64+
assert "nonexistent-session" in str(exc_info.value)
65+
print("✅ Correctly raised SessionDoesNotExistError for non-existent session")
66+
67+
68+
@pytest.mark.asyncio
69+
async def test_close_session_twice():
70+
"""Test that closing a session twice raises an error"""
71+
from rock.rocklet.local_sandbox import SessionDoesNotExistError
72+
73+
runtime = LocalSandboxRuntime()
74+
75+
# Create a session
76+
session_name = "test-double-close"
77+
create_req = CreateBashSessionRequest(session=session_name, session_type="bash")
78+
await runtime.create_session(create_req)
79+
80+
# Close the session first time - should succeed
81+
close_req = CloseBashSessionRequest(session=session_name, session_type="bash")
82+
await runtime.close_session(close_req)
83+
84+
# Close the session second time - should raise error
85+
with pytest.raises(SessionDoesNotExistError) as exc_info:
86+
await runtime.close_session(close_req)
87+
88+
assert session_name in str(exc_info.value)
89+
print("✅ Correctly raised SessionDoesNotExistError when closing session twice")
90+
91+
92+
@pytest.mark.asyncio
93+
async def test_close_session_with_default_name():
94+
"""Test closing the default session"""
95+
runtime = LocalSandboxRuntime()
96+
97+
# Create a session with default name
98+
create_req = CreateBashSessionRequest(session_type="bash") # uses default session name
99+
await runtime.create_session(create_req)
100+
assert "default" in runtime.sessions
101+
102+
# Close the default session
103+
close_req = CloseBashSessionRequest(session_type="bash") # uses default session name
104+
close_resp = await runtime.close_session(close_req)
105+
106+
assert close_resp.session_type == "bash"
107+
assert "default" not in runtime.sessions
108+
print("✅ Successfully closed default session")
109+
110+
111+
if __name__ == "__main__":
112+
import asyncio
113+
asyncio.run(test_close_session_logic())

0 commit comments

Comments
 (0)