77from tempfile import TemporaryDirectory
88from http import HTTPStatus
99from html2notion .translate .batch_import import BatchImport
10+ from html2notion .utils import rate_limit
1011from html2notion .utils .log import log_only_local
1112
1213process_once_time = 0.5
@@ -41,6 +42,12 @@ def json(self):
4142 return MockResponse (HTTPStatus .OK , content , elapsed_time )
4243
4344
45+ async def mock_notion_create_page (notion_data , * args , ** kwargs ):
46+ async with rate_limit :
47+ await asyncio .sleep (0.01 )
48+ log_only_local (f"mock_notion_create_page" )
49+ return "succ"
50+
4451@pytest .fixture (params = [10 , 20 ])
4552def temp_dir_fixture (request ):
4653 num_files = request .param
@@ -78,3 +85,20 @@ async def test_batch_process(temp_dir_fixture, concurrent_limit):
7885 f"total_time: { total_time } , sync_time: { sync_time } , least_time: { least_time } " )
7986 assert total_time >= least_time
8087 assert total_time <= sync_time
88+
89+
90+ @pytest .mark .parametrize ("concurrent_limit" , [5 , 10 , 20 ])
91+ @pytest .mark .asyncio
92+ async def test_reqlimit (temp_dir_fixture , concurrent_limit ):
93+ dir_path = temp_dir_fixture
94+ start_time = time .perf_counter ()
95+ with patch ("html2notion.translate.notion_import.NotionImporter.create_new_page" , side_effect = mock_notion_create_page ):
96+ batch_processor = BatchImport (dir_path , concurrent_limit = concurrent_limit )
97+ responses = await batch_processor .process_directory ()
98+
99+ end_time = time .perf_counter ()
100+ total_time = end_time - start_time
101+ num_files = len (list (dir_path .glob ('*.html' )))
102+ log_only_local (f"file nums: { num_files } , concurrent { concurrent_limit } , total_time: { total_time } " )
103+ # The time deviation within 1 second is acceptable here.
104+ assert (total_time >= num_files / 3 - 1 )
0 commit comments