-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredisclient_test.go
More file actions
53 lines (46 loc) · 1.54 KB
/
redisclient_test.go
File metadata and controls
53 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
"testing"
"github.com/alicebob/miniredis/v2"
"go.uber.org/zap"
)
func TestNewRedisClient_Success(t *testing.T) {
mr := miniredis.RunT(t)
client, err := newRedisClient(context.Background(), "redis://"+mr.Addr(), zap.NewNop())
if err != nil {
t.Fatalf("newRedisClient returned error: %v", err)
}
if client == nil {
t.Fatal("newRedisClient returned nil client")
}
t.Cleanup(func() { _ = client.Close() })
if err := client.Ping(context.Background()).Err(); err != nil {
t.Errorf("Expected ping to succeed, got %v", err)
}
}
func TestNewRedisClient_InvalidURL(t *testing.T) {
client, err := newRedisClient(context.Background(), "://not-a-url", zap.NewNop())
if err == nil {
t.Fatal("Expected error for malformed URL")
}
if client != nil {
t.Errorf("Expected nil client on parse error, got %v", client)
}
}
// TestNewRedisClient_PingFailureFailsOpen verifies that a Redis server that
// is unreachable at startup does not abort the constructor — the client is
// returned anyway so the rest of the app can boot and other components can
// fail open at runtime.
func TestNewRedisClient_PingFailureFailsOpen(t *testing.T) {
// Tight dial timeout keeps the test fast despite go-redis pool retries.
client, err := newRedisClient(context.Background(),
"redis://127.0.0.1:1?dial_timeout=100ms", zap.NewNop())
if err != nil {
t.Fatalf("Expected no error on PING failure, got %v", err)
}
if client == nil {
t.Fatal("Expected non-nil client even when PING fails")
}
t.Cleanup(func() { _ = client.Close() })
}