danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 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 | # |
shane | 739a277 | 2009-06-01 16:42:17 +0000 | [diff] [blame] | 16 | # $Id: tkt3793.test,v 1.2 2009/06/01 16:42:18 shane Exp $ |
danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | set testdir [file dirname $argv0] |
| 20 | source $testdir/tester.tcl |
| 21 | |
| 22 | ifcapable !shared_cache||!attach { |
| 23 | finish_test |
| 24 | return |
| 25 | } |
| 26 | set ::enable_shared_cache [sqlite3_enable_shared_cache 1] |
| 27 | |
| 28 | do_test tkt3793-1.1 { |
shane | 739a277 | 2009-06-01 16:42:17 +0000 | [diff] [blame] | 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 | } |
danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 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 | |
| 61 | proc busyhandler {db args} { set ::busyconnection $db ; return 1 } |
| 62 | db2 busy {busyhandler db2} |
| 63 | db1 busy {busyhandler db1} |
| 64 | |
| 65 | # Establish a read-lock on the database file using connection [db]. |
| 66 | # |
| 67 | do_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 | # |
| 80 | do_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 | |
| 88 | set 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 | # |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 103 | ifcapable threadsafe { |
danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 104 | set ::busyconnection db1 |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 105 | db1 eval {SELECT * FROM t2 ORDER BY a LIMIT 20} { |
| 106 | do_test tkt3793-2.[incr x] { set ::busyconnection } db1 |
| 107 | set ::busyconnection db2 |
| 108 | |
| 109 | db2 eval { SELECT count(*) FROM t2 } |
| 110 | do_test tkt3793-2.[incr x] { set ::busyconnection } db2 |
| 111 | set ::busyconnection db1 |
| 112 | } |
danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 113 | } |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 114 | |
danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 115 | do_test tkt3793-3 { |
| 116 | db1 close |
| 117 | db2 close |
| 118 | } {} |
| 119 | |
| 120 | sqlite3_enable_shared_cache $::enable_shared_cache |
| 121 | finish_test |