tkt3793.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. ifcapable !shared_cache||!attach {
  20. finish_test
  21. return
  22. }
  23. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  24. do_test tkt3793-1.1 {
  25. # This is taken from shared.test. The Windows VFS expands
  26. # ./test.db (and test.db) to be the same thing so the path
  27. # matches and they share a cache. By changing the case
  28. # for Windows platform, we get around this and get a separate
  29. # connection.
  30. if {$::tcl_platform(platform)=="unix"} {
  31. sqlite3 db1 test.db
  32. sqlite3 db2 test.db
  33. } else {
  34. sqlite3 db1 TEST.DB
  35. sqlite3 db2 TEST.DB
  36. }
  37. execsql {
  38. BEGIN;
  39. CREATE TABLE t1(a, b);
  40. CREATE TABLE t2(a PRIMARY KEY, b);
  41. INSERT INTO t1 VALUES(randstr(50,50), randstr(50,50));
  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 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 t2 SELECT * FROM t1;
  53. COMMIT;
  54. }
  55. } {}
  56. proc busyhandler {db args} { set ::busyconnection $db ; return 1 }
  57. db2 busy {busyhandler db2}
  58. db1 busy {busyhandler db1}
  59. # Establish a read-lock on the database file using connection [db].
  60. #
  61. do_test tkt3793-1.2 {
  62. execsql {
  63. BEGIN;
  64. SELECT count(*) FROM t1;
  65. }
  66. } {1024}
  67. # Set the size of the cache shared by [db1] and [db2] to 10. Then update
  68. # more than 10 pages of table t1. At this point the shared-cache will
  69. # hold a RESERVED lock on the database file. Even though there are now
  70. # more than 10 dirty pages in memory, it cannot upgrade to an EXCLUSIVE
  71. # lock because of the read-lock held by [db].
  72. #
  73. do_test tkt3793-1.3 {
  74. execsql {
  75. PRAGMA cache_size = 10;
  76. BEGIN;
  77. UPDATE t1 SET b = randstr(50,50);
  78. } db1
  79. } {}
  80. set x 0
  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].
  90. #
  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.
  93. #
  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
  99. db2 eval { SELECT count(*) FROM t2 }
  100. do_test tkt3793-2.[incr x] { set ::busyconnection } db2
  101. set ::busyconnection db1
  102. }
  103. }
  104. do_test tkt3793-3 {
  105. db1 close
  106. db2 close
  107. } {}
  108. sqlite3_enable_shared_cache $::enable_shared_cache
  109. finish_test