| 1 |
# 2016 Aug 12 |
| 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 script is using the sqlite_interrupt() API to |
| 13 |
# interrupt WAL checkpoint operations. |
| 14 |
# |
| 15 |
|
| 16 |
set testdir [file dirname $argv0] |
| 17 |
source $testdir/tester.tcl |
| 18 |
source $testdir/wal_common.tcl |
| 19 |
set testprefix interrupt2 |
| 20 |
|
| 21 |
if {[permutation]=="journaltest" || [permutation]=="inmemory_journal"} { |
| 22 |
finish_test |
| 23 |
return |
| 24 |
} |
| 25 |
|
| 26 |
db close |
| 27 |
testvfs tvfs -default 1 |
| 28 |
|
| 29 |
tvfs filter xWrite |
| 30 |
tvfs script write_cb |
| 31 |
|
| 32 |
set ::trigger_interrupt 0 |
| 33 |
set ::dbpointer "" |
| 34 |
proc write_cb {method args} { |
| 35 |
set filename [lindex $args 0] |
| 36 |
if {[file tail $filename]=="test.db" && $::trigger_interrupt} { |
| 37 |
if {$::trigger_interrupt} { |
| 38 |
incr ::trigger_interrupt -1 |
| 39 |
if {$::trigger_interrupt==0} { |
| 40 |
if {$::dbpointer!=""} { |
| 41 |
sqlite3_interrupt $::dbpointer |
| 42 |
} else { |
| 43 |
sqlite3_interrupt db |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
return 0 |
| 49 |
} |
| 50 |
|
| 51 |
sqlite3 db test.db |
| 52 |
do_execsql_test 1.0 { |
| 53 |
CREATE TABLE t1(a, b); |
| 54 |
CREATE INDEX t1a ON t1(a); |
| 55 |
CREATE INDEX t1b ON t1(b); |
| 56 |
PRAGMA journal_mode = wal; |
| 57 |
|
| 58 |
WITH ii(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM ii WHERE i<1000 ) |
| 59 |
INSERT INTO t1 SELECT i, i FROM ii; |
| 60 |
} {wal} |
| 61 |
|
| 62 |
foreach idelay { |
| 63 |
5 |
| 64 |
10 |
| 65 |
15 |
| 66 |
20 |
| 67 |
} { |
| 68 |
|
| 69 |
set ::trigger_interrupt $idelay |
| 70 |
do_catchsql_test 1.$idelay.1 { PRAGMA wal_checkpoint; } {1 interrupted} |
| 71 |
do_execsql_test 1.$idelay.2 { SELECT count(*) FROM t1 } 1000 |
| 72 |
|
| 73 |
set ::trigger_interrupt $idelay |
| 74 |
do_test 1.$idelay.3 { |
| 75 |
list [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] $msg |
| 76 |
} {1 {SQLITE_INTERRUPT - interrupted}} |
| 77 |
do_execsql_test 1.$idelay.4 { SELECT count(*) FROM t1 } 1000 |
| 78 |
} |
| 79 |
|
| 80 |
#------------------------------------------------------------------------- |
| 81 |
# Check that if there are other SQL statements running, a checkpoint does |
| 82 |
# not clear the isInterrupted flag. |
| 83 |
# |
| 84 |
do_execsql_test 2.0 { |
| 85 |
CREATE TEMP TABLE z1(a, b); |
| 86 |
INSERT INTO z1 SELECT * FROM t1; |
| 87 |
} |
| 88 |
|
| 89 |
do_test 2.1 { |
| 90 |
set i 10 |
| 91 |
set res [list [catch { |
| 92 |
set i 10 |
| 93 |
db eval {SELECT * FROM z1} { |
| 94 |
incr i -1 |
| 95 |
if {$i==0} { |
| 96 |
set ::trigger_interrupt 10 |
| 97 |
set cres [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] |
| 98 |
lappend cres $msg |
| 99 |
} |
| 100 |
} |
| 101 |
} msg] $msg] |
| 102 |
|
| 103 |
list $cres $res |
| 104 |
} {{1 {SQLITE_INTERRUPT - interrupted}} {1 interrupted}} |
| 105 |
|
| 106 |
do_execsql_test 2.0 { |
| 107 |
SELECT count(*) FROM t1 |
| 108 |
UNION ALL |
| 109 |
SELECT count(*) FROM z1 |
| 110 |
} {1000 1000} |
| 111 |
|
| 112 |
#------------------------------------------------------------------------- |
| 113 |
# Check the effect of an interrupt during sqlite3_close(). |
| 114 |
# |
| 115 |
db_save_and_close |
| 116 |
|
| 117 |
db_restore_and_reopen |
| 118 |
do_test 3.1.1 { |
| 119 |
set ::trigger_interrupt 10 |
| 120 |
|
| 121 |
# Set dbpointer here so that the write-callback can use the (sqlite3*) |
| 122 |
# pointer with sqlite3_interrupt() directly instead of looking up the |
| 123 |
# command. On at least some versions of Tcl9, looking up a command from |
| 124 |
# within Tcl_DeleteCommand apparently returns a bad pointer. |
| 125 |
# |
| 126 |
set ::dbpointer [sqlite3_connection_pointer db] |
| 127 |
db eval { SELECT * FROM sqlite_master } |
| 128 |
db close |
| 129 |
set ::dbpointer "" |
| 130 |
|
| 131 |
set {} {} |
| 132 |
} {} |
| 133 |
do_test 3.1.2 { |
| 134 |
list [file exists test.db] [file exists test.db-wal] |
| 135 |
} {1 1} |
| 136 |
|
| 137 |
db_restore_and_reopen |
| 138 |
do_test 3.2.1 { |
| 139 |
db eval { SELECT * FROM sqlite_master } |
| 140 |
db close |
| 141 |
set {} {} |
| 142 |
} {} |
| 143 |
do_test 3.2.2 { |
| 144 |
list [file exists test.db] [file exists test.db-wal] |
| 145 |
} {1 0} |
| 146 |
|
| 147 |
#------------------------------------------------------------------------- |
| 148 |
# Check the effect of an interrupt during an automatic checkpoint |
| 149 |
# |
| 150 |
db_restore_and_reopen |
| 151 |
do_test 4.0 { |
| 152 |
execsql { PRAGMA wal_autocheckpoint = 10 } |
| 153 |
set ::trigger_interrupt 10 |
| 154 |
execsql { CREATE TABLE t2(x, y) } |
| 155 |
} {} |
| 156 |
|
| 157 |
# The auto-checkpoint in test 4.0 should have been interrupted. So this |
| 158 |
# db write should cause the wal file to grow. |
| 159 |
do_test 4.1 { |
| 160 |
set nFrame1 [wal_frame_count test.db-wal 1024] |
| 161 |
execsql { CREATE TABLE t3(x, y) } |
| 162 |
set nFrame2 [wal_frame_count test.db-wal 1024] |
| 163 |
expr $nFrame2 > $nFrame1 |
| 164 |
} {1} |
| 165 |
|
| 166 |
# The auto-checkpoint in test 4.0 should not have been interrupted. So |
| 167 |
# this db write should not cause the wal file to grow. |
| 168 |
do_test 4.2 { |
| 169 |
set nFrame1 [wal_frame_count test.db-wal 1024] |
| 170 |
execsql { CREATE TABLE t4(x, y) } |
| 171 |
set nFrame2 [wal_frame_count test.db-wal 1024] |
| 172 |
expr $nFrame2 == $nFrame1 |
| 173 |
} {1} |
| 174 |
|
| 175 |
finish_test |