Remove a lot of the text describing extended format options from the
[sqlite.git] / test / walprotocol.test
blobb1d9e8c01fcca058892ce62db0420eff101cb6d1
1 # 2016 February 4
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
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.
15 # More specifically, it tests "locking protocol" errors - errors that
16 # may be caused if one or more SQLite clients does not follow the expected
17 # locking protocol when accessing a wal-mode database. These tests take
18 # quite a while to run.
21 set testdir [file dirname $argv0]
22 source $testdir/tester.tcl
23 source $testdir/lock_common.tcl
24 source $testdir/wal_common.tcl
25 ifcapable !wal {finish_test ; return }
27 set testprefix walprotocol
29 #-------------------------------------------------------------------------
30 # When recovering the contents of a WAL file, a process obtains the WRITER
31 # lock, then locks all other bytes before commencing recovery. If it fails
32 # to lock all other bytes (because some other process is holding a read
33 # lock) it should retry up to 100 times. Then return SQLITE_PROTOCOL to the 
34 # caller. Test this (test case 1.3).
36 # Also test the effect of hitting an SQLITE_BUSY while attempting to obtain
37 # the WRITER lock (should be the same). Test case 1.4.
38
39 do_execsql_test 1.0 {
40   PRAGMA journal_mode = wal;
41   CREATE TABLE x(y);
42   INSERT INTO x VALUES('z');
43 } {wal}
45 proc lock_callback {method filename handle lock} {
46   lappend ::locks $lock
48 do_test 1.1 {
49   testvfs T
50   T filter xShmLock 
51   T script lock_callback
52   set ::locks [list]
53   sqlite3 db test.db -vfs T
54   execsql { SELECT * FROM x }
55   lrange $::locks 0 5
56 } [list {0 1 lock exclusive} {1 2 lock exclusive} {4 4 lock exclusive} \
57         {1 2 unlock exclusive} {4 4 unlock exclusive} {0 1 unlock exclusive}  \
59 do_test 1.2 {
60   db close
61   set ::locks [list]
62   sqlite3 db test.db -vfs T
63   execsql { SELECT * FROM x }
64   lrange $::locks 0 5
65 } [list {0 1 lock exclusive} {1 2 lock exclusive} {4 4 lock exclusive} \
66         {1 2 unlock exclusive} {4 4 unlock exclusive} {0 1 unlock exclusive}  \
68 proc lock_callback {method filename handle lock} {
69   if {$lock == "1 2 lock exclusive"} { return SQLITE_BUSY }
70   return SQLITE_OK
72 puts "# Warning: This next test case causes SQLite to call xSleep(1) 100 times."
73 puts "# Normally this equates to a delay of roughly 10 seconds, but if SQLite"
74 puts "# is built on unix without HAVE_USLEEP defined, it may be much longer."
75 do_test 1.3 {
76   db close
77   set ::locks [list]
78   sqlite3 db test.db -vfs T
79   catchsql { SELECT * FROM x }
80 } {1 {locking protocol}}
82 puts "# Warning: Same again!"
83 proc lock_callback {method filename handle lock} {
84   if {$lock == "0 1 lock exclusive"} { return SQLITE_BUSY }
85   return SQLITE_OK
87 do_test 1.4 {
88   db close
89   set ::locks [list]
90   sqlite3 db test.db -vfs T
91   catchsql { SELECT * FROM x }
92 } {1 {locking protocol}}
94 puts "# Warning: Third time!"
95 proc lock_callback {method filename handle lock} {
96   if {$lock == "4 4 lock exclusive"} { return SQLITE_BUSY }
97   return SQLITE_OK
99 do_test 1.5 {
100   db close
101   set ::locks [list]
102   sqlite3 db test.db -vfs T
103   catchsql { SELECT * FROM x }
104 } {1 {locking protocol}}
105 db close
106 T delete
108 #-------------------------------------------------------------------------
110 do_test 2.1 {
111   forcedelete test.db test.db-journal test.db wal
112   sqlite3 db test.db
113   sqlite3 db2 test.db
114   execsql {
115     PRAGMA auto_vacuum = off;
116     PRAGMA journal_mode = WAL;
117     CREATE TABLE b(c);
118     INSERT INTO b VALUES('Tehran');
119     INSERT INTO b VALUES('Qom');
120     INSERT INTO b VALUES('Markazi');
121     PRAGMA wal_checkpoint;
122   }
123 } {wal 0 5 5}
124 do_test 2.2 {
125   execsql { SELECT * FROM b }
126 } {Tehran Qom Markazi}
127 do_test 2.3 {
128   db eval { SELECT * FROM b } {
129     db eval { INSERT INTO b VALUES('Qazvin') }
130     set r [db2 eval { SELECT * FROM b }]
131     break
132   }
133   set r
134 } {Tehran Qom Markazi Qazvin}
135 do_test 2.4 {
136   execsql {
137     INSERT INTO b VALUES('Gilan');
138     INSERT INTO b VALUES('Ardabil');
139   }
140 } {}
141 db2 close
143 faultsim_save_and_close
144 testvfs T -default 1
145 faultsim_restore_and_reopen
146 T filter xShmLock
147 T script lock_callback
149 proc lock_callback {method file handle spec} {
150   if {$spec == "1 2 unlock exclusive"} {
151     T filter {}
152     set ::r [catchsql { SELECT * FROM b } db2]
153   }
155 sqlite3 db test.db
156 sqlite3 db2 test.db
157 puts "# Warning: Another slow test!"
158 do_test 2.5 {
159   execsql { SELECT * FROM b }
160 } {Tehran Qom Markazi Qazvin Gilan Ardabil}
161 do_test 2.6 {
162   set ::r
163 } {1 {locking protocol}}
165 db close
166 db2 close
168 faultsim_restore_and_reopen
169 sqlite3 db2 test.db
170 T filter xShmLock
171 T script lock_callback
172 proc lock_callback {method file handle spec} {
173   if {$spec == "1 2 unlock exclusive"} {
174     T filter {}
175     set ::r [catchsql { SELECT * FROM b } db2]
176   }
178 unset ::r
179 puts "# Warning: Last one!"
180 do_test 2.7 {
181   execsql { SELECT * FROM b }
182 } {Tehran Qom Markazi Qazvin Gilan Ardabil}
183 do_test 2.8 {
184   set ::r
185 } {1 {locking protocol}}
187 db close
188 db2 close
189 T delete
191 finish_test