-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathscript_server_sha_unit_test.go
More file actions
193 lines (164 loc) · 4.89 KB
/
script_server_sha_unit_test.go
File metadata and controls
193 lines (164 loc) · 4.89 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package redis
import (
"context"
"strings"
"sync"
"testing"
"github.com/redis/go-redis/v9/internal/proto"
)
// fakeScripter simulates enough of Scripter for unit testing Script behavior.
// It records which redis commands were invoked and can simulate NOSCRIPT.
type fakeScripter struct {
mu sync.Mutex
// call counters
scriptLoadCalls int
evalCalls int
evalShaCalls int
evalROCalls int
evalShaROCalls int
// behavior controls
hashToReturn string
// If set, the first EvalSha/EvalShaRO returns a NOSCRIPT error.
failFirstEvalShaWithNoScr bool
failFirstEvalShaROWithNoScr bool
}
func (f *fakeScripter) ScriptLoad(ctx context.Context, script string) *StringCmd {
f.mu.Lock()
f.scriptLoadCalls++
f.mu.Unlock()
cmd := NewStringCmd(ctx)
cmd.SetVal(f.hashToReturn)
return cmd
}
func (f *fakeScripter) ScriptExists(ctx context.Context, hashes ...string) *BoolSliceCmd {
cmd := NewBoolSliceCmd(ctx)
vals := make([]bool, len(hashes))
for i := range vals {
vals[i] = true
}
cmd.SetVal(vals)
return cmd
}
func (f *fakeScripter) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *Cmd {
f.mu.Lock()
f.evalCalls++
f.mu.Unlock()
cmd := NewCmd(ctx)
cmd.SetErr(nil)
return cmd
}
func (f *fakeScripter) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *Cmd {
f.mu.Lock()
f.evalShaCalls++
callNum := f.evalShaCalls
fail := f.failFirstEvalShaWithNoScr && callNum == 1
f.mu.Unlock()
cmd := NewCmd(ctx)
if fail {
// Use RedisError so go-redis NOSCRIPT detection triggers.
cmd.SetErr(proto.RedisError("NOSCRIPT No matching script. Please use EVAL."))
return cmd
}
cmd.SetErr(nil)
return cmd
}
func (f *fakeScripter) EvalRO(ctx context.Context, script string, keys []string, args ...interface{}) *Cmd {
f.mu.Lock()
f.evalROCalls++
f.mu.Unlock()
cmd := NewCmd(ctx)
cmd.SetErr(nil)
return cmd
}
func (f *fakeScripter) EvalShaRO(ctx context.Context, sha1 string, keys []string, args ...interface{}) *Cmd {
f.mu.Lock()
f.evalShaROCalls++
callNum := f.evalShaROCalls
fail := f.failFirstEvalShaROWithNoScr && callNum == 1
f.mu.Unlock()
cmd := NewCmd(ctx)
if fail {
cmd.SetErr(proto.RedisError("NOSCRIPT No matching script. Please use EVAL."))
return cmd
}
cmd.SetErr(nil)
return cmd
}
func TestNewScriptServerSHA_Run_UsesScriptLoadAndEvalSha_NoEval(t *testing.T) {
ctx := context.Background()
c := &fakeScripter{hashToReturn: strings.Repeat("a", 40)}
s := NewScriptServerSHA("return 1")
if err := s.Run(ctx, c, []string{"k"}).Err(); err != nil {
t.Fatalf("Run() err: %v", err)
}
if c.scriptLoadCalls != 1 {
t.Fatalf("expected ScriptLoad 1 call, got %d", c.scriptLoadCalls)
}
if c.evalShaCalls != 1 {
t.Fatalf("expected EvalSha 1 call, got %d", c.evalShaCalls)
}
if c.evalCalls != 0 {
t.Fatalf("expected Eval 0 calls, got %d", c.evalCalls)
}
}
func TestNewScriptServerSHA_Run_RetriesOnNoScript_NoEvalFallback(t *testing.T) {
ctx := context.Background()
c := &fakeScripter{
hashToReturn: strings.Repeat("b", 40),
failFirstEvalShaWithNoScr: true,
}
s := NewScriptServerSHA("return 1")
if err := s.Run(ctx, c, []string{"k"}).Err(); err != nil {
t.Fatalf("Run() err: %v", err)
}
// Expected:
// - ScriptLoad once (hash cached in Script)
// - EvalSha twice (first NOSCRIPT, second retry)
if c.scriptLoadCalls != 1 {
t.Fatalf("expected ScriptLoad 1 call (hash cached), got %d", c.scriptLoadCalls)
}
if c.evalShaCalls != 2 {
t.Fatalf("expected EvalSha 2 calls (retry), got %d", c.evalShaCalls)
}
// Critical: serverSHA path should NOT fallback to Eval
if c.evalCalls != 0 {
t.Fatalf("expected Eval 0 calls, got %d", c.evalCalls)
}
}
func TestNewScriptServerSHA_RunRO_UsesScriptLoadAndEvalShaRO_NoEvalRO(t *testing.T) {
ctx := context.Background()
c := &fakeScripter{hashToReturn: strings.Repeat("c", 40)}
s := NewScriptServerSHA("return 1")
if err := s.RunRO(ctx, c, []string{"k"}).Err(); err != nil {
t.Fatalf("RunRO() err: %v", err)
}
if c.scriptLoadCalls != 1 {
t.Fatalf("expected ScriptLoad 1 call, got %d", c.scriptLoadCalls)
}
if c.evalShaROCalls != 1 {
t.Fatalf("expected EvalShaRO 1 call, got %d", c.evalShaROCalls)
}
if c.evalROCalls != 0 {
t.Fatalf("expected EvalRO 0 calls, got %d", c.evalROCalls)
}
}
func TestNewScriptServerSHA_RunRO_RetriesOnNoScript_NoEvalROFallback(t *testing.T) {
ctx := context.Background()
c := &fakeScripter{
hashToReturn: strings.Repeat("d", 40),
failFirstEvalShaROWithNoScr: true,
}
s := NewScriptServerSHA("return 1")
if err := s.RunRO(ctx, c, []string{"k"}).Err(); err != nil {
t.Fatalf("RunRO() err: %v", err)
}
if c.scriptLoadCalls != 1 {
t.Fatalf("expected ScriptLoad 1 call (hash cached), got %d", c.scriptLoadCalls)
}
if c.evalShaROCalls != 2 {
t.Fatalf("expected EvalShaRO 2 calls (retry), got %d", c.evalShaROCalls)
}
if c.evalROCalls != 0 {
t.Fatalf("expected EvalRO 0 calls, got %d", c.evalROCalls)
}
}