1111import pytest
1212from _utils import skip_if_no_dev_features
1313from conftest import _KB
14+ from memory_profiler import memory_usage
15+ from typing import Final , List
1416
1517
1618def _hash_file (file : Path ) -> str :
@@ -28,19 +30,42 @@ def _hash_file(file: Path) -> str:
2830@skip_if_no_dev_features
2931def test_upload_file (tmp_file : Path , api_client : osparc .ApiClient ) -> None :
3032 """Test that we can upload a file via the multipart upload"""
33+ _allowed_ram_usage_in_mb : Final [int ] = 300 # 300MB
34+ assert (
35+ tmp_file .stat ().st_size > _allowed_ram_usage_in_mb * 1024 * 1024
36+ ), "For this test to make sense, file size must be larger than allowed ram usage."
37+
38+ def max_diff (data : List [int ]) -> int :
39+ return max (data ) - min (data )
40+
3141 tmp_path : Path = tmp_file .parent
3242 files_api : osparc .FilesApi = osparc .FilesApi (api_client = api_client )
33- uploaded_file1 : osparc .File = files_api .upload_file (tmp_file )
34- uploaded_file2 : osparc .File = files_api .upload_file (tmp_file )
35- assert (
36- uploaded_file1 .id == uploaded_file2 .id
37- ), "could not detect that file was already on server"
38- downloaded_file = files_api .download_file (
39- uploaded_file1 .id , destination_folder = tmp_path
40- )
41- assert Path (downloaded_file ).parent == tmp_path
42- assert _hash_file (Path (downloaded_file )) == _hash_file (tmp_file )
43- files_api .delete_file (uploaded_file1 .id )
43+ try :
44+ upload_ram_usage_in_mb , uploaded_file1 = memory_usage (
45+ (files_api .upload_file , (tmp_file ,)), retval = True
46+ )
47+ assert (
48+ max_diff (upload_ram_usage_in_mb ) < _allowed_ram_usage_in_mb
49+ ), f"Used more than { _allowed_ram_usage_in_mb = } to upload file of size { tmp_file .stat ().st_size = } "
50+ uploaded_file2 : osparc .File = files_api .upload_file (tmp_file )
51+ assert (
52+ uploaded_file1 .id == uploaded_file2 .id
53+ ), "could not detect that file was already on server"
54+ download_ram_usage_in_mb , downloaded_file = memory_usage (
55+ (
56+ files_api .download_file ,
57+ (uploaded_file1 .id ,),
58+ {"destination_folder" : tmp_path },
59+ ),
60+ retval = True ,
61+ )
62+ assert (
63+ max_diff (download_ram_usage_in_mb ) < _allowed_ram_usage_in_mb
64+ ), f"Used more than { _allowed_ram_usage_in_mb = } to down file of size { Path (downloaded_file ).stat ().st_size = } "
65+ assert Path (downloaded_file ).parent == tmp_path
66+ assert _hash_file (Path (downloaded_file )) == _hash_file (tmp_file )
67+ finally :
68+ files_api .delete_file (uploaded_file1 .id )
4469
4570
4671@skip_if_no_dev_features
0 commit comments