xref: /sqlite-3.40.0/test/interrupt.test (revision 5436dc2d)
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#
14# $Id: interrupt.test,v 1.8 2004/11/14 04:04:18 drh Exp $
15
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}]
97ifcapable {vacuum} {
98  interrupt_test interrupt-2.2 {VACUUM} {} 100
99}
100do_test interrupt-2.3 {
101  execsql {
102    SELECT md5sum(a || b) FROM t1;
103  }
104} $cksum
105ifcapable {vacuum && !default_autovacuum} {
106  do_test interrupt-2.4 {
107    expr {$::origsize>[file size test.db]}
108  } 1
109}
110integrity_check interrupt-2.5
111
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
114# not reset.
115#
116for {set i 1} {$i<50} {incr i 5} {
117  do_test interrupt-3.$i.1 {
118    execsql {
119      BEGIN;
120      CREATE TEMP TABLE t2(x,y);
121      SELECT name FROM sqlite_temp_master;
122    }
123  } {t2}
124  do_test interrupt-3.$i.2 {
125    set ::sqlite_interrupt_count $::i
126    catchsql {
127      INSERT INTO t2 SELECT * FROM t1;
128    }
129  } {1 interrupted}
130  do_test interrupt-3.$i.3 {
131    execsql {
132      SELECT name FROM sqlite_temp_master;
133    }
134  } {t2}
135  do_test interrupt-3.$i.4 {
136    catchsql {
137      ROLLBACK
138    }
139  } {0 {}}
140  do_test interrupt-3.$i.5 {
141    catchsql {SELECT name FROM sqlite_temp_master};
142    execsql {
143      SELECT name FROM sqlite_temp_master;
144    }
145  } {}
146}
147
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:
151#
152execsql {
153  CREATE TABLE t2(a,b,c);
154  INSERT INTO t2 SELECT round(a/10), randstr(50,80), randstr(50,60) FROM t1;
155}
156set sql {
157  SELECT max(min(b,c)), min(max(b,c)), a FROM t2 GROUP BY a ORDER BY a;
158}
159set sqlite_interrupt_count 1000000
160execsql $sql
161set max_count [expr {1000000-$sqlite_interrupt_count}]
162for {set i 1} {$i<$max_count-5} {incr i 1} {
163  do_test interrupt-4.$i.1 {
164    set ::sqlite_interrupt_count $::i
165    catchsql $sql
166  } {1 interrupted}
167}
168
169finish_test
170