2003-10-18 13:37:26 +04:00
|
|
|
# 2001 September 15
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
# This file implements regression tests for SQLite library. The
|
|
|
|
# focus of this file is testing the 'progress callback'.
|
|
|
|
#
|
2007-06-15 18:53:53 +04:00
|
|
|
# $Id: progress.test,v 1.8 2007/06/15 14:53:53 danielk1977 Exp $
|
2003-10-18 13:37:26 +04:00
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
|
2005-01-20 05:17:01 +03:00
|
|
|
# If the progress callback is not available in this build, skip this
|
|
|
|
# whole file.
|
|
|
|
ifcapable !progress {
|
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2003-10-18 13:37:26 +04:00
|
|
|
# Build some test data
|
|
|
|
#
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE t1(a);
|
|
|
|
INSERT INTO t1 VALUES(1);
|
|
|
|
INSERT INTO t1 VALUES(2);
|
|
|
|
INSERT INTO t1 VALUES(3);
|
|
|
|
INSERT INTO t1 VALUES(4);
|
|
|
|
INSERT INTO t1 VALUES(5);
|
|
|
|
INSERT INTO t1 VALUES(6);
|
|
|
|
INSERT INTO t1 VALUES(7);
|
|
|
|
INSERT INTO t1 VALUES(8);
|
|
|
|
INSERT INTO t1 VALUES(9);
|
|
|
|
INSERT INTO t1 VALUES(10);
|
|
|
|
COMMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Test that the progress callback is invoked.
|
|
|
|
do_test progress-1.0 {
|
|
|
|
set counter 0
|
|
|
|
db progress 1 "[namespace code {incr counter}] ; expr 0"
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM t1
|
|
|
|
}
|
|
|
|
expr $counter > 1
|
|
|
|
} 1
|
2004-06-29 16:39:08 +04:00
|
|
|
do_test progress-1.0.1 {
|
|
|
|
db progress
|
|
|
|
} {::namespace inscope :: {incr counter} ; expr 0}
|
|
|
|
do_test progress-1.0.2 {
|
|
|
|
set v [catch {db progress xyz bogus} msg]
|
|
|
|
lappend v $msg
|
|
|
|
} {1 {expected integer but got "xyz"}}
|
2003-10-18 13:37:26 +04:00
|
|
|
|
|
|
|
# Test that the query is abandoned when the progress callback returns non-zero
|
2004-10-19 01:34:46 +04:00
|
|
|
do_test progress-1.1 {
|
2003-10-18 13:37:26 +04:00
|
|
|
set counter 0
|
|
|
|
db progress 1 "[namespace code {incr counter}] ; expr 1"
|
2004-05-31 12:26:49 +04:00
|
|
|
set rc [catch {execsql {
|
2003-10-18 13:37:26 +04:00
|
|
|
SELECT * FROM t1
|
2004-05-31 12:26:49 +04:00
|
|
|
}}]
|
|
|
|
list $counter $rc
|
|
|
|
} {1 1}
|
2003-10-18 13:37:26 +04:00
|
|
|
|
|
|
|
# Test that the query is rolled back when the progress callback returns
|
|
|
|
# non-zero.
|
2004-10-19 01:34:46 +04:00
|
|
|
do_test progress-1.2 {
|
2003-10-18 13:37:26 +04:00
|
|
|
|
|
|
|
# This figures out how many opcodes it takes to copy 5 extra rows into t1.
|
|
|
|
db progress 1 "[namespace code {incr five_rows}] ; expr 0"
|
|
|
|
set five_rows 0
|
|
|
|
execsql {
|
|
|
|
INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 6
|
|
|
|
}
|
|
|
|
db progress 0 ""
|
|
|
|
execsql {
|
|
|
|
DELETE FROM t1 WHERE a > 10
|
|
|
|
}
|
|
|
|
|
|
|
|
# Now set up the progress callback to abandon the query after the number of
|
|
|
|
# opcodes to copy 5 rows. That way, when we try to copy 6 rows, we know
|
|
|
|
# some data will have been inserted into the table by the time the progress
|
|
|
|
# callback abandons the query.
|
|
|
|
db progress $five_rows "expr 1"
|
2004-05-31 12:26:49 +04:00
|
|
|
catchsql {
|
|
|
|
INSERT INTO t1 SELECT a+10 FROM t1 WHERE a < 9
|
2003-10-18 13:37:26 +04:00
|
|
|
}
|
|
|
|
execsql {
|
|
|
|
SELECT count(*) FROM t1
|
|
|
|
}
|
|
|
|
} 10
|
|
|
|
|
2007-06-13 20:49:48 +04:00
|
|
|
# Test that an active transaction remains active and not rolled back
|
|
|
|
# after the progress query abandons a query.
|
|
|
|
#
|
|
|
|
# UPDATE: It is now recognised that this is a sure route to database
|
|
|
|
# corruption. So the transaction is rolled back.
|
2004-10-19 01:34:46 +04:00
|
|
|
do_test progress-1.3 {
|
2003-10-18 13:37:26 +04:00
|
|
|
|
|
|
|
db progress 0 ""
|
|
|
|
execsql BEGIN
|
|
|
|
execsql {
|
|
|
|
INSERT INTO t1 VALUES(11)
|
|
|
|
}
|
|
|
|
db progress 1 "expr 1"
|
2004-05-31 12:26:49 +04:00
|
|
|
catchsql {
|
2003-10-18 13:37:26 +04:00
|
|
|
INSERT INTO t1 VALUES(12)
|
|
|
|
}
|
|
|
|
db progress 0 ""
|
2007-06-13 20:49:48 +04:00
|
|
|
catchsql COMMIT
|
|
|
|
} {1 {cannot commit - no transaction is active}}
|
|
|
|
do_test progress-1.3.1 {
|
2003-10-18 13:37:26 +04:00
|
|
|
execsql {
|
|
|
|
SELECT count(*) FROM t1
|
|
|
|
}
|
2007-06-13 20:49:48 +04:00
|
|
|
} 10
|
2003-10-18 13:37:26 +04:00
|
|
|
|
|
|
|
# Check that a value of 0 for N means no progress callback
|
2004-10-19 01:34:46 +04:00
|
|
|
do_test progress-1.4 {
|
2003-10-18 13:37:26 +04:00
|
|
|
set counter 0
|
|
|
|
db progress 0 "[namespace code {incr counter}] ; expr 0"
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM t1;
|
|
|
|
}
|
|
|
|
set counter
|
|
|
|
} 0
|
|
|
|
|
|
|
|
db progress 0 ""
|
|
|
|
|
2006-05-26 23:57:19 +04:00
|
|
|
# Make sure other queries can be run from within the progress
|
|
|
|
# handler. Ticket #1827
|
|
|
|
#
|
|
|
|
do_test progress-1.5 {
|
|
|
|
set rx 0
|
|
|
|
proc set_rx {args} {
|
|
|
|
db progress 0 {}
|
|
|
|
set ::rx [db eval {SELECT count(*) FROM t1}]
|
|
|
|
return [expr 0]
|
|
|
|
}
|
|
|
|
db progress 10 set_rx
|
|
|
|
db eval {
|
|
|
|
SELECT sum(a) FROM t1
|
|
|
|
}
|
2007-06-13 20:49:48 +04:00
|
|
|
} {55}
|
2006-05-26 23:57:19 +04:00
|
|
|
do_test progress-1.6 {
|
|
|
|
set ::rx
|
2007-06-13 20:49:48 +04:00
|
|
|
} {10}
|
2006-05-26 23:57:19 +04:00
|
|
|
|
2007-06-15 18:53:53 +04:00
|
|
|
# Check that abandoning a query using the progress handler does
|
|
|
|
# not cause other queries to abort. Ticket #2415.
|
|
|
|
do_test progress-1.7 {
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE abc(a, b, c);
|
|
|
|
INSERT INTO abc VALUES(1, 2, 3);
|
|
|
|
INSERT INTO abc VALUES(4, 5, 6);
|
|
|
|
INSERT INTO abc VALUES(7, 8, 9);
|
|
|
|
}
|
|
|
|
|
|
|
|
set ::res [list]
|
|
|
|
db eval {SELECT a, b, c FROM abc} {
|
|
|
|
lappend ::res $a $b $c
|
2013-07-10 07:05:14 +04:00
|
|
|
db progress 5 "expr 1"
|
2007-06-15 18:53:53 +04:00
|
|
|
catch {db eval {SELECT a, b, c FROM abc} { }} msg
|
2013-08-21 11:25:03 +04:00
|
|
|
db progress 5 "expr 0"
|
2007-06-15 18:53:53 +04:00
|
|
|
lappend ::res $msg
|
|
|
|
}
|
|
|
|
|
|
|
|
set ::res
|
|
|
|
} {1 2 3 interrupted 4 5 6 interrupted 7 8 9 interrupted}
|
|
|
|
|
2003-10-18 13:37:26 +04:00
|
|
|
finish_test
|