interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / nolock.test

261 lines · 7940 bytes Raw Blame History
1# 2014-05-07
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# This file implements regression tests for SQLite library. The
13# focus of this file is testing the nolock=1 and immutable=1 query
14# parameters and the SQLITE_IOCAP_IMMUTABLE device characteristic.
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19set testprefix nolock
20
21unset -nocomplain tvfs_calls
22proc tvfs_reset {} {
23 global tvfs_calls
24 array set tvfs_calls {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0}
25}
26proc tvfs_callback {op args} {
27 global tvfs_calls
28 incr tvfs_calls($op)
29 return SQLITE_OK
30}
31tvfs_reset
32
33testvfs tvfs
34tvfs script tvfs_callback
35tvfs filter {xLock xUnlock xCheckReservedLock xAccess}
36
37############################################################################
38# Verify that the nolock=1 query parameter for URI filenames disables all
39# calls to xLock and xUnlock for rollback databases.
40#
41do_test nolock-1.0 {
42 db close
43 forcedelete test.db
44 tvfs_reset
45 sqlite db test.db -vfs tvfs
46 db eval {CREATE TABLE t1(a,b,c); INSERT INTO t1 VALUES(1,2,3);}
47 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
48 xCheckReservedLock $::tvfs_calls(xCheckReservedLock)
49} {xLock 7 xUnlock 5 xCheckReservedLock 0}
50
51do_test nolock-1.1 {
52 db close
53 forcedelete test.db
54 tvfs_reset
55 sqlite db file:test.db?nolock=0 -vfs tvfs -uri 1
56 db eval {CREATE TABLE t1(a,b,c); INSERT INTO t1 VALUES(1,2,3);}
57 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
58 xCheckReservedLock $::tvfs_calls(xCheckReservedLock)
59} {xLock 7 xUnlock 5 xCheckReservedLock 0}
60
61do_test nolock-1.2 {
62 db close
63 forcedelete test.db
64 tvfs_reset
65 sqlite db file:test.db?nolock=1 -vfs tvfs -uri 1
66 db eval {CREATE TABLE t1(a,b,c); INSERT INTO t1 VALUES(1,2,3);}
67 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
68 xCheckReservedLock $::tvfs_calls(xCheckReservedLock)
69} {xLock 0 xUnlock 0 xCheckReservedLock 0}
70
71do_test nolock-1.3 {
72 db close
73 tvfs_reset
74 sqlite db file:test.db?nolock=0 -vfs tvfs -uri 1 -readonly 1
75 db eval {SELECT * FROM t1}
76 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
77 xCheckReservedLock $::tvfs_calls(xCheckReservedLock)
78} {xLock 2 xUnlock 2 xCheckReservedLock 0}
79
80do_test nolock-1.4 {
81 db close
82 tvfs_reset
83 sqlite db file:test.db?nolock=1 -vfs tvfs -uri 1 -readonly 1
84 db eval {SELECT * FROM t1}
85 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
86 xCheckReservedLock $::tvfs_calls(xCheckReservedLock)
87} {xLock 0 xUnlock 0 xCheckReservedLock 0}
88
89#############################################################################
90# Verify that immutable=1 disables both locking and xAccess calls to the
91# journal files.
92#
93do_test nolock-2.0 {
94 db close
95 forcedelete test.db
96 # begin by creating a test database
97 sqlite3 db test.db
98 db eval {
99 CREATE TABLE t1(a,b);
100 INSERT INTO t1 VALUES('hello','world');
101 CREATE TABLE t2(x,y);
102 INSERT INTO t2 VALUES(12345,67890);
103 SELECT * FROM t1, t2;
104 }
105} {hello world 12345 67890}
106do_test nolock-2.1 {
107 tvfs_reset
108 sqlite3 db2 test.db -vfs tvfs
109 db2 eval {SELECT * FROM t1, t2}
110} {hello world 12345 67890}
111do_test nolock-2.2 {
112 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
113 xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \
114 xAccess $::tvfs_calls(xAccess)
115} {xLock 2 xUnlock 2 xCheckReservedLock 0 xAccess 4}
116
117
118do_test nolock-2.11 {
119 db2 close
120 tvfs_reset
121 sqlite3 db2 file:test.db?immutable=0 -vfs tvfs -uri 1
122 db2 eval {SELECT * FROM t1, t2}
123} {hello world 12345 67890}
124do_test nolock-2.12 {
125 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
126 xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \
127 xAccess $::tvfs_calls(xAccess)
128} {xLock 2 xUnlock 2 xCheckReservedLock 0 xAccess 4}
129
130
131do_test nolock-2.21 {
132 db2 close
133 tvfs_reset
134 sqlite3 db2 file:test.db?immutable=1 -vfs tvfs -uri 1
135 db2 eval {SELECT * FROM t1, t2}
136} {hello world 12345 67890}
137do_test nolock-2.22 {
138 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
139 xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \
140 xAccess $::tvfs_calls(xAccess)
141} {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0}
142
143do_test nolock-2.31 {
144 db2 close
145 tvfs_reset
146 sqlite3 db2 file:test.db?immutable=1 -vfs tvfs -uri 1 -readonly 1
147 db2 eval {SELECT * FROM t1, t2}
148} {hello world 12345 67890}
149do_test nolock-2.32 {
150 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
151 xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \
152 xAccess $::tvfs_calls(xAccess)
153} {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0}
154
155############################################################################
156# Verify that the SQLITE_IOCAP_IMMUTABLE flag works
157#
158do_test nolock-3.1 {
159 db2 close
160 tvfs devchar immutable
161 tvfs_reset
162 sqlite3 db2 test.db -vfs tvfs
163 db2 eval {SELECT * FROM t1, t2}
164} {hello world 12345 67890}
165do_test nolock-3.2 {
166 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
167 xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \
168 xAccess $::tvfs_calls(xAccess)
169} {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0}
170
171do_test nolock-3.11 {
172 db2 close
173 tvfs_reset
174 sqlite3 db2 test.db -vfs tvfs -readonly 1
175 db2 eval {SELECT * FROM t1, t2}
176} {hello world 12345 67890}
177do_test nolock-3.12 {
178 list xLock $::tvfs_calls(xLock) xUnlock $::tvfs_calls(xUnlock) \
179 xCheckReservedLock $::tvfs_calls(xCheckReservedLock) \
180 xAccess $::tvfs_calls(xAccess)
181} {xLock 0 xUnlock 0 xCheckReservedLock 0 xAccess 0}
182
183db2 close
184db close
185tvfs delete
186
187if {[permutation]!="inmemory_journal"} {
188 # 2016-03-11: Make sure all works when transitioning to WAL mode
189 # under nolock.
190 #
191 do_test nolock-4.1 {
192 forcedelete test.db
193 sqlite3 db file:test.db?nolock=1 -uri 1
194 db eval {
195 PRAGMA journal_mode=WAL;
196 CREATE TABLE t1(x);
197 INSERT INTO t1 VALUES('youngling');
198 SELECT * FROM t1;
199 }
200 } {delete youngling}
201 db close
202
203 do_test nolock-4.2 {
204 forcedelete test.db
205 sqlite3 db test.db
206 db eval {
207 PRAGMA journal_mode=WAL;
208 CREATE TABLE t1(x);
209 INSERT INTO t1 VALUES('catbird');
210 SELECT * FROM t1;
211 }
212 } {wal catbird}
213 do_test nolock-4.3 {
214 db close
215 sqlite3 db file:test.db?nolock=1 -uri 1
216 set rc [catch {db eval {SELECT * FROM t1}} msg]
217 lappend rc $msg
218 } {1 {unable to open database file}}
219}
220
221if {[permutation] ne "inmemory_journal"} {
222 #-------------------------------------------------------------------------
223 # For these tests, move the pending-byte to later in the file. This way,
224 # all locks are placed past the end of the file, allowing it to be read
225 # at the OS level on windows even when locked.
226 #
227 sqlite3_test_control_pending_byte 0x10000000
228 reset_db
229
230 do_execsql_test 5.0 {
231 PRAGMA cache_size = 100;
232 CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
233 }
234
235 do_execsql_test 5.1 {
236 WITH s(i) AS (
237 VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000
238 )
239 INSERT INTO t1 SELECT NULL, randomblob(100) FROM s;
240 BEGIN;
241 UPDATE t1 SET b=randomblob(100);
242 }
243
244 db_save_and_close
245 db_restore
246
247 # With immutable=1, the hot journal is not rolled back and the db is corrupt.
248 do_test 5.2 {
249 sqlite3 db file:test.db?immutable=1 -uri 1
250 expr {[db one { PRAGMA integrity_check }]=="ok"}
251 } {0}
252
253 # Without immutable=1, the hot journal is correctly rolled back and the db
254 # is not corrupt.
255 do_test 5.3 {
256 sqlite3 db file:test.db -uri 1
257 expr {[db one { PRAGMA integrity_check }]=="ok"}
258 } {1}
259}
260
261finish_test