-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feature: Add timeout to wait_for_endpoint and _wait_until. #2684
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,9 @@ | |
from __future__ import absolute_import | ||
|
||
import pytest | ||
from mock import Mock, MagicMock | ||
from mock import Mock, MagicMock, DEFAULT | ||
import sagemaker | ||
import time | ||
|
||
EXPANDED_ROLE = "arn:aws:iam::111111111111:role/ExpandedRole" | ||
REGION = "us-west-2" | ||
|
@@ -23,29 +24,39 @@ | |
ENDPOINT_NAME = "the_point_of_end" | ||
|
||
|
||
def get_sagemaker_session(returns_status): | ||
def get_sagemaker_session_mock_endpoint_status(returns_status, block_seconds=None): | ||
boto_mock = MagicMock(name="boto_session", region_name=REGION) | ||
client_mock = MagicMock() | ||
client_mock.describe_model_package = MagicMock( | ||
return_value={"ModelPackageStatus": returns_status} | ||
) | ||
client_mock.describe_endpoint = MagicMock(return_value={"EndpointStatus": returns_status}) | ||
side_effect = None | ||
|
||
def side_effect_fn(*args, **kwargs): | ||
time.sleep(block_seconds) | ||
return DEFAULT | ||
|
||
if block_seconds: | ||
side_effect = side_effect_fn | ||
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. nit: not needed, can add it on line 42. 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. what do you mean? Can you propose alternative code instead? |
||
client_mock.describe_endpoint = MagicMock( | ||
return_value={"EndpointStatus": returns_status}, side_effect=side_effect | ||
) | ||
ims = sagemaker.Session(boto_session=boto_mock, sagemaker_client=client_mock) | ||
ims.expand_role = Mock(return_value=EXPANDED_ROLE) | ||
return ims | ||
|
||
|
||
def test_does_not_raise_when_successfully_created_package(): | ||
try: | ||
sagemaker_session = get_sagemaker_session(returns_status="Completed") | ||
sagemaker_session = get_sagemaker_session_mock_endpoint_status(returns_status="Completed") | ||
sagemaker_session.wait_for_model_package(MODEL_PACKAGE_NAME) | ||
except sagemaker.exceptions.UnexpectedStatusException: | ||
pytest.fail("UnexpectedStatusException was thrown while it should not") | ||
|
||
|
||
def test_raise_when_failed_created_package(): | ||
try: | ||
sagemaker_session = get_sagemaker_session(returns_status="EnRoute") | ||
sagemaker_session = get_sagemaker_session_mock_endpoint_status(returns_status="EnRoute") | ||
sagemaker_session.wait_for_model_package(MODEL_PACKAGE_NAME) | ||
assert ( | ||
False | ||
|
@@ -59,7 +70,7 @@ def test_raise_when_failed_created_package(): | |
def test_does_not_raise_when_correct_job_status(): | ||
try: | ||
job = Mock() | ||
sagemaker_session = get_sagemaker_session(returns_status="Stopped") | ||
sagemaker_session = get_sagemaker_session_mock_endpoint_status(returns_status="Stopped") | ||
sagemaker_session._check_job_status( | ||
job, {"TransformationJobStatus": "Stopped"}, "TransformationJobStatus" | ||
) | ||
|
@@ -70,7 +81,7 @@ def test_does_not_raise_when_correct_job_status(): | |
def test_does_raise_when_incorrect_job_status(): | ||
try: | ||
job = Mock() | ||
sagemaker_session = get_sagemaker_session(returns_status="Failed") | ||
sagemaker_session = get_sagemaker_session_mock_endpoint_status(returns_status="Failed") | ||
sagemaker_session._check_job_status( | ||
job, {"TransformationJobStatus": "Failed"}, "TransformationJobStatus" | ||
) | ||
|
@@ -106,15 +117,15 @@ def test_does_raise_capacity_error_when_incorrect_job_status(): | |
|
||
def test_does_not_raise_when_successfully_deployed_endpoint(): | ||
try: | ||
sagemaker_session = get_sagemaker_session(returns_status="InService") | ||
sagemaker_session = get_sagemaker_session_mock_endpoint_status(returns_status="InService") | ||
sagemaker_session.wait_for_endpoint(ENDPOINT_NAME) | ||
except sagemaker.exceptions.UnexpectedStatusException: | ||
pytest.fail("UnexpectedStatusException was thrown while it should not") | ||
|
||
|
||
def test_raise_when_failed_to_deploy_endpoint(): | ||
try: | ||
sagemaker_session = get_sagemaker_session(returns_status="Failed") | ||
sagemaker_session = get_sagemaker_session_mock_endpoint_status(returns_status="Failed") | ||
assert sagemaker_session.wait_for_endpoint(ENDPOINT_NAME) | ||
assert ( | ||
False | ||
|
@@ -123,3 +134,15 @@ def test_raise_when_failed_to_deploy_endpoint(): | |
assert type(e) == sagemaker.exceptions.UnexpectedStatusException | ||
assert e.actual_status == "Failed" | ||
assert "InService" in e.allowed_statuses | ||
|
||
|
||
def test_wait_for_endpoint_timeout(): | ||
timeout_seconds = 2 | ||
block_seconds = timeout_seconds + 3 | ||
sagemaker_session = get_sagemaker_session_mock_endpoint_status( | ||
returns_status="InService", block_seconds=block_seconds | ||
) | ||
start_time = time.time() | ||
sagemaker_session.wait_for_endpoint(ENDPOINT_NAME, 0.1, timeout_seconds) | ||
elapsed_time = time.time() - start_time | ||
assert elapsed_time >= timeout_seconds |
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.
can we also take this opportunity to rename deploy_done to deploy_status or something similar to keep it in same lines of naming as the other status check methods? Thanks!
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.
Good suggesiton, but I think we should do that in another CR to keep this in scope. Also, why there's so many print functions instead of logging? printing from a library isn't generally a good practice.