1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import time
16-
1715from aws_advanced_python_wrapper .utils .sliding_expiration_cache import \
18- SlidingExpirationCache
16+ SlidingExpirationCache , SlidingExpirationCacheWithCleanupThread
17+ from time import sleep
1918
2019
2120def test_compute_if_absent ():
@@ -30,7 +29,7 @@ def test_compute_if_absent():
3029 assert "a" == result2
3130 assert "a" == cache .get (1 )
3231
33- time . sleep (0.07 ) # Python may sleep slightly less than the given value
32+ sleep (0.07 ) # Python may sleep slightly less than the given value
3433 result3 = cache .compute_if_absent (1 , lambda _ : "b" , 5 )
3534 assert "b" == result3
3635 assert "b" == cache .get (1 )
@@ -54,7 +53,7 @@ def test_remove():
5453 result = cache .compute_if_absent ("non_expired_item" , lambda _ : non_expired_item , 15_000_000_000 )
5554 assert non_expired_item == result
5655
57- time . sleep (0.07 ) # Python may sleep slightly less than the given value
56+ sleep (0.07 ) # Python may sleep slightly less than the given value
5857 cache .remove ("item_to_remove" )
5958
6059 assert cache .get ("item_to_remove" ) is None
@@ -89,6 +88,34 @@ def test_clear():
8988 assert item2 .disposed is True
9089
9190
91+ def test_cleanup_thread_continuous_removal ():
92+ # Use very short cleanup interval for testing (100ms)
93+ cache = SlidingExpirationCacheWithCleanupThread (
94+ cleanup_interval_ns = 100_000_000 , # 100ms
95+ item_disposal_func = lambda item : item .dispose ()
96+ )
97+
98+ # First cycle: insert item that expires quickly
99+ item1 = DisposableItem (True )
100+ cache .compute_if_absent ("key1" , lambda _ : item1 , 50_000_000 ) # 50ms expiration
101+ assert cache .get ("key1" ) == item1
102+
103+ # Wait for cleanup thread to remove expired item
104+ sleep (0.2 ) # Wait 200ms for cleanup
105+ assert cache .get ("key1" ) is None
106+ assert item1 .disposed is True
107+
108+ # Second cycle: insert another item that expires quickly
109+ item2 = DisposableItem (True )
110+ cache .compute_if_absent ("key2" , lambda _ : item2 , 50_000_000 ) # 50ms expiration
111+ assert cache .get ("key2" ) == item2
112+
113+ # Wait for cleanup thread to remove second expired item
114+ sleep (0.2 ) # Wait 200ms for cleanup
115+ assert cache .get ("key2" ) is None
116+ assert item2 .disposed is True
117+
118+
92119class DisposableItem :
93120 def __init__ (self , should_dispose ):
94121 self .should_dispose = should_dispose
0 commit comments