-
Notifications
You must be signed in to change notification settings - Fork 6k
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
[serve] Add tests for HTTP status code is_error
logic
#48822
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
777b6ef
skip
edoakes 4b404ba
WIP
edoakes 9f42aec
skip
edoakes dc1262f
Merge branch 'master' of https://github.com/ray-project/ray into eoak…
edoakes 657e170
fix some tests
edoakes 82b4183
fix some tests
edoakes ae8a67b
fix
edoakes aff8fe9
fix
edoakes 613e474
fix
edoakes ad36347
fix
edoakes 2966be8
fix
edoakes dacde33
fix
edoakes 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 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 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 |
---|---|---|
|
@@ -6,7 +6,11 @@ | |
import grpc | ||
import pytest | ||
import requests | ||
from fastapi import FastAPI | ||
from fastapi import FastAPI, WebSocket | ||
from starlette.requests import Request | ||
from starlette.responses import PlainTextResponse | ||
from websockets.exceptions import ConnectionClosed | ||
from websockets.sync.client import connect | ||
|
||
import ray | ||
import ray.util.state as state_api | ||
|
@@ -583,6 +587,161 @@ def f(*args): | |
print("serve_grpc_request_latency_ms_sum working as expected.") | ||
|
||
|
||
def test_proxy_metrics_http_status_code_is_error(serve_start_shutdown): | ||
"""Verify that 2xx status codes aren't errors, others are.""" | ||
|
||
def check_request_count_metrics( | ||
expected_error_count: int, | ||
expected_success_count: int, | ||
): | ||
resp = requests.get("http://127.0.0.1:9999").text | ||
error_count = 0 | ||
success_count = 0 | ||
for line in resp.split("\n"): | ||
if line.startswith("ray_serve_num_http_error_requests_total"): | ||
error_count += int(float(line.split(" ")[-1])) | ||
if line.startswith("ray_serve_num_http_requests_total"): | ||
success_count += int(float(line.split(" ")[-1])) | ||
|
||
assert error_count == expected_error_count | ||
assert success_count == expected_success_count | ||
return True | ||
|
||
@serve.deployment | ||
async def return_status_code(request: Request): | ||
code = int((await request.body()).decode("utf-8")) | ||
return PlainTextResponse("", status_code=code) | ||
|
||
serve.run(return_status_code.bind()) | ||
|
||
# 200 is not an error. | ||
r = requests.get("http://127.0.0.1:8000/", data=b"200") | ||
assert r.status_code == 200 | ||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=0, | ||
expected_success_count=1, | ||
) | ||
|
||
# 2xx is not an error. | ||
r = requests.get("http://127.0.0.1:8000/", data=b"250") | ||
assert r.status_code == 250 | ||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=0, | ||
expected_success_count=2, | ||
) | ||
|
||
# 3xx is an error. | ||
r = requests.get("http://127.0.0.1:8000/", data=b"300") | ||
assert r.status_code == 300 | ||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=1, | ||
expected_success_count=3, | ||
) | ||
|
||
# 4xx is an error. | ||
r = requests.get("http://127.0.0.1:8000/", data=b"400") | ||
assert r.status_code == 400 | ||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=2, | ||
expected_success_count=4, | ||
) | ||
|
||
# 5xx is an error. | ||
r = requests.get("http://127.0.0.1:8000/", data=b"500") | ||
assert r.status_code == 500 | ||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=3, | ||
expected_success_count=5, | ||
) | ||
|
||
|
||
def test_proxy_metrics_websocket_status_code_is_error(serve_start_shutdown): | ||
"""Verify that status codes aisde from 1000 or 1001 are errors.""" | ||
|
||
def check_request_count_metrics( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocker nit: Can probably refactor this and share with the other test |
||
expected_error_count: int, | ||
expected_success_count: int, | ||
): | ||
resp = requests.get("http://127.0.0.1:9999").text | ||
error_count = 0 | ||
success_count = 0 | ||
for line in resp.split("\n"): | ||
if line.startswith("ray_serve_num_http_error_requests_total"): | ||
error_count += int(float(line.split(" ")[-1])) | ||
if line.startswith("ray_serve_num_http_requests_total"): | ||
success_count += int(float(line.split(" ")[-1])) | ||
|
||
assert error_count == expected_error_count | ||
assert success_count == expected_success_count | ||
return True | ||
|
||
fastapi_app = FastAPI() | ||
|
||
@serve.deployment | ||
@serve.ingress(fastapi_app) | ||
class WebSocketServer: | ||
@fastapi_app.websocket("/") | ||
async def accept_then_close(self, ws: WebSocket): | ||
await ws.accept() | ||
code = int(await ws.receive_text()) | ||
await ws.close(code=code) | ||
|
||
serve.run(WebSocketServer.bind()) | ||
|
||
# Regular disconnect (1000) is not an error. | ||
with connect("ws://localhost:8000/") as ws: | ||
with pytest.raises(ConnectionClosed): | ||
ws.send("1000") | ||
ws.recv() | ||
|
||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=0, | ||
expected_success_count=1, | ||
) | ||
|
||
# Goaway disconnect (1001) is not an error. | ||
with connect("ws://localhost:8000/") as ws: | ||
with pytest.raises(ConnectionClosed): | ||
ws.send("1001") | ||
ws.recv() | ||
|
||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=0, | ||
expected_success_count=2, | ||
) | ||
|
||
# Other codes are errors. | ||
with connect("ws://localhost:8000/") as ws: | ||
with pytest.raises(ConnectionClosed): | ||
ws.send("1011") | ||
ws.recv() | ||
|
||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=1, | ||
expected_success_count=3, | ||
) | ||
|
||
# Other codes are errors. | ||
with connect("ws://localhost:8000/") as ws: | ||
with pytest.raises(ConnectionClosed): | ||
ws.send("3000") | ||
ws.recv() | ||
|
||
wait_for_condition( | ||
check_request_count_metrics, | ||
expected_error_count=2, | ||
expected_success_count=4, | ||
) | ||
|
||
|
||
def test_replica_metrics_fields(serve_start_shutdown): | ||
"""Test replica metrics fields""" | ||
|
||
|
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.
Non-blocker nit: can refactor this into a small helper 🙃