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