Repository navigation
Expand file tree
/
Copy pathrefused_write_test.go
More file actions
164 lines (156 loc) · 5.01 KB
/
Copy pathrefused_write_test.go
File metadata and controls
164 lines (156 loc) · 5.01 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
// Copyright (c) 2026 The liustack/sqlite Authors.
// SPDX-License-Identifier: MIT
// Use of this source code is governed by the MIT license in LICENSE at the
// root of this repository.
package sqlite_test
import (
"context"
"database/sql"
"errors"
"path/filepath"
"sync/atomic"
"testing"
"time"
"github.com/liustack/sqlite"
"github.com/liustack/sqlite/internal/replhook"
)
// writesAtOnce says a write of w goes through now, while something else of
// the test is still open.
func writesAtOnce(t *testing.T, w *sql.DB, what string) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if _, err := w.ExecContext(ctx, "INSERT INTO t VALUES (2)"); err != nil {
t.Errorf("%s: another connection writes: %v", what, err)
}
}
// A write that SQLite refuses before it starts to write leaves the write
// lock of the file free: the connection is in a transaction still, and it
// holds nothing that another writer has to wait for.
func TestRefusedWriteKeepsNoLock(t *testing.T) {
for _, query := range []string{
"?mode=ro",
"?mode=ro&_txlock=deferred",
"?_pragma=query_only(1)",
"?_pragma=query_only(1)&_txlock=deferred",
} {
path := filePath(t, "a.db")
w := openDB(t, "file:"+path)
if _, err := w.Exec("CREATE TABLE t(a)"); err != nil {
t.Fatal(err)
}
r := openDB(t, "file:"+path+query)
tx, err := r.Begin()
if err != nil {
t.Fatalf("%s: %v", query, err)
}
_, err = tx.Exec("INSERT INTO t VALUES (1)")
var e *sqlite.Error
if !errors.As(err, &e) || e.Code() != sqlite.READONLY {
t.Fatalf("%s: a write on a connection that only reads: %v", query, err)
}
writesAtOnce(t, w, query+", after a write that was refused")
// The transaction goes on, and reads: its first read is after
// the other connection's write.
var n int
if err := tx.QueryRow("SELECT count(*) FROM t").Scan(&n); err != nil || n != 1 {
t.Errorf("%s: a read after the write that was refused: %d rows, %v", query, n, err)
}
if err := tx.Rollback(); err != nil {
t.Errorf("%s: %v", query, err)
}
writesAtOnce(t, w, query+", after the transaction")
}
}
// A transaction asked to take the write lock as it starts, on a connection
// that cannot write, takes none: SQLite starts it as a reader.
func TestImmediateOnAReadOnlyConnectionKeepsNoLock(t *testing.T) {
path := filePath(t, "a.db")
w := openDB(t, "file:"+path)
if _, err := w.Exec("CREATE TABLE t(a)"); err != nil {
t.Fatal(err)
}
r := openDB(t, "file:"+path+"?mode=ro&_txlock=immediate")
tx, err := r.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
writesAtOnce(t, w, "beside BEGIN IMMEDIATE of a read-only connection")
}
// A transaction that read, and whose write SQLite refuses because another
// connection has written since (SQLITE_BUSY_SNAPSHOT), cannot write any
// more: it keeps no write lock until it ends.
func TestStaleWriteKeepsNoLock(t *testing.T) {
path := filePath(t, "a.db")
w := openDB(t, "file:"+path)
if _, err := w.Exec("CREATE TABLE t(a)"); err != nil {
t.Fatal(err)
}
d := openDB(t, "file:"+path+"?_txlock=deferred")
tx, err := d.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
var n int
if err := tx.QueryRow("SELECT count(*) FROM t").Scan(&n); err != nil {
t.Fatal(err)
}
if _, err := w.Exec("INSERT INTO t VALUES (1)"); err != nil {
t.Fatal(err)
}
_, err = tx.Exec("INSERT INTO t VALUES (3)")
var e *sqlite.Error
if !errors.As(err, &e) || e.ExtendedCode() != sqlite.BUSY_SNAPSHOT {
t.Fatalf("a write after another connection wrote: %v, want SQLITE_BUSY_SNAPSHOT", err)
}
writesAtOnce(t, w, "after a write that came too late")
}
// A write that was refused is no commit: the replicator of the file is told
// of none, and no checkpoint runs for it, while the connection is in its
// transaction still.
func TestRefusedWriteIsNoCommit(t *testing.T) {
path := filePath(t, "a.db")
setup := openDB(t, "file:"+path)
if _, err := setup.Exec("CREATE TABLE t(a)"); err != nil {
t.Fatal(err)
}
setup.Close()
var calls atomic.Int64
detach, err := replhook.Attach(filepath.FromSlash(path), func(frames, salt uint32, limit int64) { calls.Add(1) })
if err != nil {
t.Fatal(err)
}
defer detach()
w := openDB(t, "file:"+path)
d := openDB(t, "file:"+path+"?_txlock=deferred")
tx, err := d.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
var n int
if err := tx.QueryRow("SELECT count(*) FROM t").Scan(&n); err != nil {
t.Fatal(err)
}
if _, err := w.Exec("INSERT INTO t VALUES (1)"); err != nil {
t.Fatal(err)
}
before := calls.Load()
if before == 0 {
t.Fatal("the commit of a write was not reported")
}
if _, err := tx.Exec("INSERT INTO t VALUES (3)"); err == nil {
t.Fatal("a write after another connection wrote went through")
}
if got := calls.Load(); got != before {
t.Errorf("a write that was refused was reported as %d commits", got-before)
}
if err := tx.Rollback(); err != nil {
t.Fatal(err)
}
if got := calls.Load(); got != before {
t.Errorf("the end of a transaction that wrote nothing was reported as %d commits", got-before)
}
}