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 / walslow.test

230 lines · 6390 bytes Raw Blame History
1# 2010 March 17
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# This file implements regression tests for SQLite library. The
12# focus of this file is testing the operation of the library in
13# "PRAGMA journal_mode=WAL" mode. The tests in this file use
14# brute force methods, so may take a while to run.
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19source $testdir/wal_common.tcl
20source $testdir/lock_common.tcl
21
22ifcapable !wal {finish_test ; return }
23
24set testprefix walslow
25
26proc reopen_db {} {
27 catch { db close }
28 forcedelete test.db test.db-wal
29 sqlite3 db test.db
30 execsql { PRAGMA journal_mode = wal }
31}
32
33db close
34save_prng_state
35for {set seed 1} {$seed<10} {incr seed} {
36 expr srand($seed)
37 restore_prng_state
38 reopen_db
39 do_test walslow-1.seed=$seed.0 {
40 execsql { CREATE TABLE t1(a, b) }
41 execsql { CREATE INDEX i1 ON t1(a) }
42 execsql { CREATE INDEX i2 ON t1(b) }
43 } {}
44
45 for {set iTest 1} {$iTest < 100} {incr iTest} {
46
47 do_test walslow-1.seed=$seed.$iTest.1 {
48 set w [expr int(rand()*2000)]
49 set x [expr int(rand()*2000)]
50 execsql { INSERT INTO t1 VALUES(randomblob($w), randomblob($x)) }
51 execsql { PRAGMA integrity_check }
52 } {ok}
53
54 do_test walslow-1.seed=$seed.$iTest.2 {
55 execsql "PRAGMA wal_checkpoint;"
56 execsql { PRAGMA integrity_check }
57 } {ok}
58
59 do_test walslow-1.seed=$seed.$iTest.3 {
60 forcedelete testX.db testX.db-wal
61 copy_file test.db testX.db
62 copy_file test.db-wal testX.db-wal
63
64 sqlite3 db2 testX.db
65 execsql { PRAGMA journal_mode = WAL } db2
66 execsql { PRAGMA integrity_check } db2
67 } {ok}
68
69 do_test walslow-1.seed=$seed.$iTest.4 {
70 execsql { SELECT count(*) FROM t1 WHERE a!=b } db2
71 } [execsql { SELECT count(*) FROM t1 WHERE a!=b }]
72 db2 close
73 }
74}
75
76#-------------------------------------------------------------------------
77# Test case walslow-3.* tests that the checksum calculation detects single
78# byte changes to frame or frame-header data and considers the frame
79# invalid as a result.
80#
81reset_db
82do_test 3.1 {
83
84 execsql {
85 PRAGMA synchronous = NORMAL;
86 PRAGMA page_size = 1024;
87 CREATE TABLE t1(a, b);
88 INSERT INTO t1 VALUES(1, randomblob(300));
89 INSERT INTO t1 VALUES(2, randomblob(300));
90 PRAGMA journal_mode = WAL;
91 INSERT INTO t1 VALUES(3, randomblob(300));
92 }
93
94 file size test.db-wal
95} [wal_file_size 1 1024]
96do_test 3.2 {
97 forcecopy test.db-wal test2.db-wal
98 forcecopy test.db test2.db
99 sqlite3 db2 test2.db
100 execsql { SELECT a FROM t1 } db2
101} {1 2 3}
102db2 close
103forcecopy test.db test2.db
104
105foreach incr {1 2 3 20 40 60 80 100 120 140 160 180 200 220 240 253 254 255} {
106 do_test 3.3.$incr {
107 set FAIL 0
108 for {set iOff 0} {$iOff < [wal_file_size 1 1024]} {incr iOff} {
109
110 forcecopy test.db-wal test2.db-wal
111 set fd [open test2.db-wal r+]
112 fconfigure $fd -translation binary
113
114 seek $fd $iOff
115 binary scan [read $fd 1] c x
116 seek $fd $iOff
117 puts -nonewline $fd [binary format c [expr {($x+$incr)&0xFF}]]
118 close $fd
119
120 sqlite3 db2 test2.db
121 if { [execsql { SELECT a FROM t1 } db2] != "1 2" } {set FAIL 1}
122 db2 close
123 }
124 set FAIL
125 } {0}
126}
127
128
129#-------------------------------------------------------------------------
130# Test large log summaries.
131#
132# In this case "large" usually means a log file that requires a wal-index
133# mapping larger than 64KB (the default initial allocation). A 64KB wal-index
134# is large enough for a log file that contains approximately 13100 frames.
135# So the following tests create logs containing at least this many frames.
136#
137# 4.1.*: This test case creates a very large log file within the
138# file-system (around 200MB). The log file does not contain
139# any valid frames. Test that the database file can still be
140# opened and queried, and that the invalid log file causes no
141# problems.
142#
143# 4.2.*: Test that a process may create a large log file and query
144# the database (including the log file that it itself created).
145#
146# 4.3.*: Test that if a very large log file is created, and then a
147# second connection is opened on the database file, it is possible
148# to query the database (and the very large log) using the
149# second connection.
150#
151# 4.4.*: Same test as wal-13.3.*. Except in this case the second
152# connection is opened by an external process.
153#
154set ::blobcnt 0
155proc blob {nByte} {
156 incr ::blobcnt
157 return [string range [string repeat "${::blobcnt}x" $nByte] 1 $nByte]
158}
159
160reset_db
161do_execsql_test 4.1 {
162 PRAGMA journal_mode = wal;
163 CREATE TABLE t1(x, y);
164 INSERT INTO "t1" VALUES('A',0);
165 CREATE TABLE t2(x, y);
166 INSERT INTO "t2" VALUES('B',2);
167} {wal}
168db close
169
170do_test 4.1.1 {
171 list [file exists test.db] [file exists test.db-wal]
172} {1 0}
173do_test 4.1.2 {
174 set fd [open test.db-wal w]
175 seek $fd [expr 200*1024*1024]
176 puts $fd ""
177 close $fd
178 sqlite3 db test.db
179 execsql { SELECT * FROM t2 }
180} {B 2}
181do_test 4.1.3 {
182 db close
183 file exists test.db-wal
184} {0}
185
186do_test 4.2.1 {
187 sqlite3 db test.db
188 execsql { SELECT count(*) FROM t2 }
189} {1}
190do_test 4.2.2 {
191 db function blob blob
192 for {set i 0} {$i < 16} {incr i} {
193 execsql { INSERT INTO t2 SELECT blob(400), blob(400) FROM t2 }
194 }
195 execsql { SELECT count(*) FROM t2 }
196} [expr int(pow(2, 16))]
197do_test 4.2.3 {
198 expr [file size test.db-wal] > [wal_file_size 33000 1024]
199} 1
200
201do_multiclient_test tn {
202 incr tn 2
203
204 do_test 4.$tn.0 {
205 sql1 {
206 PRAGMA journal_mode = WAL;
207 CREATE TABLE t1(x);
208 INSERT INTO t1 SELECT randomblob(800);
209 }
210 sql1 { SELECT count(*) FROM t1 }
211 } {1}
212
213 for {set ii 1} {$ii<16} {incr ii} {
214 do_test 4.$tn.$ii.a {
215 sql2 { INSERT INTO t1 SELECT randomblob(800) FROM t1 }
216 sql2 { SELECT count(*) FROM t1 }
217 } [expr (1<<$ii)]
218 do_test 4.$tn.$ii.b {
219 sql1 { SELECT count(*) FROM t1 }
220 } [expr (1<<$ii)]
221 do_test 4.$tn.$ii.c {
222 sql1 { SELECT count(*) FROM t1 }
223 } [expr (1<<$ii)]
224 do_test 4.$tn.$ii.d {
225 sql1 { PRAGMA integrity_check }
226 } {ok}
227 }
228}
229
230finish_test