1# 2009 April 10
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 implements regression tests for SQLite library.
12#
13# This file implements tests to verify that ticket #3793 has been
14# fixed.  
15#
16# $Id: tkt3793.test,v 1.2 2009/06/01 16:42:18 shane Exp $
17
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22ifcapable !shared_cache||!attach {
23  finish_test
24  return
25}
26set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
27
28do_test tkt3793-1.1 {
29  # This is taken from shared.test.  The Windows VFS expands 
30  # ./test.db (and test.db) to be the same thing so the path
31  # matches and they share a cache.  By changing the case 
32  # for Windows platform, we get around this and get a separate
33  # connection.
34  if {$::tcl_platform(platform)=="unix"} {
35    sqlite3 db1 test.db
36    sqlite3 db2 test.db
37  } else {
38    sqlite3 db1 TEST.DB
39    sqlite3 db2 TEST.DB
40  }
41  execsql {
42    BEGIN;
43    CREATE TABLE t1(a, b);
44    CREATE TABLE t2(a PRIMARY KEY, b);
45    INSERT INTO t1 VALUES(randstr(50,50), randstr(50,50));
46    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
47    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
48    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
49    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
50    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
51    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
52    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
53    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
54    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
55    INSERT INTO t1 SELECT randstr(50,50), randstr(50,50) FROM t1;
56    INSERT INTO t2 SELECT * FROM t1;
57    COMMIT;
58  }
59} {}
60
61proc busyhandler {db args} { set ::busyconnection $db ; return 1 }
62db2 busy {busyhandler db2}
63db1 busy {busyhandler db1}
64
65# Establish a read-lock on the database file using connection [db].
66#
67do_test tkt3793-1.2 {
68  execsql {
69    BEGIN;
70    SELECT count(*) FROM t1;
71  }
72} {1024}
73
74# Set the size of the cache shared by [db1] and [db2] to 10. Then update
75# more than 10 pages of table t1. At this point the shared-cache will
76# hold a RESERVED lock on the database file. Even though there are now
77# more than 10 dirty pages in memory, it cannot upgrade to an EXCLUSIVE 
78# lock because of the read-lock held by [db].
79#
80do_test tkt3793-1.3 {
81  execsql {
82    PRAGMA cache_size = 10;
83    BEGIN;
84    UPDATE t1 SET b = randstr(50,50);
85  } db1
86} {}
87
88set x 0
89
90# Run one SELECT query on the shared-cache using [db1], then from within 
91# the callback run another via [db2]. Because of the large number of dirty
92# pages within the cache, each time a new page is read from the database
93# SQLite will attempt to upgrade to an EXCLUSIVE lock, and hence invoke
94# the busy-handler. The tests here verify that the correct busy-handler
95# function is invoked (the busy-handler associated with the database
96# connection that called sqlite3_step()). When bug #3793 existed, sometimes
97# the [db2] busy-handler was invoked from within the call to sqlite3_step()
98# associated with [db1]. 
99#
100# Note: Before the bug was fixed, if [db2] was opened with the "-fullmutex 1"
101# option, then this test case would cause an assert() to fail.
102#
103set ::busyconnection db1
104db1 eval {SELECT * FROM t2 ORDER BY a LIMIT 20} {
105  do_test tkt3793-2.[incr x] { set ::busyconnection } db1
106  set ::busyconnection db2
107
108  db2 eval { SELECT count(*) FROM t2 }
109  do_test tkt3793-2.[incr x] { set ::busyconnection } db2
110  set ::busyconnection db1
111}
112
113do_test tkt3793-3 {
114  db1 close
115  db2 close
116} {}
117
118sqlite3_enable_shared_cache $::enable_shared_cache
119finish_test
120