1# 2# 2004 Feb 8 3# 4# The author disclaims copyright to this source code. In place of 5# a legal notice, here is a blessing: 6# 7# May you do good and not evil. 8# May you find forgiveness for yourself and forgive others. 9# May you share freely, never taking more than you give. 10# 11#*********************************************************************** 12# This file implements regression tests for SQLite library. The 13# focus of this script is the sqlite_interrupt() API. 14# 15# $Id: interrupt.test,v 1.4.2.1 2004/05/10 20:27:42 drh Exp $ 16 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21# Compute a checksum on the entire database. 22# 23proc cksum {{db db}} { 24 set txt [$db eval {SELECT name, type, sql FROM sqlite_master}]\n 25 foreach tbl [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] { 26 append txt [$db eval "SELECT * FROM $tbl"]\n 27 } 28 foreach prag {default_synchronous default_cache_size} { 29 append txt $prag-[$db eval "PRAGMA $prag"]\n 30 } 31 set cksum [string length $txt]-[md5 $txt] 32 # puts $cksum-[file size test.db] 33 return $cksum 34} 35 36# This routine attempts to execute the sql in $sql. It triggers an 37# interrupt a progressively later and later points during the processing 38# and checks to make sure SQLITE_INTERRUPT is returned. Eventually, 39# the routine completes successfully. 40# 41proc interrupt_test {testid sql result {initcnt 0} {maxcnt 1000000}} { 42 set orig_sum [cksum] 43 set i $initcnt 44 global sqlite_interrupt_count 45 while {$i<$maxcnt} { 46 incr i 47 set sqlite_interrupt_count $i 48 do_test $testid.$i.1 [format { 49 set ::r [catchsql %s] 50 set ::code [db errorcode] 51 expr {$::code==0 || $::code==9} 52 } [list $sql]] 1 53 if {$::code==9} { 54 do_test $testid.$i.2 { 55 cksum 56 } $orig_sum 57 } elseif {$sqlite_interrupt_count>0} { 58 do_test $testid.$i.99 { 59 set ::r 60 } [list 0 $result] 61 break 62 } 63 } 64 set sqlite_interrupt_count 0 65} 66 67do_test interrupt-1.1 { 68 execsql { 69 CREATE TABLE t1(a,b); 70 SELECT name FROM sqlite_master; 71 } 72} {t1} 73interrupt_test interrupt-1.2 {DROP TABLE t1} {} 1 14 74do_test interrupt-1.3 { 75 execsql { 76 SELECT name FROM sqlite_master; 77 } 78} {} 79integrity_check interrupt-1.4 80 81do_test interrrupt-2.1 { 82 execsql { 83 BEGIN; 84 CREATE TABLE t1(a,b); 85 INSERT INTO t1 VALUES(1,randstr(300,400)); 86 INSERT INTO t1 SELECT a+1, randstr(300,400) FROM t1; 87 INSERT INTO t1 SELECT a+2, a || '-' || b FROM t1; 88 INSERT INTO t1 SELECT a+4, a || '-' || b FROM t1; 89 INSERT INTO t1 SELECT a+8, a || '-' || b FROM t1; 90 INSERT INTO t1 SELECT a+16, a || '-' || b FROM t1; 91 INSERT INTO t1 SELECT a+32, a || '-' || b FROM t1; 92 COMMIT; 93 UPDATE t1 SET b=substr(b,-5,5); 94 SELECT count(*) from t1; 95 } 96} 64 97set origsize [file size test.db] 98set cksum [db eval {SELECT md5sum(a || b) FROM t1}] 99interrupt_test interrupt-2.2 {VACUUM} {} 100 100do_test interrupt-2.3 { 101 execsql { 102 SELECT md5sum(a || b) FROM t1; 103 } 104} $cksum 105do_test interrupt-2.4 { 106 expr {$::origsize>[file size test.db]} 107} 1 108integrity_check interrupt-2.5 109 110# Ticket #594. If an interrupt occurs in the middle of a transaction 111# and that transaction is later rolled back, the internal schema tables do 112# not reset. 113# 114for {set i 1} {$i<50} {incr i 5} { 115 do_test interrupt-3.$i.1 { 116 execsql { 117 BEGIN; 118 CREATE TEMP TABLE t2(x,y); 119 SELECT name FROM sqlite_temp_master; 120 } 121 } {t2} 122 do_test interrupt-3.$i.2 { 123 set ::sqlite_interrupt_count $::i 124 catchsql { 125 INSERT INTO t2 SELECT * FROM t1; 126 } 127 } {1 interrupted} 128 do_test interrupt-3.$i.3 { 129 execsql { 130 SELECT name FROM sqlite_temp_master; 131 } 132 } {t2} 133 do_test interrupt-3.$i.4 { 134 catchsql { 135 ROLLBACK 136 } 137 } {0 {}} 138 do_test interrupt-3.$i.5 { 139 catchsql {SELECT name FROM sqlite_temp_master}; 140 execsql { 141 SELECT name FROM sqlite_temp_master; 142 } 143 } {} 144} 145 146# There are reports of a memory leak if an interrupt occurs during 147# the beginning of a complex query - before the first callback. We 148# will try to reproduce it here: 149# 150execsql { 151 CREATE TABLE t2(a,b,c); 152 INSERT INTO t2 SELECT round(a/10), randstr(50,80), randstr(50,60) FROM t1; 153} 154set sql { 155 SELECT max(min(b,c)), min(max(b,c)), a FROM t2 GROUP BY a ORDER BY a; 156} 157set sqlite_interrupt_count 1000000 158execsql $sql 159set max_count [expr {1000000-$sqlite_interrupt_count}] 160for {set i 1} {$i<$max_count-5} {incr i 1} { 161 do_test interrupt-4.$i.1 { 162 set ::sqlite_interrupt_count $::i 163 catchsql $sql 164 } {1 interrupted} 165} 166 167 168finish_test 169