Add cache test for admins (#31265)

Add a test to probe the cache similar to the email test func.


![image](700e2733-586d-4091-900f-f5f71e6e94bf)


![image](2a953802-18fc-4e81-a37d-24ebe1297365)


![image](e00d62ad-bb60-41cc-9138-09993daee156)

---------

Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
6543 2024-06-17 21:22:39 +02:00 committed by GitHub
parent 4b6eb46e69
commit 363c123598
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 119 additions and 27 deletions

View file

@ -4,6 +4,7 @@
package cache
import (
"fmt"
"strconv"
"time"
@ -35,6 +36,37 @@ func Init() error {
return nil
}
const (
testCacheKey = "DefaultCache.TestKey"
SlowCacheThreshold = 100 * time.Microsecond
)
func Test() (time.Duration, error) {
if defaultCache == nil {
return 0, fmt.Errorf("default cache not initialized")
}
testData := fmt.Sprintf("%x", make([]byte, 500))
start := time.Now()
if err := defaultCache.Delete(testCacheKey); err != nil {
return 0, fmt.Errorf("expect cache to delete data based on key if exist but got: %w", err)
}
if err := defaultCache.Put(testCacheKey, testData, 10); err != nil {
return 0, fmt.Errorf("expect cache to store data but got: %w", err)
}
testVal, hit := defaultCache.Get(testCacheKey)
if !hit {
return 0, fmt.Errorf("expect cache hit but got none")
}
if testVal != testData {
return 0, fmt.Errorf("expect cache to return same value as stored but got other")
}
return time.Since(start), nil
}
// GetCache returns the currently configured cache
func GetCache() StringCache {
return defaultCache

View file

@ -34,6 +34,18 @@ func TestNewContext(t *testing.T) {
assert.Nil(t, con)
}
func TestTest(t *testing.T) {
defaultCache = nil
_, err := Test()
assert.Error(t, err)
createTestCache()
elapsed, err := Test()
assert.NoError(t, err)
// mem cache should take from 300ns up to 1ms on modern hardware ...
assert.Less(t, elapsed, SlowCacheThreshold)
}
func TestGetCache(t *testing.T) {
createTestCache()