drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1 | # 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 | # |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 17 | set testprefix busy |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 18 | |
| 19 | do_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 |
| 27 | proc busy x { |
| 28 | lappend ::busyargs $x |
| 29 | if {$x>2} {return 1} |
| 30 | return 0 |
| 31 | } |
drh | e3000ae | 2005-09-17 18:02:36 +0000 | [diff] [blame] | 32 | set busyargs {} |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 33 | do_test busy-1.2 { |
| 34 | db busy busy |
drh | 175cd71 | 2008-03-15 02:09:21 +0000 | [diff] [blame] | 35 | db2 eval {BEGIN EXCLUSIVE} |
| 36 | catchsql {BEGIN IMMEDIATE} |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 37 | } {1 {database is locked}} |
| 38 | do_test busy-1.3 { |
| 39 | set busyargs |
| 40 | } {0 1 2 3} |
drh | 175cd71 | 2008-03-15 02:09:21 +0000 | [diff] [blame] | 41 | do_test busy-1.4 { |
| 42 | set busyargs {} |
| 43 | catchsql {BEGIN IMMEDIATE} |
| 44 | set busyargs |
| 45 | } {0 1 2 3} |
| 46 | |
| 47 | do_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}} |
| 54 | do_test busy-2.2 { |
| 55 | set busyargs |
| 56 | } {0 1 2 3} |
| 57 | |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 58 | db2 close |
| 59 | |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 60 | #------------------------------------------------------------------------- |
| 61 | # Test that the busy-handler is invoked correctly for "PRAGMA optimize" |
| 62 | # and ANALYZE commnds. |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 63 | ifcapable pragma&&analyze&&!stat4 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 64 | |
| 65 | reset_db |
| 66 | |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 67 | do_execsql_test 3.1 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 68 | 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 | |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 83 | do_test 3.2 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 84 | sqlite3 db2 test.db |
| 85 | execsql { BEGIN EXCLUSIVE } db2 |
| 86 | catchsql { PRAGMA optimize } |
| 87 | } {1 {database is locked}} |
| 88 | |
| 89 | proc busy_handler {n} { |
| 90 | if {$n>1000} { execsql { COMMIT } db2 } |
| 91 | return 0 |
| 92 | } |
| 93 | db busy busy_handler |
| 94 | |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 95 | do_test 3.3 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 96 | catchsql { PRAGMA optimize } |
| 97 | } {0 {}} |
| 98 | |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 99 | do_test 3.4 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 100 | execsql { |
| 101 | BEGIN; |
| 102 | SELECT count(*) FROM sqlite_master; |
| 103 | } db2 |
| 104 | } {6} |
| 105 | |
| 106 | proc busy_handler {n} { return 1 } |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 107 | do_test 3.5 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 108 | catchsql { PRAGMA optimize } |
| 109 | } {0 {}} |
| 110 | |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 111 | do_test 3.6 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 112 | 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 | |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 125 | do_test 3.7 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 126 | catchsql { PRAGMA optimize } |
| 127 | } {1 {database is locked}} |
| 128 | |
| 129 | proc busy_handler {n} { |
| 130 | if {$n>1000} { execsql { COMMIT } db2 } |
| 131 | return 0 |
| 132 | } |
dan | 4c16760 | 2017-10-04 12:08:35 +0000 | [diff] [blame] | 133 | do_test 3.8 { |
dan | 61b513e | 2017-10-04 10:39:28 +0000 | [diff] [blame] | 134 | catchsql { PRAGMA optimize } |
| 135 | } {0 {}} |
| 136 | |
| 137 | } |
| 138 | |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 139 | finish_test |