-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathprepared_test.go
90 lines (73 loc) · 2.04 KB
/
prepared_test.go
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
package pgcat
import (
"context"
"database/sql"
"fmt"
"sync"
"testing"
_ "github.com/lib/pq"
)
func Test(t *testing.T) {
t.Cleanup(setup(t))
t.Run("Named parameterized prepared statement works", namedParameterizedPreparedStatement)
t.Run("Unnamed parameterized prepared statement works", unnamedParameterizedPreparedStatement)
}
func namedParameterizedPreparedStatement(t *testing.T) {
db, err := sql.Open("postgres", fmt.Sprintf("host=localhost port=%d database=sharded_db user=sharding_user password=sharding_user sslmode=disable", port))
if err != nil {
t.Fatalf("could not open connection: %+v", err)
}
stmt, err := db.Prepare("SELECT $1")
if err != nil {
t.Fatalf("could not prepare: %+v", err)
}
for i := 0; i < 100; i++ {
rows, err := stmt.Query(1)
if err != nil {
t.Fatalf("could not query: %+v", err)
}
_ = rows.Close()
}
}
func unnamedParameterizedPreparedStatement(t *testing.T) {
var wg sync.WaitGroup
errCh := make(chan error, 2) // create error channel
// Have two concurrent clients executing different unnamed prepared statements
for i := 0; i < 2; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
db, err := sql.Open("postgres", fmt.Sprintf("host=localhost port=%d database=sharded_db user=sharding_user password=sharding_user sslmode=disable", port))
if err != nil {
errCh <- err // send error to channel
return
}
for j := 0; j < 100; j++ {
// Under the hood QueryContext generates an unnamed parameterized prepared statement
switch id {
case 0:
rows, err := db.QueryContext(context.Background(), "SELECT $1", 1)
if err != nil {
errCh <- err // send error to channel
return
}
_ = rows.Close()
case 1:
rows, err := db.QueryContext(context.Background(), "SELECT $1, $2", 1, 2)
if err != nil {
errCh <- err // send error to channel
return
}
_ = rows.Close()
}
}
}(i)
}
wg.Wait()
close(errCh)
for err := range errCh {
if err != nil {
t.Fatalf("received error from goroutine: %v", err)
}
}
}