@@ -153,6 +153,109 @@ def test_clear():
153153 assert len (os .listdir (cache1 )) < 2
154154
155155
156+ def test_clear_expired (tmp_path ):
157+ def __ager (cache_fn , fn ):
158+ """
159+ Modify the cache file to virtually add time lag to selected files.
160+
161+ Parameters
162+ ---------
163+ cache_fn: str
164+ cache path
165+ fn: str
166+ file name to be modified
167+ """
168+ import pathlib
169+ import time
170+
171+ if os .path .exists (cache_fn ):
172+ with open (cache_fn , "rb" ) as f :
173+ cached_files = pickle .load (f )
174+ fn_posix = pathlib .Path (fn ).as_posix ()
175+ cached_files [fn_posix ]["time" ] = cached_files [fn_posix ]["time" ] - 691200
176+ assert os .access (cache_fn , os .W_OK ), "Cache is not writable"
177+ with open (cache_fn , "wb" ) as f :
178+ pickle .dump (cached_files , f )
179+ time .sleep (1 )
180+
181+ origin = tmp_path .joinpath ("origin" )
182+ cache1 = tmp_path .joinpath ("cache1" )
183+ cache2 = tmp_path .joinpath ("cache2" )
184+ cache3 = tmp_path .joinpath ("cache3" )
185+
186+ origin .mkdir ()
187+ cache1 .mkdir ()
188+ cache2 .mkdir ()
189+ cache3 .mkdir ()
190+
191+ data = b"test data"
192+ f1 = origin .joinpath ("afile" )
193+ f2 = origin .joinpath ("bfile" )
194+ f3 = origin .joinpath ("cfile" )
195+ f4 = origin .joinpath ("dfile" )
196+
197+ with open (f1 , "wb" ) as f :
198+ f .write (data )
199+ with open (f2 , "wb" ) as f :
200+ f .write (data )
201+ with open (f3 , "wb" ) as f :
202+ f .write (data )
203+ with open (f4 , "wb" ) as f :
204+ f .write (data )
205+
206+ # populates first cache
207+ fs = fsspec .filesystem (
208+ "filecache" , target_protocol = "file" , cache_storage = str (cache1 ), cache_check = 1
209+ )
210+ assert fs .cat (str (f1 )) == data
211+
212+ # populates "last" cache if file not found in first one
213+ fs = fsspec .filesystem (
214+ "filecache" ,
215+ target_protocol = "file" ,
216+ cache_storage = [str (cache1 ), str (cache2 )],
217+ cache_check = 1 ,
218+ )
219+ assert fs .cat (str (f2 )) == data
220+ assert fs .cat (str (f3 )) == data
221+ assert len (os .listdir (cache2 )) == 3
222+
223+ # force the expiration
224+ cache_fn = os .path .join (fs .storage [- 1 ], "cache" )
225+ __ager (cache_fn , f2 )
226+
227+ # remove from cache2 the expired files
228+ fs .clear_expired_cache ()
229+ assert len (os .listdir (cache2 )) == 2
230+
231+ # check complete cleanup
232+ __ager (cache_fn , f3 )
233+
234+ fs .clear_expired_cache ()
235+ assert not fs ._check_file (f2 )
236+ assert not fs ._check_file (f3 )
237+ assert len (os .listdir (cache2 )) < 2
238+
239+ # check cache1 to be untouched after cleaning
240+ assert len (os .listdir (cache1 )) == 2
241+
242+ # check cleaning with 'same_name' option enabled
243+ fs = fsspec .filesystem (
244+ "filecache" ,
245+ target_protocol = "file" ,
246+ cache_storage = [str (cache1 ), str (cache2 ), str (cache3 )],
247+ same_names = True ,
248+ cache_check = 1 ,
249+ )
250+ assert fs .cat (str (f4 )) == data
251+
252+ cache_fn = os .path .join (fs .storage [- 1 ], "cache" )
253+ __ager (cache_fn , f4 )
254+
255+ fs .clear_expired_cache ()
256+ assert not fs ._check_file (str (f4 ))
257+
258+
156259def test_pop ():
157260 import tempfile
158261
0 commit comments