2005-07-09 06:16:02 +04:00
|
|
|
# 2005 july 8
|
|
|
|
#
|
|
|
|
# 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 test the busy handler
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
2017-10-04 13:39:28 +03:00
|
|
|
set testprefix busy
|
2005-07-09 06:16:02 +04:00
|
|
|
|
|
|
|
do_test busy-1.1 {
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t1(x);
|
|
|
|
INSERT INTO t1 VALUES(1);
|
|
|
|
SELECT * FROM t1
|
|
|
|
}
|
|
|
|
} 1
|
|
|
|
proc busy x {
|
|
|
|
lappend ::busyargs $x
|
|
|
|
if {$x>2} {return 1}
|
|
|
|
return 0
|
|
|
|
}
|
2005-09-17 22:02:36 +04:00
|
|
|
set busyargs {}
|
2005-07-09 06:16:02 +04:00
|
|
|
do_test busy-1.2 {
|
|
|
|
db busy busy
|
2008-03-15 05:09:21 +03:00
|
|
|
db2 eval {BEGIN EXCLUSIVE}
|
|
|
|
catchsql {BEGIN IMMEDIATE}
|
2005-07-09 06:16:02 +04:00
|
|
|
} {1 {database is locked}}
|
|
|
|
do_test busy-1.3 {
|
|
|
|
set busyargs
|
|
|
|
} {0 1 2 3}
|
2008-03-15 05:09:21 +03:00
|
|
|
do_test busy-1.4 {
|
|
|
|
set busyargs {}
|
|
|
|
catchsql {BEGIN IMMEDIATE}
|
|
|
|
set busyargs
|
|
|
|
} {0 1 2 3}
|
|
|
|
|
|
|
|
do_test busy-2.1 {
|
|
|
|
db2 eval {COMMIT}
|
|
|
|
db eval {BEGIN; INSERT INTO t1 VALUES(5)}
|
|
|
|
db2 eval {BEGIN; SELECT * FROM t1}
|
|
|
|
set busyargs {}
|
|
|
|
catchsql COMMIT
|
|
|
|
} {1 {database is locked}}
|
|
|
|
do_test busy-2.2 {
|
|
|
|
set busyargs
|
|
|
|
} {0 1 2 3}
|
|
|
|
|
2005-07-09 06:16:02 +04:00
|
|
|
db2 close
|
|
|
|
|
2017-10-04 13:39:28 +03:00
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
# Test that the busy-handler is invoked correctly for "PRAGMA optimize"
|
|
|
|
# and ANALYZE commnds.
|
2017-10-04 15:08:35 +03:00
|
|
|
ifcapable pragma&&analyze&&!stat4 {
|
2017-10-04 13:39:28 +03:00
|
|
|
|
|
|
|
reset_db
|
|
|
|
|
2017-10-04 15:08:35 +03:00
|
|
|
do_execsql_test 3.1 {
|
2017-10-04 13:39:28 +03:00
|
|
|
CREATE TABLE t1(x);
|
|
|
|
CREATE TABLE t2(y);
|
|
|
|
CREATE TABLE t3(z);
|
|
|
|
|
|
|
|
CREATE INDEX i1 ON t1(x);
|
|
|
|
CREATE INDEX i2 ON t2(y);
|
|
|
|
|
|
|
|
INSERT INTO t1 VALUES(1);
|
|
|
|
INSERT INTO t2 VALUES(1);
|
|
|
|
ANALYZE;
|
|
|
|
|
|
|
|
SELECT * FROM t1 WHERE x=1;
|
|
|
|
SELECT * FROM t2 WHERE y=1;
|
|
|
|
} {1 1}
|
|
|
|
|
2017-10-04 15:08:35 +03:00
|
|
|
do_test 3.2 {
|
2017-10-04 13:39:28 +03:00
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql { BEGIN EXCLUSIVE } db2
|
|
|
|
catchsql { PRAGMA optimize }
|
|
|
|
} {1 {database is locked}}
|
|
|
|
|
|
|
|
proc busy_handler {n} {
|
|
|
|
if {$n>1000} { execsql { COMMIT } db2 }
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
db busy busy_handler
|
|
|
|
|
2017-10-04 15:08:35 +03:00
|
|
|
do_test 3.3 {
|
2017-10-04 13:39:28 +03:00
|
|
|
catchsql { PRAGMA optimize }
|
|
|
|
} {0 {}}
|
|
|
|
|
2017-10-04 15:08:35 +03:00
|
|
|
do_test 3.4 {
|
2017-10-04 13:39:28 +03:00
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT count(*) FROM sqlite_master;
|
|
|
|
} db2
|
|
|
|
} {6}
|
|
|
|
|
|
|
|
proc busy_handler {n} { return 1 }
|
2017-10-04 15:08:35 +03:00
|
|
|
do_test 3.5 {
|
2017-10-04 13:39:28 +03:00
|
|
|
catchsql { PRAGMA optimize }
|
|
|
|
} {0 {}}
|
|
|
|
|
2017-10-04 15:08:35 +03:00
|
|
|
do_test 3.6 {
|
2017-10-04 13:39:28 +03:00
|
|
|
execsql { COMMIT } db2
|
|
|
|
execsql {
|
|
|
|
WITH s(i) AS (
|
|
|
|
SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000
|
|
|
|
)
|
|
|
|
INSERT INTO t1 SELECT i FROM s;
|
|
|
|
}
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT count(*) FROM sqlite_master;
|
|
|
|
} db2
|
|
|
|
} {6}
|
|
|
|
|
2017-10-04 15:08:35 +03:00
|
|
|
do_test 3.7 {
|
2017-10-04 13:39:28 +03:00
|
|
|
catchsql { PRAGMA optimize }
|
|
|
|
} {1 {database is locked}}
|
|
|
|
|
|
|
|
proc busy_handler {n} {
|
|
|
|
if {$n>1000} { execsql { COMMIT } db2 }
|
|
|
|
return 0
|
|
|
|
}
|
2017-10-04 15:08:35 +03:00
|
|
|
do_test 3.8 {
|
2017-10-04 13:39:28 +03:00
|
|
|
catchsql { PRAGMA optimize }
|
|
|
|
} {0 {}}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-07-09 06:16:02 +04:00
|
|
|
finish_test
|