xref: /illumos-gate/usr/src/lib/libsqlite/test/interrupt.test (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*1da57d55SToomas Soome#
2c5c4113dSnw141292# 2004 Feb 8
3c5c4113dSnw141292#
4c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
5c5c4113dSnw141292# a legal notice, here is a blessing:
6c5c4113dSnw141292#
7c5c4113dSnw141292#    May you do good and not evil.
8c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
9c5c4113dSnw141292#    May you share freely, never taking more than you give.
10c5c4113dSnw141292#
11c5c4113dSnw141292#***********************************************************************
12c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
13c5c4113dSnw141292# focus of this script is the sqlite_interrupt() API.
14c5c4113dSnw141292#
15c5c4113dSnw141292# $Id: interrupt.test,v 1.4.2.1 2004/05/10 20:27:42 drh Exp $
16c5c4113dSnw141292
17c5c4113dSnw141292
18c5c4113dSnw141292set testdir [file dirname $argv0]
19c5c4113dSnw141292source $testdir/tester.tcl
20c5c4113dSnw141292
21c5c4113dSnw141292# Compute a checksum on the entire database.
22c5c4113dSnw141292#
23c5c4113dSnw141292proc cksum {{db db}} {
24c5c4113dSnw141292  set txt [$db eval {SELECT name, type, sql FROM sqlite_master}]\n
25c5c4113dSnw141292  foreach tbl [$db eval {SELECT name FROM sqlite_master WHERE type='table'}] {
26c5c4113dSnw141292    append txt [$db eval "SELECT * FROM $tbl"]\n
27c5c4113dSnw141292  }
28c5c4113dSnw141292  foreach prag {default_synchronous default_cache_size} {
29c5c4113dSnw141292    append txt $prag-[$db eval "PRAGMA $prag"]\n
30c5c4113dSnw141292  }
31c5c4113dSnw141292  set cksum [string length $txt]-[md5 $txt]
32c5c4113dSnw141292  # puts $cksum-[file size test.db]
33c5c4113dSnw141292  return $cksum
34c5c4113dSnw141292}
35c5c4113dSnw141292
36c5c4113dSnw141292# This routine attempts to execute the sql in $sql.  It triggers an
37c5c4113dSnw141292# interrupt a progressively later and later points during the processing
38c5c4113dSnw141292# and checks to make sure SQLITE_INTERRUPT is returned.  Eventually,
39c5c4113dSnw141292# the routine completes successfully.
40c5c4113dSnw141292#
41c5c4113dSnw141292proc interrupt_test {testid sql result {initcnt 0} {maxcnt 1000000}} {
42c5c4113dSnw141292  set orig_sum [cksum]
43c5c4113dSnw141292  set i $initcnt
44c5c4113dSnw141292  global sqlite_interrupt_count
45c5c4113dSnw141292  while {$i<$maxcnt} {
46c5c4113dSnw141292    incr i
47c5c4113dSnw141292    set sqlite_interrupt_count $i
48c5c4113dSnw141292    do_test $testid.$i.1 [format {
49c5c4113dSnw141292      set ::r [catchsql %s]
50c5c4113dSnw141292      set ::code [db errorcode]
51c5c4113dSnw141292      expr {$::code==0 || $::code==9}
52c5c4113dSnw141292    } [list $sql]] 1
53c5c4113dSnw141292    if {$::code==9} {
54c5c4113dSnw141292      do_test $testid.$i.2 {
55c5c4113dSnw141292        cksum
56c5c4113dSnw141292      } $orig_sum
57c5c4113dSnw141292    } elseif {$sqlite_interrupt_count>0} {
58c5c4113dSnw141292      do_test $testid.$i.99 {
59c5c4113dSnw141292        set ::r
60c5c4113dSnw141292      } [list 0 $result]
61c5c4113dSnw141292      break
62c5c4113dSnw141292    }
63c5c4113dSnw141292  }
64c5c4113dSnw141292  set sqlite_interrupt_count 0
65c5c4113dSnw141292}
66c5c4113dSnw141292
67c5c4113dSnw141292do_test interrupt-1.1 {
68c5c4113dSnw141292  execsql {
69c5c4113dSnw141292    CREATE TABLE t1(a,b);
70c5c4113dSnw141292    SELECT name FROM sqlite_master;
71c5c4113dSnw141292  }
72c5c4113dSnw141292} {t1}
73c5c4113dSnw141292interrupt_test interrupt-1.2 {DROP TABLE t1} {} 1 14
74c5c4113dSnw141292do_test interrupt-1.3 {
75c5c4113dSnw141292  execsql {
76c5c4113dSnw141292    SELECT name FROM sqlite_master;
77c5c4113dSnw141292  }
78c5c4113dSnw141292} {}
79c5c4113dSnw141292integrity_check interrupt-1.4
80c5c4113dSnw141292
81c5c4113dSnw141292do_test interrrupt-2.1 {
82c5c4113dSnw141292  execsql {
83c5c4113dSnw141292    BEGIN;
84c5c4113dSnw141292    CREATE TABLE t1(a,b);
85c5c4113dSnw141292    INSERT INTO t1 VALUES(1,randstr(300,400));
86c5c4113dSnw141292    INSERT INTO t1 SELECT a+1, randstr(300,400) FROM t1;
87c5c4113dSnw141292    INSERT INTO t1 SELECT a+2, a || '-' || b FROM t1;
88c5c4113dSnw141292    INSERT INTO t1 SELECT a+4, a || '-' || b FROM t1;
89c5c4113dSnw141292    INSERT INTO t1 SELECT a+8, a || '-' || b FROM t1;
90c5c4113dSnw141292    INSERT INTO t1 SELECT a+16, a || '-' || b FROM t1;
91c5c4113dSnw141292    INSERT INTO t1 SELECT a+32, a || '-' || b FROM t1;
92c5c4113dSnw141292    COMMIT;
93c5c4113dSnw141292    UPDATE t1 SET b=substr(b,-5,5);
94c5c4113dSnw141292    SELECT count(*) from t1;
95c5c4113dSnw141292  }
96c5c4113dSnw141292} 64
97c5c4113dSnw141292set origsize [file size test.db]
98c5c4113dSnw141292set cksum [db eval {SELECT md5sum(a || b) FROM t1}]
99c5c4113dSnw141292interrupt_test interrupt-2.2 {VACUUM} {} 100
100c5c4113dSnw141292do_test interrupt-2.3 {
101c5c4113dSnw141292  execsql {
102c5c4113dSnw141292    SELECT md5sum(a || b) FROM t1;
103c5c4113dSnw141292  }
104c5c4113dSnw141292} $cksum
105c5c4113dSnw141292do_test interrupt-2.4 {
106c5c4113dSnw141292  expr {$::origsize>[file size test.db]}
107c5c4113dSnw141292} 1
108c5c4113dSnw141292integrity_check interrupt-2.5
109c5c4113dSnw141292
110c5c4113dSnw141292# Ticket #594.  If an interrupt occurs in the middle of a transaction
111c5c4113dSnw141292# and that transaction is later rolled back, the internal schema tables do
112c5c4113dSnw141292# not reset.
113c5c4113dSnw141292#
114c5c4113dSnw141292for {set i 1} {$i<50} {incr i 5} {
115c5c4113dSnw141292  do_test interrupt-3.$i.1 {
116c5c4113dSnw141292    execsql {
117c5c4113dSnw141292      BEGIN;
118c5c4113dSnw141292      CREATE TEMP TABLE t2(x,y);
119c5c4113dSnw141292      SELECT name FROM sqlite_temp_master;
120c5c4113dSnw141292    }
121c5c4113dSnw141292  } {t2}
122c5c4113dSnw141292  do_test interrupt-3.$i.2 {
123c5c4113dSnw141292    set ::sqlite_interrupt_count $::i
124c5c4113dSnw141292    catchsql {
125c5c4113dSnw141292      INSERT INTO t2 SELECT * FROM t1;
126c5c4113dSnw141292    }
127c5c4113dSnw141292  } {1 interrupted}
128c5c4113dSnw141292  do_test interrupt-3.$i.3 {
129c5c4113dSnw141292    execsql {
130c5c4113dSnw141292      SELECT name FROM sqlite_temp_master;
131c5c4113dSnw141292    }
132c5c4113dSnw141292  } {t2}
133c5c4113dSnw141292  do_test interrupt-3.$i.4 {
134c5c4113dSnw141292    catchsql {
135c5c4113dSnw141292      ROLLBACK
136c5c4113dSnw141292    }
137c5c4113dSnw141292  } {0 {}}
138c5c4113dSnw141292  do_test interrupt-3.$i.5 {
139c5c4113dSnw141292    catchsql {SELECT name FROM sqlite_temp_master};
140c5c4113dSnw141292    execsql {
141c5c4113dSnw141292      SELECT name FROM sqlite_temp_master;
142c5c4113dSnw141292    }
143c5c4113dSnw141292  } {}
144c5c4113dSnw141292}
145c5c4113dSnw141292
146c5c4113dSnw141292# There are reports of a memory leak if an interrupt occurs during
147c5c4113dSnw141292# the beginning of a complex query - before the first callback.  We
148c5c4113dSnw141292# will try to reproduce it here:
149c5c4113dSnw141292#
150c5c4113dSnw141292execsql {
151c5c4113dSnw141292  CREATE TABLE t2(a,b,c);
152c5c4113dSnw141292  INSERT INTO t2 SELECT round(a/10), randstr(50,80), randstr(50,60) FROM t1;
153c5c4113dSnw141292}
154c5c4113dSnw141292set sql {
155c5c4113dSnw141292  SELECT max(min(b,c)), min(max(b,c)), a FROM t2 GROUP BY a ORDER BY a;
156c5c4113dSnw141292}
157c5c4113dSnw141292set sqlite_interrupt_count 1000000
158c5c4113dSnw141292execsql $sql
159c5c4113dSnw141292set max_count [expr {1000000-$sqlite_interrupt_count}]
160c5c4113dSnw141292for {set i 1} {$i<$max_count-5} {incr i 1} {
161c5c4113dSnw141292  do_test interrupt-4.$i.1 {
162c5c4113dSnw141292    set ::sqlite_interrupt_count $::i
163c5c4113dSnw141292    catchsql $sql
164c5c4113dSnw141292  } {1 interrupted}
165c5c4113dSnw141292}
166c5c4113dSnw141292
167c5c4113dSnw141292
168c5c4113dSnw141292finish_test
169