sl@0: # 2004 Feb 8 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this script is the sqlite_interrupt() API. sl@0: # sl@0: # $Id: interrupt.test,v 1.16 2008/01/16 17:46:38 drh Exp $ sl@0: sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: set DB [sqlite3_connection_pointer db] sl@0: sl@0: # This routine attempts to execute the sql in $sql. It triggers an sl@0: # interrupt at progressively later and later points during the processing sl@0: # and checks to make sure SQLITE_INTERRUPT is returned. Eventually, sl@0: # the routine completes successfully. sl@0: # sl@0: proc interrupt_test {testid sql result {initcnt 0}} { sl@0: set orig_sum [cksum] sl@0: set i $initcnt sl@0: while 1 { sl@0: incr i sl@0: set ::sqlite_interrupt_count $i sl@0: do_test $testid.$i.1 [format { sl@0: set ::r [catchsql %s] sl@0: set ::code [db errorcode] sl@0: expr {$::code==0 || $::code==9} sl@0: } [list $sql]] 1 sl@0: if {$::code==9} { sl@0: do_test $testid.$i.2 { sl@0: cksum sl@0: } $orig_sum sl@0: } else { sl@0: do_test $testid.$i.99 { sl@0: set ::r sl@0: } [list 0 $result] sl@0: break sl@0: } sl@0: } sl@0: set ::sqlite_interrupt_count 0 sl@0: } sl@0: sl@0: do_test interrupt-1.1 { sl@0: execsql { sl@0: CREATE TABLE t1(a,b); sl@0: SELECT name FROM sqlite_master; sl@0: } sl@0: } {t1} sl@0: interrupt_test interrupt-1.2 {DROP TABLE t1} {} sl@0: do_test interrupt-1.3 { sl@0: execsql { sl@0: SELECT name FROM sqlite_master; sl@0: } sl@0: } {} sl@0: integrity_check interrupt-1.4 sl@0: sl@0: do_test interrrupt-2.1 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t1(a,b); sl@0: INSERT INTO t1 VALUES(1,randstr(300,400)); sl@0: INSERT INTO t1 SELECT a+1, randstr(300,400) FROM t1; sl@0: INSERT INTO t1 SELECT a+2, a || '-' || b FROM t1; sl@0: INSERT INTO t1 SELECT a+4, a || '-' || b FROM t1; sl@0: INSERT INTO t1 SELECT a+8, a || '-' || b FROM t1; sl@0: INSERT INTO t1 SELECT a+16, a || '-' || b FROM t1; sl@0: INSERT INTO t1 SELECT a+32, a || '-' || b FROM t1; sl@0: COMMIT; sl@0: UPDATE t1 SET b=substr(b,-5,5); sl@0: SELECT count(*) from t1; sl@0: } sl@0: } 64 sl@0: set origsize [file size test.db] sl@0: set cksum [db eval {SELECT md5sum(a || b) FROM t1}] sl@0: ifcapable {vacuum} { sl@0: interrupt_test interrupt-2.2 {VACUUM} {} 100 sl@0: } sl@0: do_test interrupt-2.3 { sl@0: execsql { sl@0: SELECT md5sum(a || b) FROM t1; sl@0: } sl@0: } $cksum sl@0: ifcapable {vacuum && !default_autovacuum} { sl@0: do_test interrupt-2.4 { sl@0: expr {$::origsize>[file size test.db]} sl@0: } 1 sl@0: } sl@0: ifcapable {explain} { sl@0: do_test interrupt-2.5 { sl@0: set sql {EXPLAIN SELECT max(a,b), a, b FROM t1} sl@0: execsql $sql sl@0: set rc [catch {db eval $sql {sqlite3_interrupt $DB}} msg] sl@0: lappend rc $msg sl@0: } {1 interrupted} sl@0: } sl@0: integrity_check interrupt-2.6 sl@0: sl@0: # Ticket #594. If an interrupt occurs in the middle of a transaction sl@0: # and that transaction is later rolled back, the internal schema tables do sl@0: # not reset. sl@0: # sl@0: # UPDATE: Interrupting a DML statement in the middle of a transaction now sl@0: # causes the transaction to roll back. Leaving the transaction open after sl@0: # an SQL statement was interrupted halfway through risks database corruption. sl@0: # sl@0: ifcapable tempdb { sl@0: for {set i 1} {$i<50} {incr i 5} { sl@0: do_test interrupt-3.$i.1 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TEMP TABLE t2(x,y); sl@0: SELECT name FROM sqlite_temp_master; sl@0: } sl@0: } {t2} sl@0: do_test interrupt-3.$i.2 { sl@0: set ::sqlite_interrupt_count $::i sl@0: catchsql { sl@0: INSERT INTO t2 SELECT * FROM t1; sl@0: } sl@0: } {1 interrupted} sl@0: do_test interrupt-3.$i.3 { sl@0: execsql { sl@0: SELECT name FROM sqlite_temp_master; sl@0: } sl@0: } {} sl@0: do_test interrupt-3.$i.4 { sl@0: catchsql { sl@0: ROLLBACK sl@0: } sl@0: } {1 {cannot rollback - no transaction is active}} sl@0: do_test interrupt-3.$i.5 { sl@0: catchsql {SELECT name FROM sqlite_temp_master}; sl@0: execsql { sl@0: SELECT name FROM sqlite_temp_master; sl@0: } sl@0: } {} sl@0: } sl@0: } sl@0: sl@0: # There are reports of a memory leak if an interrupt occurs during sl@0: # the beginning of a complex query - before the first callback. We sl@0: # will try to reproduce it here: sl@0: # sl@0: execsql { sl@0: CREATE TABLE t2(a,b,c); sl@0: INSERT INTO t2 SELECT round(a/10), randstr(50,80), randstr(50,60) FROM t1; sl@0: } sl@0: set sql { sl@0: SELECT max(min(b,c)), min(max(b,c)), a FROM t2 GROUP BY a ORDER BY a; sl@0: } sl@0: set sqlite_interrupt_count 1000000 sl@0: execsql $sql sl@0: set max_count [expr {1000000-$sqlite_interrupt_count}] sl@0: for {set i 1} {$i<$max_count-5} {incr i 1} { sl@0: do_test interrupt-4.$i.1 { sl@0: set ::sqlite_interrupt_count $::i sl@0: catchsql $sql sl@0: } {1 interrupted} sl@0: } sl@0: sl@0: # Interrupt during parsing sl@0: # sl@0: do_test interrupt-5.1 { sl@0: proc fake_interrupt {args} { sl@0: db collate fake_collation no-op sl@0: sqlite3_interrupt db sl@0: return SQLITE_OK sl@0: } sl@0: db collation_needed fake_interrupt sl@0: catchsql { sl@0: CREATE INDEX fake ON fake1(a COLLATE fake_collation, b, c DESC); sl@0: } sl@0: } {1 interrupt} sl@0: sl@0: finish_test