Skip to content

Commit

Permalink
add failing test for stopping bash session
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Feb 20, 2025
1 parent 74c942c commit efd0a6e
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions tests/unit/test_bash_session_stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest
import time
from openhands.events.action import CmdRunAction
from openhands.events.observation import CmdOutputObservation
from openhands.runtime.utils.bash import BashSession

def test_bash_session_stop_behavior():
"""Test that stopping a long-running process works correctly."""
session = BashSession(work_dir="/tmp", no_change_timeout_seconds=1)
session.initialize()

# Start a long-running process that will timeout
action = CmdRunAction(command="sleep 10")
action.set_hard_timeout(2) # Set a timeout so test doesn't hang
action.blocking = False # Allow no-change timeout

# Execute the command and wait for timeout
result = session.execute(action)
assert isinstance(result, CmdOutputObservation)
assert "command timed out" in result.metadata.suffix.lower() or "no new output" in result.metadata.suffix.lower()

# Try to send a new command - this should fail since process is still running
new_action = CmdRunAction(command="echo test")
new_result = session.execute(new_action)
assert isinstance(new_result, CmdOutputObservation)
assert "previous command is still running" in new_result.metadata.suffix.lower()
assert "not executed" in new_result.metadata.suffix.lower()

# Try to send an empty command - this should show the running process output
empty_action = CmdRunAction(command="")
empty_result = session.execute(empty_action)
assert isinstance(empty_result, CmdOutputObservation)
assert "output of the previous command" in empty_result.metadata.prefix.lower()

# Clean up
session.close()

def test_bash_session_stop_command():
"""Test that sending C-c to stop a process works correctly."""
session = BashSession(work_dir="/tmp", no_change_timeout_seconds=1)
session.initialize()

# Start a long-running process that will timeout
action = CmdRunAction(command="sleep 10")
action.set_hard_timeout(2) # Set a timeout so test doesn't hang
action.blocking = False # Allow no-change timeout

# Execute the command and wait for timeout
result = session.execute(action)
assert isinstance(result, CmdOutputObservation)
assert "command timed out" in result.metadata.suffix.lower() or "no new output" in result.metadata.suffix.lower()

# Send C-c to stop the process
stop_action = CmdRunAction(command="C-c", is_input="true")
stop_result = session.execute(stop_action)
assert isinstance(stop_result, CmdOutputObservation)
assert stop_result.metadata.exit_code == 130 # 130 is the exit code for SIGINT

# Now we should be able to run a new command
new_action = CmdRunAction(command="echo test")
new_result = session.execute(new_action)
assert isinstance(new_result, CmdOutputObservation)
assert new_result.metadata.exit_code == 0
assert "test" in new_result.content

# Clean up
session.close()

0 comments on commit efd0a6e

Please sign in to comment.