xref: /sqlite-3.40.0/test/busy.test (revision 4c16760c)
1a4afb65cSdrh# 2005 july 8
2a4afb65cSdrh#
3a4afb65cSdrh# The author disclaims copyright to this source code.  In place of
4a4afb65cSdrh# a legal notice, here is a blessing:
5a4afb65cSdrh#
6a4afb65cSdrh#    May you do good and not evil.
7a4afb65cSdrh#    May you find forgiveness for yourself and forgive others.
8a4afb65cSdrh#    May you share freely, never taking more than you give.
9a4afb65cSdrh#
10a4afb65cSdrh#***********************************************************************
11a4afb65cSdrh# This file test the busy handler
12a4afb65cSdrh#
13a4afb65cSdrh
14a4afb65cSdrh
15a4afb65cSdrhset testdir [file dirname $argv0]
16a4afb65cSdrhsource $testdir/tester.tcl
1761b513e9Sdanset testprefix busy
18a4afb65cSdrh
19a4afb65cSdrhdo_test busy-1.1 {
20a4afb65cSdrh  sqlite3 db2 test.db
21a4afb65cSdrh  execsql {
22a4afb65cSdrh    CREATE TABLE t1(x);
23a4afb65cSdrh    INSERT INTO t1 VALUES(1);
24a4afb65cSdrh    SELECT * FROM t1
25a4afb65cSdrh  }
26a4afb65cSdrh} 1
27a4afb65cSdrhproc busy x {
28a4afb65cSdrh  lappend ::busyargs $x
29a4afb65cSdrh  if {$x>2} {return 1}
30a4afb65cSdrh  return 0
31a4afb65cSdrh}
32e3000ae8Sdrhset busyargs {}
33a4afb65cSdrhdo_test busy-1.2 {
34a4afb65cSdrh  db busy busy
35175cd71bSdrh  db2 eval {BEGIN EXCLUSIVE}
36175cd71bSdrh  catchsql {BEGIN IMMEDIATE}
37a4afb65cSdrh} {1 {database is locked}}
38a4afb65cSdrhdo_test busy-1.3 {
39a4afb65cSdrh  set busyargs
40a4afb65cSdrh} {0 1 2 3}
41175cd71bSdrhdo_test busy-1.4 {
42175cd71bSdrh  set busyargs {}
43175cd71bSdrh  catchsql {BEGIN IMMEDIATE}
44175cd71bSdrh  set busyargs
45175cd71bSdrh} {0 1 2 3}
46175cd71bSdrh
47175cd71bSdrhdo_test busy-2.1 {
48175cd71bSdrh  db2 eval {COMMIT}
49175cd71bSdrh  db eval {BEGIN; INSERT INTO t1 VALUES(5)}
50175cd71bSdrh  db2 eval {BEGIN; SELECT * FROM t1}
51175cd71bSdrh  set busyargs {}
52175cd71bSdrh  catchsql COMMIT
53175cd71bSdrh} {1 {database is locked}}
54175cd71bSdrhdo_test busy-2.2 {
55175cd71bSdrh  set busyargs
56175cd71bSdrh} {0 1 2 3}
57175cd71bSdrh
58a4afb65cSdrhdb2 close
59a4afb65cSdrh
6061b513e9Sdan#-------------------------------------------------------------------------
6161b513e9Sdan# Test that the busy-handler is invoked correctly for "PRAGMA optimize"
6261b513e9Sdan# and ANALYZE commnds.
63*4c16760cSdanifcapable pragma&&analyze&&!stat4 {
6461b513e9Sdan
6561b513e9Sdanreset_db
6661b513e9Sdan
67*4c16760cSdando_execsql_test 3.1 {
6861b513e9Sdan  CREATE TABLE t1(x);
6961b513e9Sdan  CREATE TABLE t2(y);
7061b513e9Sdan  CREATE TABLE t3(z);
7161b513e9Sdan
7261b513e9Sdan  CREATE INDEX i1 ON t1(x);
7361b513e9Sdan  CREATE INDEX i2 ON t2(y);
7461b513e9Sdan
7561b513e9Sdan  INSERT INTO t1 VALUES(1);
7661b513e9Sdan  INSERT INTO t2 VALUES(1);
7761b513e9Sdan  ANALYZE;
7861b513e9Sdan
7961b513e9Sdan  SELECT * FROM t1 WHERE x=1;
8061b513e9Sdan  SELECT * FROM t2 WHERE y=1;
8161b513e9Sdan} {1 1}
8261b513e9Sdan
83*4c16760cSdando_test 3.2 {
8461b513e9Sdan  sqlite3 db2 test.db
8561b513e9Sdan  execsql { BEGIN EXCLUSIVE } db2
8661b513e9Sdan  catchsql { PRAGMA optimize }
8761b513e9Sdan} {1 {database is locked}}
8861b513e9Sdan
8961b513e9Sdanproc busy_handler {n} {
9061b513e9Sdan  if {$n>1000} { execsql { COMMIT } db2 }
9161b513e9Sdan  return 0
9261b513e9Sdan}
9361b513e9Sdandb busy busy_handler
9461b513e9Sdan
95*4c16760cSdando_test 3.3 {
9661b513e9Sdan  catchsql { PRAGMA optimize }
9761b513e9Sdan} {0 {}}
9861b513e9Sdan
99*4c16760cSdando_test 3.4 {
10061b513e9Sdan  execsql {
10161b513e9Sdan    BEGIN;
10261b513e9Sdan    SELECT count(*) FROM sqlite_master;
10361b513e9Sdan  } db2
10461b513e9Sdan} {6}
10561b513e9Sdan
10661b513e9Sdanproc busy_handler {n} { return 1 }
107*4c16760cSdando_test 3.5 {
10861b513e9Sdan  catchsql { PRAGMA optimize }
10961b513e9Sdan} {0 {}}
11061b513e9Sdan
111*4c16760cSdando_test 3.6 {
11261b513e9Sdan  execsql { COMMIT } db2
11361b513e9Sdan  execsql {
11461b513e9Sdan    WITH s(i) AS (
11561b513e9Sdan      SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000
11661b513e9Sdan    )
11761b513e9Sdan    INSERT INTO t1 SELECT i FROM s;
11861b513e9Sdan  }
11961b513e9Sdan  execsql {
12061b513e9Sdan    BEGIN;
12161b513e9Sdan    SELECT count(*) FROM sqlite_master;
12261b513e9Sdan  } db2
12361b513e9Sdan} {6}
12461b513e9Sdan
125*4c16760cSdando_test 3.7 {
12661b513e9Sdan  catchsql { PRAGMA optimize }
12761b513e9Sdan} {1 {database is locked}}
12861b513e9Sdan
12961b513e9Sdanproc busy_handler {n} {
13061b513e9Sdan  if {$n>1000} { execsql { COMMIT } db2 }
13161b513e9Sdan  return 0
13261b513e9Sdan}
133*4c16760cSdando_test 3.8 {
13461b513e9Sdan  catchsql { PRAGMA optimize }
13561b513e9Sdan} {0 {}}
13661b513e9Sdan
13761b513e9Sdan}
13861b513e9Sdan
139a4afb65cSdrhfinish_test
140