-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(airbyte-cdk) Async jobs - Limit memory usage (#46286)
- Loading branch information
Showing
4 changed files
with
53 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
import csv | ||
import os | ||
from io import BytesIO | ||
from pathlib import Path | ||
from unittest import TestCase | ||
|
||
import pytest | ||
import requests | ||
import requests_mock | ||
from airbyte_cdk.sources.declarative.extractors import ResponseToFileExtractor | ||
|
@@ -52,3 +55,33 @@ def _mock_streamed_response(self, io: BytesIO) -> requests.Response: | |
any_url = "https://anyurl.com" | ||
self._http_mocker.register_uri("GET", any_url, [{"body": io, "status_code": 200}]) | ||
return requests.get(any_url) | ||
|
||
|
||
@pytest.fixture(name="large_events_response") | ||
def large_event_response_fixture(): | ||
lines_in_response = 2_000_000 # ≈ 62 MB of response | ||
dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
file_path = f"{dir_path}/test_response.csv" | ||
with open(file_path, "w") as csvfile: | ||
csv_writer = csv.writer(csvfile) | ||
csv_writer.writerow(["username", "email"]) # headers | ||
for _ in range(lines_in_response): | ||
csv_writer.writerow(["a_username","[email protected]"]) | ||
yield (lines_in_response, file_path) | ||
os.remove(file_path) | ||
|
||
|
||
@pytest.mark.slow | ||
@pytest.mark.limit_memory("20 MB") | ||
def test_response_to_file_extractor_memory_usage(requests_mock, large_events_response): | ||
lines_in_response, file_path = large_events_response | ||
extractor = ResponseToFileExtractor() | ||
|
||
url = "https://for-all-mankind.nasa.com/api/v1/users/users1" | ||
requests_mock.get(url, body=open(file_path, "rb")) | ||
|
||
counter = 0 | ||
for _ in extractor.extract_records(requests.get(url, stream=True)): | ||
counter += 1 | ||
|
||
assert counter == lines_in_response |