2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this script is the sqlite_interrupt() API.
17 # $Id: interrupt.test,v 1.4.2.1 2004/05/10 20:27:42 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Compute a checksum on the entire database.
25 proc cksum {{db db}} {
26 set txt [$db eval {SELECT name, type, sql FROM sqlite_master}]\n
27 foreach tbl [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] {
28 append txt [$db eval "SELECT * FROM $tbl"]\n
30 foreach prag {default_synchronous default_cache_size} {
31 append txt $prag-[$db eval "PRAGMA $prag"]\n
33 set cksum [string length $txt]-[md5 $txt]
34 # puts $cksum-[file size test.db]
38 # This routine attempts to execute the sql in $sql. It triggers an
39 # interrupt a progressively later and later points during the processing
40 # and checks to make sure SQLITE_INTERRUPT is returned. Eventually,
41 # the routine completes successfully.
43 proc interrupt_test {testid sql result {initcnt 0} {maxcnt 1000000}} {
46 global sqlite_interrupt_count
49 set sqlite_interrupt_count $i
50 do_test $testid.$i.1 [format {
52 set ::code [db errorcode]
53 expr {$::code==0 || $::code==9}
56 do_test $testid.$i.2 {
59 } elseif {$sqlite_interrupt_count>0} {
60 do_test $testid.$i.99 {
66 set sqlite_interrupt_count 0
69 do_test interrupt-1.1 {
72 SELECT name FROM sqlite_master;
75 interrupt_test interrupt-1.2 {DROP TABLE t1} {} 1 14
76 do_test interrupt-1.3 {
78 SELECT name FROM sqlite_master;
81 integrity_check interrupt-1.4
83 do_test interrrupt-2.1 {
87 INSERT INTO t1 VALUES(1,randstr(300,400));
88 INSERT INTO t1 SELECT a+1, randstr(300,400) FROM t1;
89 INSERT INTO t1 SELECT a+2, a || '-' || b FROM t1;
90 INSERT INTO t1 SELECT a+4, a || '-' || b FROM t1;
91 INSERT INTO t1 SELECT a+8, a || '-' || b FROM t1;
92 INSERT INTO t1 SELECT a+16, a || '-' || b FROM t1;
93 INSERT INTO t1 SELECT a+32, a || '-' || b FROM t1;
95 UPDATE t1 SET b=substr(b,-5,5);
96 SELECT count(*) from t1;
99 set origsize [file size test.db]
100 set cksum [db eval {SELECT md5sum(a || b) FROM t1}]
101 interrupt_test interrupt-2.2 {VACUUM} {} 100
102 do_test interrupt-2.3 {
104 SELECT md5sum(a || b) FROM t1;
107 do_test interrupt-2.4 {
108 expr {$::origsize>[file size test.db]}
110 integrity_check interrupt-2.5
112 # Ticket #594. If an interrupt occurs in the middle of a transaction
113 # and that transaction is later rolled back, the internal schema tables do
116 for {set i 1} {$i<50} {incr i 5} {
117 do_test interrupt-3.$i.1 {
120 CREATE TEMP TABLE t2(x,y);
121 SELECT name FROM sqlite_temp_master;
124 do_test interrupt-3.$i.2 {
125 set ::sqlite_interrupt_count $::i
127 INSERT INTO t2 SELECT * FROM t1;
130 do_test interrupt-3.$i.3 {
132 SELECT name FROM sqlite_temp_master;
135 do_test interrupt-3.$i.4 {
140 do_test interrupt-3.$i.5 {
141 catchsql {SELECT name FROM sqlite_temp_master};
143 SELECT name FROM sqlite_temp_master;
148 # There are reports of a memory leak if an interrupt occurs during
149 # the beginning of a complex query - before the first callback. We
150 # will try to reproduce it here:
153 CREATE TABLE t2(a,b,c);
154 INSERT INTO t2 SELECT round(a/10), randstr(50,80), randstr(50,60) FROM t1;
157 SELECT max(min(b,c)), min(max(b,c)), a FROM t2 GROUP BY a ORDER BY a;
159 set sqlite_interrupt_count 1000000
161 set max_count [expr {1000000-$sqlite_interrupt_count}]
162 for {set i 1} {$i<$max_count-5} {incr i 1} {
163 do_test interrupt-4.$i.1 {
164 set ::sqlite_interrupt_count $::i