-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathjdbc_test.go
More file actions
315 lines (280 loc) · 8.56 KB
/
jdbc_test.go
File metadata and controls
315 lines (280 loc) · 8.56 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
package integration
import (
"context"
"crypto/tls"
"fmt"
"sync"
"testing"
"time"
"github.com/jackc/pgx/v5"
)
// pgxConnect creates a pgx connection to Duckgres (uses extended query protocol).
func pgxConnect(t *testing.T) *pgx.Conn {
t.Helper()
connStr := fmt.Sprintf("host=127.0.0.1 port=%d user=testuser password=testpass dbname=test sslmode=require", testHarness.dgPort)
cfg, err := pgx.ParseConfig(connStr)
if err != nil {
t.Fatalf("Failed to parse config: %v", err)
}
cfg.TLSConfig = &tls.Config{InsecureSkipVerify: true}
ctx := context.Background()
conn, err := pgx.ConnectConfig(ctx, cfg)
if err != nil {
t.Fatalf("Failed to connect: %v", err)
}
return conn
}
// TestJDBCVersionQuery tests SELECT version() via extended query protocol,
// which is the pattern JDBC/Metabase uses on connection and that hangs sporadically.
func TestJDBCVersionQuery(t *testing.T) {
ctx := context.Background()
t.Run("simple_version", func(t *testing.T) {
conn := pgxConnect(t)
defer func() { _ = conn.Close(ctx) }()
var ver string
err := conn.QueryRow(ctx, "SELECT version()").Scan(&ver)
if err != nil {
t.Fatalf("SELECT version() failed: %v", err)
}
if ver == "" {
t.Error("version() returned empty string")
}
t.Logf("version() = %s", ver)
})
t.Run("version_with_timeout", func(t *testing.T) {
conn := pgxConnect(t)
defer func() { _ = conn.Close(ctx) }()
tctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var ver string
err := conn.QueryRow(tctx, "SELECT version()").Scan(&ver)
if err != nil {
t.Fatalf("SELECT version() timed out or failed: %v", err)
}
})
t.Run("repeated_version_same_connection", func(t *testing.T) {
conn := pgxConnect(t)
defer func() { _ = conn.Close(ctx) }()
for i := 0; i < 50; i++ {
tctx, cancel := context.WithTimeout(ctx, 5*time.Second)
var ver string
err := conn.QueryRow(tctx, "SELECT version()").Scan(&ver)
cancel()
if err != nil {
t.Fatalf("Iteration %d: SELECT version() failed: %v", i, err)
}
}
})
t.Run("repeated_version_new_connections", func(t *testing.T) {
for i := 0; i < 20; i++ {
conn := pgxConnect(t)
tctx, cancel := context.WithTimeout(ctx, 5*time.Second)
var ver string
err := conn.QueryRow(tctx, "SELECT version()").Scan(&ver)
cancel()
_ = conn.Close(ctx)
if err != nil {
t.Fatalf("Connection %d: SELECT version() failed: %v", i, err)
}
}
})
}
// TestJDBCConcurrentVersionQueries tests concurrent version() queries
// across multiple connections, simulating a connection pool.
func TestJDBCConcurrentVersionQueries(t *testing.T) {
ctx := context.Background()
numWorkers := 5
queriesPerWorker := 20
timeout := 30 * time.Second
tctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
var wg sync.WaitGroup
errors := make(chan error, numWorkers*queriesPerWorker)
for w := 0; w < numWorkers; w++ {
wg.Add(1)
go func(workerID int) {
defer wg.Done()
conn := pgxConnect(t)
defer func() { _ = conn.Close(context.Background()) }()
for i := 0; i < queriesPerWorker; i++ {
qctx, qcancel := context.WithTimeout(tctx, 5*time.Second)
var ver string
err := conn.QueryRow(qctx, "SELECT version()").Scan(&ver)
qcancel()
if err != nil {
errors <- fmt.Errorf("worker %d, query %d: %w", workerID, i, err)
return
}
}
}(w)
}
wg.Wait()
close(errors)
for err := range errors {
t.Error(err)
}
}
// TestJDBCMetabaseProbeQueries tests the specific queries Metabase sends
// when probing a connection, using the extended query protocol.
func TestJDBCMetabaseProbeQueries(t *testing.T) {
ctx := context.Background()
probeQueries := []struct {
name string
sql string
}{
{"version", "SELECT version()"},
{"current_setting_version_num", "SELECT current_setting('server_version_num')"},
{"current_setting_version", "SELECT current_setting('server_version')"},
{"select_1", "SELECT 1"},
}
// Test via extended query protocol (pgx uses it by default)
for _, q := range probeQueries {
t.Run("extended_"+q.name, func(t *testing.T) {
conn := pgxConnect(t)
defer func() { _ = conn.Close(ctx) }()
tctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
// Scan as string to handle all return types uniformly (matches JDBC text format)
rows, err := conn.Query(tctx, q.sql)
if err != nil {
t.Fatalf("Query %q failed: %v", q.sql, err)
}
if !rows.Next() {
rows.Close()
t.Fatalf("Query %q returned no rows", q.sql)
}
rawValues := rows.RawValues()
rows.Close()
t.Logf("%s = %s", q.sql, string(rawValues[0]))
})
}
// Test via simple query protocol (lib/pq through database/sql)
for _, q := range probeQueries {
t.Run("simple_"+q.name, func(t *testing.T) {
var val string
err := testHarness.DuckgresDB.QueryRow(q.sql).Scan(&val)
if err != nil {
t.Fatalf("Query %q failed: %v", q.sql, err)
}
t.Logf("%s = %s", q.sql, val)
})
}
}
// TestJDBCDatabaseMetaDataQueries tests queries similar to what JDBC
// DatabaseMetaData methods generate (getTables, getSchemas, etc.).
func TestJDBCDatabaseMetaDataQueries(t *testing.T) {
// These are representative of what the PostgreSQL JDBC driver generates
// for DatabaseMetaData calls
metadataQueries := []struct {
name string
sql string
}{
{
"getTables",
`SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM, c.relname AS TABLE_NAME,
CASE c.relkind WHEN 'r' THEN 'TABLE' WHEN 'v' THEN 'VIEW' END AS TABLE_TYPE
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v')
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY TABLE_SCHEM, TABLE_NAME
LIMIT 20`,
},
{
"getSchemas",
`SELECT nspname AS TABLE_SCHEM, NULL AS TABLE_CATALOG
FROM pg_catalog.pg_namespace
WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema'
ORDER BY TABLE_SCHEM`,
},
{
"getCatalogs",
`SELECT datname AS TABLE_CAT FROM pg_catalog.pg_database ORDER BY 1`,
},
{
"getTypeInfo",
`SELECT typname FROM pg_catalog.pg_type LIMIT 20`,
},
}
for _, q := range metadataQueries {
t.Run(q.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
conn := pgxConnect(t)
defer func() { _ = conn.Close(ctx) }()
rows, err := conn.Query(ctx, q.sql)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
count := 0
for rows.Next() {
count++
}
if err := rows.Err(); err != nil {
t.Fatalf("Row iteration error: %v", err)
}
rows.Close()
t.Logf("%s returned %d rows", q.name, count)
})
}
}
// TestJDBCPreparedStatementReuse tests reusing prepared statements,
// which is a common JDBC pattern.
func TestJDBCPreparedStatementReuse(t *testing.T) {
ctx := context.Background()
conn := pgxConnect(t)
defer func() { _ = conn.Close(ctx) }()
// Create table
_, err := conn.Exec(ctx, "DROP TABLE IF EXISTS jdbc_prep_test")
if err != nil {
t.Fatalf("DROP TABLE failed: %v", err)
}
_, err = conn.Exec(ctx, "CREATE TABLE jdbc_prep_test (id INTEGER, val VARCHAR)")
if err != nil {
t.Fatalf("CREATE TABLE failed: %v", err)
}
defer func() {
_, _ = conn.Exec(ctx, "DROP TABLE IF EXISTS jdbc_prep_test")
}()
// Insert using prepared statement reuse (use pgx.QueryExecModeSimpleProtocol
// to avoid type encoding issues with pgx's extended protocol for DML)
for i := 0; i < 20; i++ {
tctx, cancel := context.WithTimeout(ctx, 5*time.Second)
_, err := conn.Exec(tctx, fmt.Sprintf("INSERT INTO jdbc_prep_test VALUES (%d, 'val_%d')", i, i))
cancel()
if err != nil {
t.Fatalf("Insert %d failed: %v", i, err)
}
}
// Query using prepared statement reuse (text mode via pgx)
for i := 0; i < 20; i++ {
tctx, cancel := context.WithTimeout(ctx, 5*time.Second)
var val string
err := conn.QueryRow(tctx, "SELECT val FROM jdbc_prep_test WHERE id = $1",
pgx.QueryExecModeSimpleProtocol, i).Scan(&val)
cancel()
if err != nil {
t.Fatalf("Select %d failed: %v", i, err)
}
expected := fmt.Sprintf("val_%d", i)
if val != expected {
t.Errorf("Expected %q, got %q", expected, val)
}
}
}
// TestJDBCRapidReconnect tests rapid connect/query/disconnect cycles,
// which can trigger timing issues in the protocol handshake.
func TestJDBCRapidReconnect(t *testing.T) {
ctx := context.Background()
for i := 0; i < 30; i++ {
conn := pgxConnect(t)
tctx, cancel := context.WithTimeout(ctx, 5*time.Second)
var val string
err := conn.QueryRow(tctx, "SELECT 'ok'").Scan(&val)
cancel()
_ = conn.Close(ctx)
if err != nil {
t.Fatalf("Reconnect %d: query failed: %v", i, err)
}
}
}