-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcntl_test.go
152 lines (127 loc) · 3.71 KB
/
fcntl_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
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
// Copyright 2024 The Sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite // import "modernc.org/sqlite"
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
)
func TestFcntlPersistWAL(t *testing.T) {
t.Run("WAL is cleaned up without persist WAL", func(t *testing.T) {
name := filepath.Join(t.TempDir(), "tmp.db")
walName := name + "-wal"
db, err := sql.Open(driverName, fmt.Sprintf("file:%s", name))
if err != nil {
t.Fatal(err)
}
defer db.Close()
// enable WAL journal
if _, err := db.Exec("pragma journal_mode = WAL"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("create table t(b int)"); err != nil {
t.Fatal(err)
}
// database file must exist after creating a table
if _, err := os.Stat(name); err != nil {
t.Fatal(err)
}
// wal file must exist after creating a table
if _, err := os.Stat(walName); err != nil {
t.Fatal(err)
}
if err := db.Close(); err != nil {
t.Fatal(err)
}
// database file must exist after closing it
if _, err := os.Stat(name); err != nil {
t.Fatal(err)
}
// wal file must NOT exist after closing the db
if _, err := os.Stat(walName); err == nil {
t.Errorf("expected WAL file %s to not exist after closing db", walName)
} else if !errors.Is(err, os.ErrNotExist) {
t.Fatal(err)
}
})
t.Run("WAL is not cleaned up with persist WAL", func(t *testing.T) {
name := filepath.Join(t.TempDir(), "tmp.db")
walName := name + "-wal"
db, err := sql.Open(driverName, fmt.Sprintf("file:%s", name))
if err != nil {
t.Fatal(err)
}
defer db.Close()
conn, err := db.Conn(context.TODO())
if err != nil {
t.Fatal(err)
}
// enable persist WAL for a connection, normally this is done with a hook
err = conn.Raw(func(driverConn any) error {
fc, ok := driverConn.(FileControl)
if !ok {
return fmt.Errorf("driver connection didn't implement FileControl")
}
// query
mode, err := fc.FileControlPersistWAL("main", -1)
if err != nil {
return fmt.Errorf("file control call failed: %w", err)
} else if mode != 0 {
return fmt.Errorf("file control call returned unexpected mode: %d", mode)
}
// enable
mode, err = fc.FileControlPersistWAL("main", 1)
if err != nil {
return fmt.Errorf("file control call failed: %w", err)
} else if mode != 1 {
return fmt.Errorf("file control call returned unexpected mode: %d", mode)
}
// verify
mode, err = fc.FileControlPersistWAL("main", -1)
if err != nil {
return fmt.Errorf("file control call failed: %w", err)
} else if mode != 1 {
return fmt.Errorf("file control call returned unexpected mode: %d", mode)
}
return nil
})
if err != nil {
t.Fatal(err)
}
if _, err := conn.ExecContext(context.TODO(), "pragma journal_mode = WAL"); err != nil {
t.Fatal(err)
}
if _, err := conn.ExecContext(context.TODO(), "create table t(b int)"); err != nil {
t.Fatal(err)
}
// database file must exist after creating a table
if _, err := os.Stat(name); err != nil {
t.Fatal(err)
}
// wal file must exist after creating a table
if _, err := os.Stat(walName); err != nil {
t.Fatal(err)
}
// close connection, should persist WAL
if err := conn.Close(); err != nil {
t.Fatal(err)
}
// close database, should persist WAL
if err := db.Close(); err != nil {
t.Fatal(err)
}
// database file must exist after closing it
if _, err := os.Stat(name); err != nil {
t.Fatal(err)
}
// wal file must exist after closing the db
if _, err := os.Stat(walName); err != nil {
t.Errorf("expected WAL file %s to exist after closing db: %s", walName, err.Error())
}
})
}