blob: be0515b013072ec3b50a2f99bfd7097ef623dab4 [file] [log] [blame]
drha4afb652005-07-09 02:16:02 +00001# 2005 july 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 test the busy handler
12#
drha4afb652005-07-09 02:16:02 +000013
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
dan61b513e2017-10-04 10:39:28 +000017set testprefix busy
drha4afb652005-07-09 02:16:02 +000018
19do_test busy-1.1 {
20 sqlite3 db2 test.db
21 execsql {
22 CREATE TABLE t1(x);
23 INSERT INTO t1 VALUES(1);
24 SELECT * FROM t1
25 }
26} 1
27proc busy x {
28 lappend ::busyargs $x
29 if {$x>2} {return 1}
30 return 0
31}
drhe3000ae2005-09-17 18:02:36 +000032set busyargs {}
drha4afb652005-07-09 02:16:02 +000033do_test busy-1.2 {
34 db busy busy
drh175cd712008-03-15 02:09:21 +000035 db2 eval {BEGIN EXCLUSIVE}
36 catchsql {BEGIN IMMEDIATE}
drha4afb652005-07-09 02:16:02 +000037} {1 {database is locked}}
38do_test busy-1.3 {
39 set busyargs
40} {0 1 2 3}
drh175cd712008-03-15 02:09:21 +000041do_test busy-1.4 {
42 set busyargs {}
43 catchsql {BEGIN IMMEDIATE}
44 set busyargs
45} {0 1 2 3}
46
47do_test busy-2.1 {
48 db2 eval {COMMIT}
49 db eval {BEGIN; INSERT INTO t1 VALUES(5)}
50 db2 eval {BEGIN; SELECT * FROM t1}
51 set busyargs {}
52 catchsql COMMIT
53} {1 {database is locked}}
54do_test busy-2.2 {
55 set busyargs
56} {0 1 2 3}
57
drha4afb652005-07-09 02:16:02 +000058db2 close
59
dan61b513e2017-10-04 10:39:28 +000060#-------------------------------------------------------------------------
61# Test that the busy-handler is invoked correctly for "PRAGMA optimize"
62# and ANALYZE commnds.
dan4c167602017-10-04 12:08:35 +000063ifcapable pragma&&analyze&&!stat4 {
dan61b513e2017-10-04 10:39:28 +000064
65reset_db
66
dan4c167602017-10-04 12:08:35 +000067do_execsql_test 3.1 {
dan61b513e2017-10-04 10:39:28 +000068 CREATE TABLE t1(x);
69 CREATE TABLE t2(y);
70 CREATE TABLE t3(z);
71
72 CREATE INDEX i1 ON t1(x);
73 CREATE INDEX i2 ON t2(y);
74
75 INSERT INTO t1 VALUES(1);
76 INSERT INTO t2 VALUES(1);
77 ANALYZE;
78
79 SELECT * FROM t1 WHERE x=1;
80 SELECT * FROM t2 WHERE y=1;
81} {1 1}
82
dan4c167602017-10-04 12:08:35 +000083do_test 3.2 {
dan61b513e2017-10-04 10:39:28 +000084 sqlite3 db2 test.db
85 execsql { BEGIN EXCLUSIVE } db2
86 catchsql { PRAGMA optimize }
87} {1 {database is locked}}
88
89proc busy_handler {n} {
90 if {$n>1000} { execsql { COMMIT } db2 }
91 return 0
92}
93db busy busy_handler
94
dan4c167602017-10-04 12:08:35 +000095do_test 3.3 {
dan61b513e2017-10-04 10:39:28 +000096 catchsql { PRAGMA optimize }
97} {0 {}}
98
dan4c167602017-10-04 12:08:35 +000099do_test 3.4 {
dan61b513e2017-10-04 10:39:28 +0000100 execsql {
101 BEGIN;
102 SELECT count(*) FROM sqlite_master;
103 } db2
104} {6}
105
106proc busy_handler {n} { return 1 }
dan4c167602017-10-04 12:08:35 +0000107do_test 3.5 {
dan61b513e2017-10-04 10:39:28 +0000108 catchsql { PRAGMA optimize }
109} {0 {}}
110
dan4c167602017-10-04 12:08:35 +0000111do_test 3.6 {
dan61b513e2017-10-04 10:39:28 +0000112 execsql { COMMIT } db2
113 execsql {
114 WITH s(i) AS (
115 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000
116 )
117 INSERT INTO t1 SELECT i FROM s;
118 }
119 execsql {
120 BEGIN;
121 SELECT count(*) FROM sqlite_master;
122 } db2
123} {6}
124
dan4c167602017-10-04 12:08:35 +0000125do_test 3.7 {
dan61b513e2017-10-04 10:39:28 +0000126 catchsql { PRAGMA optimize }
127} {1 {database is locked}}
128
129proc busy_handler {n} {
130 if {$n>1000} { execsql { COMMIT } db2 }
131 return 0
132}
dan4c167602017-10-04 12:08:35 +0000133do_test 3.8 {
dan61b513e2017-10-04 10:39:28 +0000134 catchsql { PRAGMA optimize }
135} {0 {}}
136
137}
138
drha4afb652005-07-09 02:16:02 +0000139finish_test