From 9dde469de9ce54d1603b6dae7d4770aff6ccb4fd Mon Sep 17 00:00:00 2001 From: benoit74 Date: Tue, 3 Mar 2026 14:18:22 +0000 Subject: [PATCH] WIP: Apply backpressure to add_item operations --- tests/zim/test_zim_creator.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/zim/test_zim_creator.py b/tests/zim/test_zim_creator.py index ad2bf7b..51c3442 100644 --- a/tests/zim/test_zim_creator.py +++ b/tests/zim/test_zim_creator.py @@ -973,3 +973,33 @@ def test_config_indexing(tmp_path: pathlib.Path): assert Creator(tmp_path / "_.zim", "").config_indexing(True, "bam") assert Creator(tmp_path / "_.zim", "").config_indexing(False, "bam") assert Creator(tmp_path / "_.zim", "").config_indexing(False) + + +def test_item_backpressure(tmp_path: pathlib.Path, html_file: pathlib.Path): + fpath = tmp_path / "test.zim" + + class Store: + inflight_count = 0 + + def cb(): + Store.inflight_count -= 1 + + with Creator(fpath, "").config_dev_metadata() as creator: + max_inflight = 0 + for i in range(200): + while Store.inflight_count > 100: + time.sleep(0.01) + # adding GC collection to trigger finalization does not help + # gc.collect() + creator.add_item( + StaticItem( + path=f"{i}_{html_file.name}", + filepath=html_file, + mimetype="text/html", + ), + callbacks=Callback(func=cb), + ) + Store.inflight_count += 1 + max_inflight = max(Store.inflight_count, max_inflight) + + assert max_inflight < 1000