1- from unittest import skipUnless
1+ from django .core .cache import caches
2+ from django .test import TestCase
3+ from redis import RedisError
24
35from django_prometheus .testutils import PrometheusTestCaseMixin
4- from django .test import TestCase
5- from django .core .cache import caches
66
77
88class TestCachesMetrics (PrometheusTestCaseMixin , TestCase ):
99 """Test django_prometheus.caches metrics."""
1010
1111 def testCounters (self ):
12- supported_caches = ['memcached' , 'filebased' , 'locmem' ]
12+ supported_caches = ['memcached' , 'filebased' , 'locmem' , 'redis' ]
1313
1414 # Note: those tests require a memcached server running
1515 for supported_cache in supported_caches :
1616 tested_cache = caches [supported_cache ]
1717
18+ total_before = self .getMetric (
19+ 'django_cache_get_total' , backend = supported_cache ) or 0
20+ hit_before = self .getMetric (
21+ 'django_cache_get_hits_total' , backend = supported_cache ) or 0
22+ miss_before = self .getMetric (
23+ 'django_cache_get_misses_total' , backend = supported_cache ) or 0
24+
1825 tested_cache .set ('foo1' , 'bar' )
1926 tested_cache .get ('foo1' )
2027 tested_cache .get ('foo1' )
@@ -25,14 +32,72 @@ def testCounters(self):
2532 assert result == 'default'
2633
2734 self .assertMetricEquals (
28- 4 , 'django_cache_get_total' , backend = supported_cache )
35+ total_before + 4 ,
36+ 'django_cache_get_total' , backend = supported_cache )
2937 self .assertMetricEquals (
30- 2 , 'django_cache_get_hits_total' , backend = supported_cache )
38+ hit_before + 2 ,
39+ 'django_cache_get_hits_total' , backend = supported_cache )
3140 self .assertMetricEquals (
32- 2 , 'django_cache_get_misses_total' , backend = supported_cache )
41+ miss_before + 2 ,
42+ 'django_cache_get_misses_total' , backend = supported_cache )
43+
44+ def test_redis_cache_fail (self ):
45+
46+ # Note: test use fake service config (like if server was stopped)
47+ supported_cache = 'redis'
48+
49+ total_before = self .getMetric (
50+ 'django_cache_get_total' , backend = supported_cache ) or 0
51+ fail_before = self .getMetric (
52+ 'django_cache_get_fail_total' , backend = supported_cache ) or 0
53+ hit_before = self .getMetric (
54+ 'django_cache_get_hits_total' , backend = supported_cache ) or 0
55+ miss_before = self .getMetric (
56+ 'django_cache_get_misses_total' , backend = supported_cache ) or 0
57+
58+ tested_cache = caches ['stopped_redis_ignore_exception' ]
59+ tested_cache .get ('foo1' )
60+
61+ self .assertMetricEquals (
62+ hit_before ,
63+ 'django_cache_get_hits_total' , backend = supported_cache
64+ )
65+ self .assertMetricEquals (
66+ miss_before ,
67+ 'django_cache_get_misses_total' , backend = supported_cache
68+ )
69+ self .assertMetricEquals (
70+ total_before + 1 ,
71+ 'django_cache_get_total' , backend = supported_cache
72+ )
73+ self .assertMetricEquals (
74+ fail_before + 1 ,
75+ 'django_cache_get_fail_total' , backend = supported_cache
76+ )
77+
78+ tested_cache = caches ['stopped_redis' ]
79+ with self .assertRaises (RedisError ):
80+ tested_cache .get ('foo1' )
81+
82+ self .assertMetricEquals (
83+ hit_before ,
84+ 'django_cache_get_hits_total' , backend = supported_cache
85+ )
86+ self .assertMetricEquals (
87+ miss_before ,
88+ 'django_cache_get_misses_total' , backend = supported_cache
89+ )
90+ self .assertMetricEquals (
91+ total_before + 2 ,
92+ 'django_cache_get_total' , backend = supported_cache
93+ )
94+ self .assertMetricEquals (
95+ fail_before + 2 ,
96+ 'django_cache_get_fail_total' , backend = supported_cache
97+ )
3398
3499 def test_cache_version_support (self ):
35- supported_caches = ['memcached' , 'filebased' , 'locmem' ]
100+ supported_caches = ['memcached' , 'filebased' , 'locmem' , 'redis' ]
36101
37102 # Note: those tests require a memcached server running
38103 for supported_cache in supported_caches :
0 commit comments