@@ -190,6 +190,7 @@ def test_rate_reached_generic_issue(self):
190
190
191
191
192
192
@apply_feature_flag_on_cls ("organizations:groupsnooze-cached-counts" )
193
+ @apply_feature_flag_on_cls ("organizations:groupsnooze-cached-rates" )
193
194
class GroupSnoozeWCacheTest (GroupSnoozeTest ):
194
195
"""
195
196
Test the cached version of the snooze.
@@ -367,3 +368,82 @@ def test_test_user_count_w_cache_expired(self):
367
368
assert not snooze .is_valid (test_rates = True )
368
369
assert mocked_count_users_seen .call_count == 3
369
370
assert cache_spy .set .called_with (cache_key , 100 , 300 )
371
+
372
+ def test_test_frequency_rates_w_cache (self ):
373
+ snooze = GroupSnooze .objects .create (group = self .group , count = 100 , window = 60 )
374
+
375
+ cache_key = f"groupsnooze:v1:{ snooze .id } :test_frequency_rate:events_seen_counter"
376
+
377
+ with (
378
+ mock .patch ("sentry.models.groupsnooze.tsdb.backend.get_sums" ) as mocked_get_sums ,
379
+ mock .patch .object (
380
+ sentry .models .groupsnooze , "cache" , wraps = sentry .models .groupsnooze .cache # type: ignore[attr-defined]
381
+ ) as cache_spy ,
382
+ ):
383
+ mocked_get_sums .side_effect = [{snooze .group_id : c } for c in [95 , 98 , 100 ]]
384
+
385
+ cache_spy .set = mock .Mock (side_effect = cache_spy .set )
386
+ cache_spy .incr = mock .Mock (side_effect = cache_spy .incr )
387
+
388
+ assert snooze .is_valid (test_rates = True )
389
+ assert mocked_get_sums .call_count == 1
390
+ assert cache_spy .set .called_with (cache_key , 95 , 3600 )
391
+
392
+ assert snooze .is_valid (test_rates = True )
393
+ assert mocked_get_sums .call_count == 1
394
+ assert cache_spy .incr .called_with (cache_key )
395
+ assert cache_spy .get (cache_key ) == 96
396
+
397
+ assert snooze .is_valid (test_rates = True )
398
+ assert cache_spy .get (cache_key ) == 97
399
+ assert snooze .is_valid (test_rates = True )
400
+ assert cache_spy .get (cache_key ) == 98
401
+ assert snooze .is_valid (test_rates = True )
402
+ assert cache_spy .get (cache_key ) == 99
403
+
404
+ # cache counter reaches 100, but gets 98 from get_distinct_counts_totals
405
+
406
+ assert snooze .is_valid (test_rates = True )
407
+ assert mocked_get_sums .call_count == 2
408
+ assert cache_spy .set .called_with (cache_key , 98 , 3600 )
409
+ assert cache_spy .get (cache_key ) == 98
410
+
411
+ assert snooze .is_valid (test_rates = True )
412
+ assert cache_spy .get (cache_key ) == 99
413
+ # with this call counter reaches 100, gets 100 from get_distinct_counts_totals, so is_valid returns False
414
+ assert not snooze .is_valid (test_rates = True )
415
+ assert mocked_get_sums .call_count == 3
416
+
417
+ def test_test_frequency_rates_w_cache_expired (self ):
418
+ snooze = GroupSnooze .objects .create (group = self .group , count = 100 , window = 60 )
419
+
420
+ cache_key = f"groupsnooze:v1:{ snooze .id } :test_frequency_rate:events_seen_counter"
421
+
422
+ with (
423
+ mock .patch ("sentry.models.groupsnooze.tsdb.backend.get_sums" ) as mocked_get_sums ,
424
+ mock .patch .object (
425
+ sentry .models .groupsnooze , "cache" , wraps = sentry .models .groupsnooze .cache # type: ignore[attr-defined]
426
+ ) as cache_spy ,
427
+ ):
428
+ mocked_get_sums .side_effect = [{snooze .group_id : c } for c in [98 , 99 , 100 ]]
429
+
430
+ cache_spy .set = mock .Mock (side_effect = cache_spy .set )
431
+ cache_spy .incr = mock .Mock (side_effect = cache_spy .incr )
432
+
433
+ assert snooze .is_valid (test_rates = True )
434
+ assert mocked_get_sums .call_count == 1
435
+ assert cache_spy .set .called_with (cache_key , 98 , 3600 )
436
+
437
+ # simulate cache expiration
438
+ cache_spy .delete (cache_key )
439
+
440
+ assert snooze .is_valid (test_rates = True )
441
+ assert mocked_get_sums .call_count == 2
442
+ assert cache_spy .set .called_with (cache_key , 99 , 3600 )
443
+
444
+ # simulate cache expiration
445
+ cache_spy .delete (cache_key )
446
+
447
+ assert not snooze .is_valid (test_rates = True )
448
+ assert mocked_get_sums .call_count == 3
449
+ assert cache_spy .set .called_with (cache_key , 100 , 3600 )
0 commit comments