blob: 3860ea897de54bc14bc31b731d49e3705c233aa1 [file] [log] [blame]
drh93581642004-02-12 13:02:55 +00001# 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#
drh27d258a2004-10-30 20:23:09 +000014# $Id: interrupt.test,v 1.5 2004/10/30 20:23:10 drh Exp $
drh93581642004-02-12 13:02:55 +000015
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
drh27d258a2004-10-30 20:23:09 +0000103ifcapable vacuum {
104 do_test interrupt-2.4 {
105 expr {$::origsize>[file size test.db]}
106 } 1
107}
drh93581642004-02-12 13:02:55 +0000108integrity_check interrupt-2.5
109
drh8ef83ff2004-02-12 15:31:21 +0000110# 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}
drh93581642004-02-12 13:02:55 +0000145
drh23068022004-02-18 01:31:53 +0000146# 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 {
drh9cbe7ca2004-02-18 16:57:23 +0000151 CREATE TABLE t2(a,b,c);
152 INSERT INTO t2 SELECT round(a/10), randstr(50,80), randstr(50,60) FROM t1;
drh23068022004-02-18 01:31:53 +0000153}
154set sql {
drh9cbe7ca2004-02-18 16:57:23 +0000155 SELECT max(min(b,c)), min(max(b,c)), a FROM t2 GROUP BY a ORDER BY a;
drh23068022004-02-18 01:31:53 +0000156}
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
drh9cbe7ca2004-02-18 16:57:23 +0000163 catchsql $sql
drh23068022004-02-18 01:31:53 +0000164 } {1 interrupted}
165}
166
drh93581642004-02-12 13:02:55 +0000167finish_test