-
Notifications
You must be signed in to change notification settings - Fork 109
Fix broken idle detection and websocket disconnect handling #1634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
simonrosenberg
wants to merge
1
commit into
main
from
openhands/fix-idle-detection-and-websocket-shutdown
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """Tests for the agent server middleware functionality.""" | ||
|
|
||
| import time | ||
|
|
||
| from fastapi import FastAPI | ||
| from fastapi.testclient import TestClient | ||
|
|
||
| from openhands.agent_server.middleware import ActivityTrackingMiddleware | ||
| from openhands.agent_server.server_details_router import ( | ||
| _last_event_time, | ||
| update_last_execution_time, | ||
| ) | ||
|
|
||
|
|
||
| def test_activity_tracking_middleware_updates_last_activity_time(): | ||
| """Test that ActivityTrackingMiddleware updates last activity time on requests.""" | ||
| # Create a simple FastAPI app with the middleware | ||
| app = FastAPI() | ||
| app.add_middleware(ActivityTrackingMiddleware) | ||
|
|
||
| @app.get("/test") | ||
| async def test_endpoint(): | ||
| return {"status": "ok"} | ||
|
|
||
| client = TestClient(app) | ||
|
|
||
| # Record the initial last event time | ||
| initial_time = _last_event_time | ||
|
|
||
| # Wait a small amount to ensure time difference | ||
| time.sleep(0.01) | ||
|
|
||
| # Make a request | ||
| response = client.get("/test") | ||
| assert response.status_code == 200 | ||
|
|
||
| # Import the module-level variable again to get the updated value | ||
| from openhands.agent_server import server_details_router | ||
|
|
||
| # The last event time should have been updated | ||
| assert server_details_router._last_event_time > initial_time | ||
|
|
||
|
|
||
| def test_activity_tracking_middleware_updates_on_every_request(): | ||
| """Test that ActivityTrackingMiddleware updates on every HTTP request.""" | ||
| # Create a simple FastAPI app with the middleware | ||
| app = FastAPI() | ||
| app.add_middleware(ActivityTrackingMiddleware) | ||
|
|
||
| @app.get("/test1") | ||
| async def test_endpoint1(): | ||
| return {"status": "ok"} | ||
|
|
||
| @app.get("/test2") | ||
| async def test_endpoint2(): | ||
| return {"status": "ok"} | ||
|
|
||
| client = TestClient(app) | ||
|
|
||
| # Make first request | ||
| response1 = client.get("/test1") | ||
| assert response1.status_code == 200 | ||
|
|
||
| from openhands.agent_server import server_details_router | ||
|
|
||
| time_after_first = server_details_router._last_event_time | ||
|
|
||
| # Wait a small amount | ||
| time.sleep(0.01) | ||
|
|
||
| # Make second request | ||
| response2 = client.get("/test2") | ||
| assert response2.status_code == 200 | ||
|
|
||
| # The last event time should have been updated again | ||
| assert server_details_router._last_event_time > time_after_first | ||
|
|
||
|
|
||
| def test_activity_tracking_middleware_updates_on_error_responses(): | ||
| """Test that ActivityTrackingMiddleware updates even when endpoint returns error.""" | ||
| # Create a simple FastAPI app with the middleware | ||
| app = FastAPI() | ||
| app.add_middleware(ActivityTrackingMiddleware) | ||
|
|
||
| @app.get("/error") | ||
| async def error_endpoint(): | ||
| from fastapi import HTTPException | ||
|
|
||
| raise HTTPException(status_code=500, detail="Test error") | ||
|
|
||
| client = TestClient(app, raise_server_exceptions=False) | ||
|
|
||
| from openhands.agent_server import server_details_router | ||
|
|
||
| initial_time = server_details_router._last_event_time | ||
|
|
||
| # Wait a small amount | ||
| time.sleep(0.01) | ||
|
|
||
| # Make a request that will return an error | ||
| response = client.get("/error") | ||
| assert response.status_code == 500 | ||
|
|
||
| # The last event time should still have been updated | ||
| assert server_details_router._last_event_time > initial_time | ||
|
|
||
|
|
||
| def test_update_last_execution_time_function(): | ||
| """Test that update_last_execution_time function works correctly.""" | ||
| from openhands.agent_server import server_details_router | ||
|
|
||
| initial_time = server_details_router._last_event_time | ||
|
|
||
| # Wait a small amount | ||
| time.sleep(0.01) | ||
|
|
||
| # Call the function | ||
| update_last_execution_time() | ||
|
|
||
| # The time should have been updated | ||
| assert server_details_router._last_event_time > initial_time |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure about not raising on
RuntimeError? 🤔Tiny nit: we could refactor these as
except (exceptions) as e:like theWebSocketDisconnectcode above, rather than isinstanceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closed the PR and make 2 smaller PRs
#1636
#1635