3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests to verify that ticket #3793 has been
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 ifcapable !shared_cache||!attach {
25 set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
29 sqlite3 db "file:test.db" -uri 1
30 sqlite3 db1 "file:test.db?cache=private" -uri 1
31 sqlite3 db2 "file:test.db?cache=shared" -uri 1
34 CREATE TABLE t1(a, b);
35 CREATE TABLE t2(a PRIMARY KEY, b);
36 INSERT INTO t1 VALUES(randstr(50,50), randstr(50,50));
37 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
38 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
39 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
40 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
41 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
42 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
43 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
44 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
45 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
46 INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
47 INSERT INTO t2 SELECT * FROM t1;
52 proc busyhandler {db args} { set ::busyconnection $db ; return 1 }
53 db2 busy {busyhandler db2}
54 db1 busy {busyhandler db1}
56 # Establish a read-lock on the database file using connection [db].
61 SELECT count(*) FROM t1;
65 # Set the size of the cache shared by [db1] and [db2] to 10. Then update
66 # more than 10 pages of table t1. At this point the shared-cache will
67 # hold a RESERVED lock on the database file. Even though there are now
68 # more than 10 dirty pages in memory, it cannot upgrade to an EXCLUSIVE
69 # lock because of the read-lock held by [db].
73 PRAGMA cache_size = 10;
75 UPDATE t1 SET b = randstr(50,50);
81 # Run one SELECT query on the shared-cache using [db1], then from within
82 # the callback run another via [db2]. Because of the large number of dirty
83 # pages within the cache, each time a new page is read from the database
84 # SQLite will attempt to upgrade to an EXCLUSIVE lock, and hence invoke
85 # the busy-handler. The tests here verify that the correct busy-handler
86 # function is invoked (the busy-handler associated with the database
87 # connection that called sqlite3_step()). When bug #3793 existed, sometimes
88 # the [db2] busy-handler was invoked from within the call to sqlite3_step()
89 # associated with [db1].
91 # Note: Before the bug was fixed, if [db2] was opened with the "-fullmutex 1"
92 # option, then this test case would cause an assert() to fail.
94 ifcapable threadsafe {
95 set ::busyconnection db1
96 db1 eval {SELECT * FROM t2 ORDER BY a LIMIT 20} {
97 do_test tkt3793-2.[incr x] { set ::busyconnection } db1
98 set ::busyconnection db2
100 db2 eval { SELECT count(*) FROM t2 }
101 do_test tkt3793-2.[incr x] { set ::busyconnection } db2
102 set ::busyconnection db1
111 sqlite3_enable_shared_cache $::enable_shared_cache