-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support passing a custom parser to
HttpServerSourceStage
and `HttpC…
…lientSourceStage` stages (#1705) * Add a new constructor argument to `HttpServerSourceStage` & `HttpClientSourceStage` called `payload_to_df_fn`, allowing users to specify a custom payload parser. * Remove work-around for rapidsai/cudf#5712 this bug is fixed in our current version of cudf. * Relocate updated tests to `tests/stages` Closes #1703 ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/nv-morpheus/Morpheus/blob/main/docs/source/developer_guide/contributing.md). - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. Authors: - David Gardner (https://github.com/dagardner-nv) Approvers: - Michael Demoret (https://github.com/mdemoret-nv) URL: #1705
- Loading branch information
1 parent
ca14433
commit 12abdce
Showing
9 changed files
with
247 additions
and
71 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
HTTP/1.1 200 OK | ||
Content-Type: application/json | ||
|
||
{"not_valid":"json |
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Copyright (c) 2023-2024, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from _utils import TEST_DIRS | ||
from _utils import assert_results | ||
from _utils.dataset_manager import DatasetManager | ||
from morpheus.config import Config | ||
from morpheus.pipeline import LinearPipeline | ||
from morpheus.stages.input.http_client_source_stage import HttpClientSourceStage | ||
from morpheus.stages.output.compare_dataframe_stage import CompareDataFrameStage | ||
|
||
|
||
@pytest.mark.slow | ||
@pytest.mark.use_cudf | ||
@pytest.mark.parametrize("lines", [False, True], ids=["json", "lines"]) | ||
@pytest.mark.parametrize("use_payload_to_df_fn", [False, True], ids=["no_payload_to_df_fn", "payload_to_df_fn"]) | ||
def test_http_client_source_stage_pipe(config: Config, | ||
dataset: DatasetManager, | ||
mock_rest_server: str, | ||
lines: bool, | ||
use_payload_to_df_fn: bool): | ||
""" | ||
Test the HttpClientSourceStage against a mock REST server which will return JSON data which can be deserialized | ||
into a DataFrame. | ||
""" | ||
source_df = dataset['filter_probs.csv'] | ||
|
||
if lines: | ||
endpoint = "data-lines" | ||
else: | ||
endpoint = "data" | ||
|
||
if use_payload_to_df_fn: | ||
if lines: | ||
payload_file = os.path.join(TEST_DIRS.tests_data_dir, "filter_probs.jsonlines") | ||
else: | ||
payload_file = os.path.join(TEST_DIRS.tests_data_dir, "filter_probs.json") | ||
|
||
with open(payload_file, "rb") as f: | ||
expected_payload = f.read() | ||
|
||
def payload_to_df_fn(payload, lines_arg): | ||
assert payload == expected_payload | ||
assert lines_arg == lines | ||
return source_df[['v2', 'v3']].copy(deep=True) | ||
|
||
expected_df = source_df[['v2', 'v3']].copy(deep=True) | ||
|
||
else: | ||
payload_to_df_fn = None | ||
expected_payload = None | ||
expected_df = source_df.copy(deep=True) | ||
|
||
url = f"{mock_rest_server}/api/v1/{endpoint}" | ||
|
||
num_records = len(expected_df) | ||
|
||
pipe = LinearPipeline(config) | ||
pipe.set_source( | ||
HttpClientSourceStage(config=config, | ||
url=url, | ||
max_retries=1, | ||
lines=lines, | ||
stop_after=num_records, | ||
payload_to_df_fn=payload_to_df_fn)) | ||
comp_stage = pipe.add_stage(CompareDataFrameStage(config, expected_df)) | ||
pipe.run() | ||
|
||
assert_results(comp_stage.get_results()) | ||
|
||
|
||
@pytest.mark.slow | ||
@pytest.mark.use_cudf | ||
@pytest.mark.parametrize( | ||
"lines", | ||
[False, pytest.param(True, marks=pytest.mark.skip(reason="https://github.com/rapidsai/cudf/issues/15820"))], | ||
ids=["json", "lines"]) | ||
@pytest.mark.parametrize("use_payload_to_df_fn", [False, True], ids=["no_payload_to_df_fn", "payload_to_df_fn"]) | ||
def test_parse_errors(config: Config, mock_rest_server: str, lines: bool, use_payload_to_df_fn: bool): | ||
url = f"{mock_rest_server}/api/v1/invalid" | ||
|
||
if use_payload_to_df_fn: | ||
payload_to_df_fn = mock.MagicMock(side_effect=ValueError("Invalid payload")) | ||
else: | ||
payload_to_df_fn = None | ||
|
||
pipe = LinearPipeline(config) | ||
pipe.set_source( | ||
HttpClientSourceStage(config=config, url=url, max_retries=1, lines=lines, payload_to_df_fn=payload_to_df_fn)) | ||
|
||
# cudf raises a RuntimeError when it should be raising a ValueError also a part of #15820 | ||
with pytest.raises(Exception): | ||
pipe.run() | ||
|
||
if use_payload_to_df_fn: | ||
payload_to_df_fn.assert_called_once() |
Oops, something went wrong.