-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinconn_test.go
More file actions
67 lines (59 loc) · 1.6 KB
/
Copy pathpinconn_test.go
File metadata and controls
67 lines (59 loc) · 1.6 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
package sqlite
import (
"context"
"testing"
)
func TestPinConn(t *testing.T) {
// Shared in-memory so the pinned conn and the pool see the same DB.
db, err := OpenShared("pinconn_" + t.Name())
if err != nil {
t.Fatalf("OpenShared: %v", err)
}
defer db.Close()
ctx := context.Background()
if _, err := db.ExecContext(ctx, `CREATE TABLE t(b BLOB)`); err != nil {
t.Fatalf("CREATE: %v", err)
}
res, err := db.ExecContext(ctx, `INSERT INTO t(b) VALUES (zeroblob(4))`)
if err != nil {
t.Fatalf("INSERT: %v", err)
}
rowid, _ := res.LastInsertId()
c, release, err := db.PinConn(ctx)
if err != nil {
t.Fatalf("PinConn: %v", err)
}
if c == nil {
t.Fatal("PinConn returned a nil *Conn")
}
// The whole point: a connection-scoped op (OpenBlob) without the
// db.Conn + Raw + type-assert dance.
b, err := c.OpenBlob("main", "t", "b", rowid, true)
if err != nil {
t.Fatalf("OpenBlob: %v", err)
}
if _, err := b.WriteAt([]byte("data"), 0); err != nil {
t.Fatalf("WriteAt: %v", err)
}
if err := b.Close(); err != nil {
t.Fatalf("blob Close: %v", err)
}
if err := release(); err != nil {
t.Fatalf("release: %v", err)
}
// Connection returned to the pool: pinning again still succeeds.
c2, release2, err := db.PinConn(ctx)
if err != nil || c2 == nil {
t.Fatalf("second PinConn = (%v, %v)", c2, err)
}
if err := release2(); err != nil {
t.Fatalf("second release: %v", err)
}
var got []byte
if err := db.QueryRowContext(ctx, `SELECT b FROM t`).Scan(&got); err != nil {
t.Fatalf("SELECT: %v", err)
}
if string(got) != "data" {
t.Fatalf("blob contents = %q, want %q", got, "data")
}
}