-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstmt_cache_test.go
More file actions
374 lines (340 loc) · 9.99 KB
/
Copy pathstmt_cache_test.go
File metadata and controls
374 lines (340 loc) · 9.99 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package sqlite
import (
"context"
"database/sql"
"errors"
"sync/atomic"
"testing"
)
// TestStmtCache_BasicLRU walks the LRU directly: put 3 entries with capacity
// 2, observe eviction of the LRU on the 3rd put. We bypass the conn so the
// test exercises only the data structure, not SQLite.
func TestStmtCache_BasicLRU(t *testing.T) {
c := newStmtCache(2)
c.put("a", 100, 200)
c.put("b", 101, 201)
if c.len() != 2 {
t.Fatalf("after 2 puts len=%d, want 2", c.len())
}
// Third put exceeds capacity → evicts LRU (which is "a").
evicted := c.put("c", 102, 202)
if evicted == nil {
t.Fatal("expected eviction on 3rd put")
}
if evicted.key != "a" {
t.Errorf("evicted %q, want %q", evicted.key, "a")
}
// "a" should now be a miss.
if got := c.take("a"); got != nil {
t.Errorf("take(a) after eviction returned %+v, want nil", got)
}
// "b" and "c" still cached.
if got := c.take("b"); got == nil || got.pstmt != 201 {
t.Errorf("take(b) = %+v, want pstmt=201", got)
}
if got := c.take("c"); got == nil || got.pstmt != 202 {
t.Errorf("take(c) = %+v, want pstmt=202", got)
}
}
// TestStmtCache_TakeMovesNothing asserts take() pulls the entry out of the
// cache cleanly — subsequent take of the same key misses, and the cache
// length drops.
func TestStmtCache_TakeMovesNothing(t *testing.T) {
c := newStmtCache(2)
c.put("x", 1, 2)
if c.len() != 1 {
t.Fatalf("len after put = %d, want 1", c.len())
}
if e := c.take("x"); e == nil {
t.Fatal("take returned nil")
}
if c.len() != 0 {
t.Errorf("len after take = %d, want 0", c.len())
}
if e := c.take("x"); e != nil {
t.Errorf("second take returned %+v, want nil", e)
}
}
// TestStmtCache_PutReplacesSameKey verifies that putting the same SQL key
// twice does not grow the cache and evicts the older entry's handles.
func TestStmtCache_PutReplacesSameKey(t *testing.T) {
c := newStmtCache(2)
c.put("k", 10, 20)
evicted := c.put("k", 11, 21)
if evicted == nil {
t.Fatal("expected eviction of prior entry with same key")
}
if evicted.pstmt != 20 {
t.Errorf("evicted pstmt=%d, want 20", evicted.pstmt)
}
if c.len() != 1 {
t.Errorf("len after replace = %d, want 1", c.len())
}
}
// TestStmtCache_Disabled is a no-op cache; put returns the entry back to
// the caller for immediate finalization, and take always misses.
func TestStmtCache_Disabled(t *testing.T) {
c := newStmtCache(0)
if c.enabled() {
t.Fatal("cap=0 should be disabled")
}
if got := c.take("anything"); got != nil {
t.Errorf("take on disabled cache returned %+v, want nil", got)
}
returned := c.put("k", 1, 2)
if returned == nil || returned.pstmt != 2 {
t.Errorf("disabled put should hand the entry back; got %+v", returned)
}
if c.len() != 0 {
t.Errorf("disabled cache len = %d, want 0", c.len())
}
}
// TestStmtCache_NormalizeTrimsWhitespace checks the canonical key transform:
// SQL with extra surrounding whitespace hits the same cache entry.
func TestStmtCache_NormalizeTrimsWhitespace(t *testing.T) {
c := newStmtCache(2)
c.put("SELECT 1", 1, 2)
if got := c.take(" SELECT 1\n"); got == nil {
t.Errorf("trimmed SQL should match cached key")
}
}
// TestStmtCache_DrainAll empties the cache and returns every entry. Used by
// conn.Close to finalize cached pstmts.
func TestStmtCache_DrainAll(t *testing.T) {
c := newStmtCache(3)
c.put("a", 1, 10)
c.put("b", 2, 20)
c.put("c", 3, 30)
got := c.drainAll()
if len(got) != 3 {
t.Errorf("drainAll returned %d entries, want 3", len(got))
}
if c.len() != 0 {
t.Errorf("after drainAll len = %d, want 0", c.len())
}
}
// TestStmtCache_PrepareCacheHit is the end-to-end happy-path: open a DB,
// Prepare the same query twice via two separate *sql.Stmt values, observe
// the second prepare reuse the cached pstmt.
//
// We hook directly into a *Conn to count cache hits/misses without exposing
// internal counters to the public API.
func TestStmtCache_PrepareCacheHit(t *testing.T) {
db, err := sql.Open(DriverNameSQLite3, ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
db.SetMaxOpenConns(1)
ctx := context.Background()
sc, err := db.Conn(ctx)
if err != nil {
t.Fatal(err)
}
defer sc.Close()
var c *Conn
if err := sc.Raw(func(dc any) error {
c = dc.(*Conn)
return nil
}); err != nil {
t.Fatal(err)
}
if _, err := sc.ExecContext(ctx, "CREATE TABLE t (x INTEGER)"); err != nil {
t.Fatal(err)
}
baseLen := c.stmts.len() // CREATE TABLE + anything database/sql cached
// Prepare + close 10 times. After the first iteration, the cache should
// contain the SELECT and subsequent prepares are hits — the cache
// length should grow by exactly 1, not 10.
const query = "SELECT x FROM t WHERE x > ?"
for range 10 {
stmt, err := sc.PrepareContext(ctx, query)
if err != nil {
t.Fatal(err)
}
rows, err := stmt.QueryContext(ctx, 0)
if err != nil {
stmt.Close()
t.Fatal(err)
}
for rows.Next() {
}
rows.Close()
if err := stmt.Close(); err != nil {
t.Fatal(err)
}
}
if delta := c.stmts.len() - baseLen; delta != 1 {
t.Errorf("cache grew by %d after 10 prepares of one query, want 1", delta)
}
// A different query bumps the cache by exactly one more.
stmt2, err := sc.PrepareContext(ctx, "SELECT count(*) FROM t")
if err != nil {
t.Fatal(err)
}
stmt2.Close()
if delta := c.stmts.len() - baseLen; delta != 2 {
t.Errorf("cache grew by %d after second distinct prepare, want 2", delta)
}
}
// TestStmtCache_CapZeroDisablesViaDSN asserts that `_stmt_cache_size=0`
// turns the cache off, and prepares are not retained between Close calls.
func TestStmtCache_CapZeroDisablesViaDSN(t *testing.T) {
db, err := sql.Open(DriverNameSQLite3, ":memory:?_stmt_cache_size=0")
if err != nil {
t.Fatal(err)
}
defer db.Close()
db.SetMaxOpenConns(1)
ctx := context.Background()
sc, err := db.Conn(ctx)
if err != nil {
t.Fatal(err)
}
defer sc.Close()
var c *Conn
if err := sc.Raw(func(dc any) error {
c = dc.(*Conn)
return nil
}); err != nil {
t.Fatal(err)
}
if _, err := sc.ExecContext(ctx, "CREATE TABLE t (x INTEGER)"); err != nil {
t.Fatal(err)
}
stmt, err := sc.PrepareContext(ctx, "SELECT x FROM t")
if err != nil {
t.Fatal(err)
}
stmt.Close()
if c.stmts.enabled() {
t.Errorf("_stmt_cache_size=0 should disable the cache")
}
if got := c.stmts.len(); got != 0 {
t.Errorf("disabled cache len = %d, want 0", got)
}
}
// TestStmtCache_FinalizesOnConnClose proves that closing the connection
// finalizes every retained pstmt and frees its psql. We don't have a
// public way to inspect SQLite's "open statements count" so we observe the
// effect indirectly: a connection with retained prepares can be closed
// without sqlite3_close returning SQLITE_BUSY.
func TestStmtCache_FinalizesOnConnClose(t *testing.T) {
db, err := sql.Open(DriverNameSQLite3, ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
db.SetMaxOpenConns(1)
ctx := context.Background()
sc, err := db.Conn(ctx)
if err != nil {
t.Fatal(err)
}
// Stash multiple prepared statements in the cache before closing.
for _, q := range []string{"SELECT 1", "SELECT 2", "SELECT 3"} {
stmt, err := sc.PrepareContext(ctx, q)
if err != nil {
sc.Close()
t.Fatal(err)
}
stmt.Close()
}
// Closing the conn must succeed even though the cache holds pstmts.
if err := sc.Close(); err != nil {
t.Fatalf("Conn.Close with cached stmts: %v", err)
}
}
// TestStmtCache_ResetClearsBindingsBetweenUses asserts the reset path:
// bind args to one stmt, close it, prepare the same SQL again, leave the
// args UNbound — execution should use the new bindings (NULL) rather than
// carry over the old ones.
func TestStmtCache_ResetClearsBindingsBetweenUses(t *testing.T) {
db, err := sql.Open(DriverNameSQLite3, ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
db.SetMaxOpenConns(1)
ctx := context.Background()
sc, err := db.Conn(ctx)
if err != nil {
t.Fatal(err)
}
defer sc.Close()
if _, err := sc.ExecContext(ctx, "CREATE TABLE t (x)"); err != nil {
t.Fatal(err)
}
// First use: bind 42 and confirm.
stmt, err := sc.PrepareContext(ctx, "SELECT ?")
if err != nil {
t.Fatal(err)
}
var got int
if err := stmt.QueryRowContext(ctx, 42).Scan(&got); err != nil {
stmt.Close()
t.Fatal(err)
}
stmt.Close()
if got != 42 {
t.Errorf("first use scan = %d, want 42", got)
}
// Second use of the SAME SQL — pulled from cache. Bind a different
// value; if reset didn't clear, we'd see 42 here instead of 7.
stmt, err = sc.PrepareContext(ctx, "SELECT ?")
if err != nil {
t.Fatal(err)
}
defer stmt.Close()
if err := stmt.QueryRowContext(ctx, 7).Scan(&got); err != nil {
t.Fatal(err)
}
if got != 7 {
t.Errorf("cache-reused stmt scan = %d, want 7 (reset failed?)", got)
}
}
// TestStmtCache_DistinctErrorOnClosedConn confirms operating on a *Conn
// after a clean close does not panic and surfaces a sensible error. This
// is mostly a regression guard: an earlier draft of the cache could leave
// `c.stmts` pointing at stale entries after Conn.Close.
func TestStmtCache_DistinctErrorOnClosedConn(t *testing.T) {
db, err := sql.Open(DriverNameSQLite3, ":memory:")
if err != nil {
t.Fatal(err)
}
db.SetMaxOpenConns(1)
ctx := context.Background()
sc, err := db.Conn(ctx)
if err != nil {
t.Fatal(err)
}
// Stash some retained pstmts then close the conn.
stmt, err := sc.PrepareContext(ctx, "SELECT 1")
if err != nil {
t.Fatal(err)
}
stmt.Close()
if err := sc.Close(); err != nil {
t.Fatal(err)
}
// db.Close should also be clean.
if err := db.Close(); err != nil {
t.Fatal(err)
}
// Sanity: explicitly running a query after close errors instead of
// panicking.
var v atomic.Int32
defer func() {
if r := recover(); r != nil {
t.Errorf("panic from db.Query after Close: %v", r)
}
v.Store(1)
}()
row := db.QueryRowContext(ctx, "SELECT 1")
if err := row.Scan(new(int)); !errors.Is(err, sql.ErrConnDone) && err == nil {
// Whatever the specific error, it should not be nil and should not
// panic. The exact identity (ErrConnDone vs a driver error) varies
// across Go versions; just assert non-nil.
t.Errorf("expected an error after Close, got nil")
}
}