fix: make cache module in test-remote-cacher run against remote redis

This commit is contained in:
Mathieu Fenniak 2025-11-15 14:40:02 -07:00
commit f70815a034
No known key found for this signature in database

View file

@ -8,18 +8,36 @@ import (
"testing"
"time"
"code.forgejo.org/go-chi/cache"
"forgejo.org/modules/setting"
"forgejo.org/modules/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func createTestCache() {
conn, _ = newCache(setting.Cache{
Adapter: "memory",
TTL: time.Minute,
})
setting.CacheService.TTL = 24 * time.Hour
func createTestCache(t *testing.T) {
var err error
var testCache cache.Cache
testRedisHost := os.Getenv("TEST_REDIS_SERVER")
if testRedisHost == "" {
testCache, err = newCache(setting.Cache{
Adapter: "memory",
TTL: time.Minute,
})
} else {
testCache, err = newCache(setting.Cache{
Adapter: "redis",
Conn: fmt.Sprintf("redis://%s", testRedisHost),
TTL: time.Minute,
})
}
require.NoError(t, err)
require.NotNil(t, testCache)
t.Cleanup(test.MockVariableValue(&conn, testCache))
t.Cleanup(test.MockVariableValue(&setting.CacheService.TTL, 24*time.Hour))
}
func TestNewContext(t *testing.T) {
@ -36,13 +54,13 @@ func TestNewContext(t *testing.T) {
}
func TestGetCache(t *testing.T) {
createTestCache()
createTestCache(t)
assert.NotNil(t, GetCache())
}
func TestGetString(t *testing.T) {
createTestCache()
createTestCache(t)
data, err := GetString("key", func() (string, error) {
return "", errors.New("some error")
@ -78,7 +96,7 @@ func TestGetString(t *testing.T) {
}
func TestGetInt(t *testing.T) {
createTestCache()
createTestCache(t)
data, err := GetInt("key", func() (int, error) {
return 0, errors.New("some error")
@ -114,7 +132,7 @@ func TestGetInt(t *testing.T) {
}
func TestGetInt64(t *testing.T) {
createTestCache()
createTestCache(t)
data, err := GetInt64("key", func() (int64, error) {
return 0, errors.New("some error")