redis_test.go 813 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/redis/go-redis/v9"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. var mockRedisOptions = &redis.Options{
  9. Addr: "localhost:6379",
  10. Password: "",
  11. DB: 0,
  12. }
  13. func TestGetRedisClient(t *testing.T) {
  14. client := GetRedisClient()
  15. assert.Nil(t, client)
  16. initRedisClient(mockRedisOptions)
  17. client = GetRedisClient()
  18. assert.NotNil(t, client)
  19. // Test redis exec commands and response
  20. ctx := context.Background()
  21. rs := client.Ping(ctx)
  22. assert.Nil(t, rs.Err())
  23. assert.Equal(t, "PONG", rs.Val())
  24. rsCmd := GetRedisClient().Do(ctx, "dbsize")
  25. assert.Nil(t, rsCmd.Err())
  26. }
  27. func BenchmarkGetRedisClient(b *testing.B) {
  28. initRedisClient(mockRedisOptions)
  29. b.ResetTimer()
  30. for i := 0; i < b.N; i++ {
  31. GetRedisClient().Get(context.Background(), "key")
  32. }
  33. }