@@ -199,6 +199,44 @@ def test_should_stale_item_be_fetched_synchronously_reached(self):
199199 def test_should_stale_item_be_fetched_synchronously_not_reached (self ):
200200 assert StaleDummyJob ().should_stale_item_be_fetched_synchronously (300 ) is False
201201
202+ def test_should_refresh_setting_disabled (self , settings ):
203+ settings .CACHEBACK_VALIDATE_JOB_REFRESH_NEEDED = False
204+ assert DummyJob ().should_refresh ('foo' ) is True
205+
206+ def test_should_refresh_cache_empty (self , settings ):
207+ settings .CACHEBACK_VALIDATE_JOB_REFRESH_NEEDED = True
208+ assert DummyJob ().should_refresh ('foo' ) is True
209+
210+ def test_should_refresh_cache_fresh_with_margin (self , settings ):
211+ settings .CACHEBACK_VALIDATE_JOB_REFRESH_NEEDED = True
212+ with freeze_time ('2016-03-20 14:00' ):
213+ job = DummyJob ()
214+ job .refresh ('foo' )
215+
216+ # lifetime=600, refresh_timeout=60 -> skip while delta > 0
217+ # at +539s remaining time is 61s, which is > refresh_timeout=60s
218+ with freeze_time ('2016-03-20 14:08:59' ):
219+ assert job .should_refresh ('foo' ) is False
220+
221+ def test_should_refresh_cache_within_refresh_timeout (self , settings ):
222+ settings .CACHEBACK_VALIDATE_JOB_REFRESH_NEEDED = True
223+ with freeze_time ('2016-03-20 14:00' ):
224+ job = DummyJob ()
225+ job .refresh ('foo' )
226+
227+ # at +540s the remaining time equals refresh_timeout -> must refresh
228+ with freeze_time ('2016-03-20 14:09:00' ):
229+ assert job .should_refresh ('foo' ) is True
230+
231+ def test_should_refresh_cache_expired (self , settings ):
232+ settings .CACHEBACK_VALIDATE_JOB_REFRESH_NEEDED = True
233+ with freeze_time ('2016-03-20 14:00' ):
234+ job = DummyJob ()
235+ job .refresh ('foo' )
236+
237+ with freeze_time ('2016-03-20 14:11' ):
238+ assert job .should_refresh ('foo' ) is True
239+
202240 def test_key_no_args_no_kwargs (self ):
203241 assert DummyJob ().key () == 'tests.test_base_job.DummyJob'
204242
@@ -291,3 +329,27 @@ def test_job_refresh_perform_error(self, logger_mock):
291329 def test_job_refresh (self ):
292330 Job .perform_async_refresh ('tests.test_base_job.EmptyDummyJob' , (), {}, ('foo' ,), {})
293331 assert EmptyDummyJob ().get ('foo' ) is not None
332+
333+ @pytest .mark .redis_required
334+ @mock .patch ('tests.test_base_job.EmptyDummyJob.fetch' , return_value = 'bar' )
335+ @pytest .mark .parametrize (
336+ 'validate_refresh_enabled, refresh_count' ,
337+ [
338+ (True , 1 ),
339+ (False , 2 ),
340+ ],
341+ )
342+ def test_validate_job_refresh_needed (
343+ self , fetch_mock , rq_burst , settings , validate_refresh_enabled , refresh_count
344+ ):
345+ settings .CACHEBACK_VALIDATE_JOB_REFRESH_NEEDED = validate_refresh_enabled
346+
347+ job = EmptyDummyJob ()
348+ job .task_options = {'is_async' : False }
349+
350+ job .async_refresh ('foo' )
351+ job .async_refresh ('foo' )
352+
353+ rq_burst ()
354+
355+ assert fetch_mock .call_count == refresh_count
0 commit comments