@@ -94,3 +94,83 @@ def test_deprecated_shard_seed(synthetic_dataset, reader_factory):
9494 match_str = 'shard_seed was deprecated and will be removed in future versions.'
9595 with pytest .warns (UserWarning , match = match_str ):
9696 reader_factory (synthetic_dataset .url , shard_seed = 123 )
97+
98+
99+ def test_cleanup_cache_with_local_disk_cache (synthetic_dataset , tmpdir ):
100+ """Test that cleanup_cache properly removes local disk cache directory"""
101+ import os
102+ cache_location = tmpdir .strpath
103+
104+ with make_reader (synthetic_dataset .url ,
105+ cache_type = 'local-disk' ,
106+ cache_location = cache_location ,
107+ cache_size_limit = 1000000 ,
108+ cache_row_size_estimate = 100 ) as reader :
109+ # Read some data to populate cache
110+ next (reader )
111+ # Cache directory should exist
112+ assert os .path .exists (cache_location )
113+
114+ # After context manager exit, cache should be cleaned up
115+ assert not os .path .exists (cache_location )
116+
117+
118+ def test_cleanup_cache_with_null_cache (synthetic_dataset , capsys ):
119+ """Test that cleanup_cache works properly with null cache"""
120+ with make_reader (synthetic_dataset .url , cache_type = 'null' ) as reader :
121+ next (reader )
122+ # Manually call cleanup_cache to test it
123+ reader .cleanup_cache ()
124+
125+ # Check that cleanup message was printed
126+ captured = capsys .readouterr ()
127+ assert "Cache cleanup complete." in captured .out
128+
129+
130+ def test_cleanup_cache_manual_call (synthetic_dataset , tmpdir ):
131+ """Test manually calling cleanup_cache method"""
132+ import os
133+ cache_location = tmpdir .strpath
134+
135+ reader = make_reader (synthetic_dataset .url ,
136+ cache_type = 'local-disk' ,
137+ cache_location = cache_location ,
138+ cache_size_limit = 1000000 ,
139+ cache_row_size_estimate = 100 )
140+
141+ try :
142+ next (reader )
143+ assert os .path .exists (cache_location )
144+
145+ # Manually call cleanup_cache
146+ reader .cleanup_cache ()
147+ assert not os .path .exists (cache_location )
148+ finally :
149+ reader .stop ()
150+ reader .join ()
151+
152+
153+ def test_cleanup_cache_exception_handling (synthetic_dataset , tmpdir , capsys , monkeypatch ):
154+ """Test that cleanup_cache handles exceptions gracefully"""
155+ cache_location = tmpdir .strpath
156+
157+ with make_reader (synthetic_dataset .url ,
158+ cache_type = 'local-disk' ,
159+ cache_location = cache_location ,
160+ cache_size_limit = 1000000 ,
161+ cache_row_size_estimate = 100 ) as reader :
162+ next (reader )
163+
164+ # Mock the cleanup method to raise an exception
165+ def mock_cleanup ():
166+ raise OSError ("Simulated cleanup error" )
167+
168+ monkeypatch .setattr (reader .cache , 'cleanup' , mock_cleanup )
169+
170+ # Call cleanup_cache - should handle exception gracefully
171+ reader .cleanup_cache ()
172+
173+ # Check that error message was printed
174+ captured = capsys .readouterr ()
175+ assert "Error cleaning cache: Simulated cleanup error" in captured .out
176+ assert "Cache cleanup complete." in captured .out
0 commit comments