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 script is using the sqlite_interrupt() API to
13 # interrupt WAL checkpoint operations.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 source $testdir/wal_common.tcl
19 set testprefix interrupt2
21 if {[permutation]=="journaltest" || [permutation]=="inmemory_journal"} {
27 testvfs tvfs -default 1
32 set ::trigger_interrupt 0
33 proc write_cb {method args} {
34 set filename [lindex $args 0]
35 if {[file tail $filename]=="test.db" && $::trigger_interrupt} {
36 if {$::trigger_interrupt} {
37 incr ::trigger_interrupt -1
38 if {$::trigger_interrupt==0} { sqlite3_interrupt db }
46 CREATE TABLE t1(a, b);
47 CREATE INDEX t1a ON t1(a);
48 CREATE INDEX t1b ON t1(b);
49 PRAGMA journal_mode = wal;
51 WITH ii(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM ii WHERE i<1000 )
52 INSERT INTO t1 SELECT i, i FROM ii;
62 set ::trigger_interrupt $idelay
63 do_catchsql_test 1.$idelay.1 { PRAGMA wal_checkpoint; } {1 interrupted}
64 do_execsql_test 1.$idelay.2 { SELECT count(*) FROM t1 } 1000
66 set ::trigger_interrupt $idelay
68 list [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] $msg
69 } {1 {SQLITE_INTERRUPT - interrupted}}
70 do_execsql_test 1.$idelay.4 { SELECT count(*) FROM t1 } 1000
73 #-------------------------------------------------------------------------
74 # Check that if there are other SQL statements running, a checkpoint does
75 # not clear the isInterrupted flag.
78 CREATE TEMP TABLE z1(a, b);
79 INSERT INTO z1 SELECT * FROM t1;
84 set res [list [catch {
86 db eval {SELECT * FROM z1} {
89 set ::trigger_interrupt 10
90 set cres [catch { sqlite3_wal_checkpoint_v2 db truncate } msg]
97 } {{1 {SQLITE_INTERRUPT - interrupted}} {1 interrupted}}
100 SELECT count(*) FROM t1
102 SELECT count(*) FROM z1
105 #-------------------------------------------------------------------------
106 # Check the effect of an interrupt during sqlite3_close().
110 db_restore_and_reopen
112 set ::trigger_interrupt 10
113 db eval { SELECT * FROM sqlite_master }
118 list [file exists test.db] [file exists test.db-wal]
121 db_restore_and_reopen
123 db eval { SELECT * FROM sqlite_master }
128 list [file exists test.db] [file exists test.db-wal]
131 #-------------------------------------------------------------------------
132 # Check the effect of an interrupt during an automatic checkpoint
134 db_restore_and_reopen
136 execsql { PRAGMA wal_autocheckpoint = 10 }
137 set ::trigger_interrupt 10
138 execsql { CREATE TABLE t2(x, y) }
141 # The auto-checkpoint in test 4.0 should have been interrupted. So this
142 # db write should cause the wal file to grow.
144 set nFrame1 [wal_frame_count test.db-wal 1024]
145 execsql { CREATE TABLE t3(x, y) }
146 set nFrame2 [wal_frame_count test.db-wal 1024]
147 expr $nFrame2 > $nFrame1
150 # The auto-checkpoint in test 4.0 should not have been interrupted. So
151 # this db write should not cause the wal file to grow.
153 set nFrame1 [wal_frame_count test.db-wal 1024]
154 execsql { CREATE TABLE t4(x, y) }
155 set nFrame2 [wal_frame_count test.db-wal 1024]
156 expr $nFrame2 == $nFrame1