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