Files
mtusi2026lab4/internal/storage/redis/main_test.go
T
2026-10-15 20:45:29 +03:00

67 lines
1.2 KiB
Go

//go:build integration
package redis_test
import (
"context"
"os"
"strconv"
"time"
goredis "github.com/redis/go-redis/v9"
"mtusi-messenger/internal/platform/cache"
)
const testRedisDB = 1
func redisEnv(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
func redisEnvInt(key string, fallback int) int {
value := os.Getenv(key)
if value == "" {
return fallback
}
parsed, err := strconv.Atoi(value)
if err != nil {
return fallback
}
return parsed
}
func testRedisConfig() cache.RedisConfig {
return cache.RedisConfig{
Host: redisEnv("TEST_REDIS_HOST", "localhost"),
Port: redisEnvInt("TEST_REDIS_PORT", 6379),
Password: os.Getenv("TEST_REDIS_PASSWORD"),
DB: redisEnvInt("TEST_REDIS_DB", testRedisDB),
MaxRetries: 3,
DialTimeout: 5 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
PoolSize: 5,
}
}
func flushRedisDB(ctx context.Context) error {
cfg := testRedisConfig()
client := goredis.NewClient(&goredis.Options{
Addr: cfg.Host + ":" + strconv.Itoa(cfg.Port),
Password: cfg.Password,
DB: cfg.DB,
})
defer func() { _ = client.Close() }()
return client.FlushDB(ctx).Err()
}