2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2004 Feb 8
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this script is the sqlite_interrupt() API.
2N/A#
2N/A# $Id: interrupt.test,v 1.4.2.1 2004/05/10 20:27:42 drh Exp $
2N/A
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Compute a checksum on the entire database.
2N/A#
2N/Aproc cksum {{db db}} {
2N/A set txt [$db eval {SELECT name, type, sql FROM sqlite_master}]\n
2N/A foreach tbl [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] {
2N/A append txt [$db eval "SELECT * FROM $tbl"]\n
2N/A }
2N/A foreach prag {default_synchronous default_cache_size} {
2N/A append txt $prag-[$db eval "PRAGMA $prag"]\n
2N/A }
2N/A set cksum [string length $txt]-[md5 $txt]
2N/A # puts $cksum-[file size test.db]
2N/A return $cksum
2N/A}
2N/A
2N/A# This routine attempts to execute the sql in $sql. It triggers an
2N/A# interrupt a progressively later and later points during the processing
2N/A# and checks to make sure SQLITE_INTERRUPT is returned. Eventually,
2N/A# the routine completes successfully.
2N/A#
2N/Aproc interrupt_test {testid sql result {initcnt 0} {maxcnt 1000000}} {
2N/A set orig_sum [cksum]
2N/A set i $initcnt
2N/A global sqlite_interrupt_count
2N/A while {$i<$maxcnt} {
2N/A incr i
2N/A set sqlite_interrupt_count $i
2N/A do_test $testid.$i.1 [format {
2N/A set ::r [catchsql %s]
2N/A set ::code [db errorcode]
2N/A expr {$::code==0 || $::code==9}
2N/A } [list $sql]] 1
2N/A if {$::code==9} {
2N/A do_test $testid.$i.2 {
2N/A cksum
2N/A } $orig_sum
2N/A } elseif {$sqlite_interrupt_count>0} {
2N/A do_test $testid.$i.99 {
2N/A set ::r
2N/A } [list 0 $result]
2N/A break
2N/A }
2N/A }
2N/A set sqlite_interrupt_count 0
2N/A}
2N/A
2N/Ado_test interrupt-1.1 {
2N/A execsql {
2N/A CREATE TABLE t1(a,b);
2N/A SELECT name FROM sqlite_master;
2N/A }
2N/A} {t1}
2N/Ainterrupt_test interrupt-1.2 {DROP TABLE t1} {} 1 14
2N/Ado_test interrupt-1.3 {
2N/A execsql {
2N/A SELECT name FROM sqlite_master;
2N/A }
2N/A} {}
2N/Aintegrity_check interrupt-1.4
2N/A
2N/Ado_test interrrupt-2.1 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TABLE t1(a,b);
2N/A INSERT INTO t1 VALUES(1,randstr(300,400));
2N/A INSERT INTO t1 SELECT a+1, randstr(300,400) FROM t1;
2N/A INSERT INTO t1 SELECT a+2, a || '-' || b FROM t1;
2N/A INSERT INTO t1 SELECT a+4, a || '-' || b FROM t1;
2N/A INSERT INTO t1 SELECT a+8, a || '-' || b FROM t1;
2N/A INSERT INTO t1 SELECT a+16, a || '-' || b FROM t1;
2N/A INSERT INTO t1 SELECT a+32, a || '-' || b FROM t1;
2N/A COMMIT;
2N/A UPDATE t1 SET b=substr(b,-5,5);
2N/A SELECT count(*) from t1;
2N/A }
2N/A} 64
2N/Aset origsize [file size test.db]
2N/Aset cksum [db eval {SELECT md5sum(a || b) FROM t1}]
2N/Ainterrupt_test interrupt-2.2 {VACUUM} {} 100
2N/Ado_test interrupt-2.3 {
2N/A execsql {
2N/A SELECT md5sum(a || b) FROM t1;
2N/A }
2N/A} $cksum
2N/Ado_test interrupt-2.4 {
2N/A expr {$::origsize>[file size test.db]}
2N/A} 1
2N/Aintegrity_check interrupt-2.5
2N/A
2N/A# Ticket #594. If an interrupt occurs in the middle of a transaction
2N/A# and that transaction is later rolled back, the internal schema tables do
2N/A# not reset.
2N/A#
2N/Afor {set i 1} {$i<50} {incr i 5} {
2N/A do_test interrupt-3.$i.1 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TEMP TABLE t2(x,y);
2N/A SELECT name FROM sqlite_temp_master;
2N/A }
2N/A } {t2}
2N/A do_test interrupt-3.$i.2 {
2N/A set ::sqlite_interrupt_count $::i
2N/A catchsql {
2N/A INSERT INTO t2 SELECT * FROM t1;
2N/A }
2N/A } {1 interrupted}
2N/A do_test interrupt-3.$i.3 {
2N/A execsql {
2N/A SELECT name FROM sqlite_temp_master;
2N/A }
2N/A } {t2}
2N/A do_test interrupt-3.$i.4 {
2N/A catchsql {
2N/A ROLLBACK
2N/A }
2N/A } {0 {}}
2N/A do_test interrupt-3.$i.5 {
2N/A catchsql {SELECT name FROM sqlite_temp_master};
2N/A execsql {
2N/A SELECT name FROM sqlite_temp_master;
2N/A }
2N/A } {}
2N/A}
2N/A
2N/A# There are reports of a memory leak if an interrupt occurs during
2N/A# the beginning of a complex query - before the first callback. We
2N/A# will try to reproduce it here:
2N/A#
2N/Aexecsql {
2N/A CREATE TABLE t2(a,b,c);
2N/A INSERT INTO t2 SELECT round(a/10), randstr(50,80), randstr(50,60) FROM t1;
2N/A}
2N/Aset sql {
2N/A SELECT max(min(b,c)), min(max(b,c)), a FROM t2 GROUP BY a ORDER BY a;
2N/A}
2N/Aset sqlite_interrupt_count 1000000
2N/Aexecsql $sql
2N/Aset max_count [expr {1000000-$sqlite_interrupt_count}]
2N/Afor {set i 1} {$i<$max_count-5} {incr i 1} {
2N/A do_test interrupt-4.$i.1 {
2N/A set ::sqlite_interrupt_count $::i
2N/A catchsql $sql
2N/A } {1 interrupted}
2N/A}
2N/A
2N/A
2N/Afinish_test