cache.test 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # 2007 March 24
  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. #
  12. # $Id: cache.test,v 1.4 2007/08/22 02:56:44 drh Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. ifcapable !pager_pragmas||!compound {
  16. finish_test
  17. return
  18. }
  19. sqlite3_soft_heap_limit 0
  20. proc pager_cache_size {db} {
  21. set bt [btree_from_db $db]
  22. db_enter $db
  23. array set stats [btree_pager_stats $bt]
  24. db_leave $db
  25. return $stats(page)
  26. }
  27. if {[permutation] == ""} {
  28. do_test cache-1.1 { pager_cache_size db } {0}
  29. }
  30. do_test cache-1.2 {
  31. execsql {
  32. PRAGMA auto_vacuum=OFF;
  33. CREATE TABLE abc(a, b, c);
  34. INSERT INTO abc VALUES(1, 2, 3);
  35. }
  36. pager_cache_size db
  37. } {2}
  38. # At one point, repeatedly locking and unlocking the cache was causing
  39. # a resource leak of one page per repetition. The page wasn't actually
  40. # leaked, but would not be reused until the pager-cache was full (i.e.
  41. # 2000 pages by default).
  42. #
  43. # This tests that once the pager-cache is initialized, it can be locked
  44. # and unlocked repeatedly without internally allocating any new pages.
  45. #
  46. set cache_size [pager_cache_size db]
  47. for {set ii 0} {$ii < 10} {incr ii} {
  48. do_test cache-1.3.$ii {
  49. execsql {SELECT * FROM abc}
  50. pager_cache_size db
  51. } $::cache_size
  52. }
  53. #-------------------------------------------------------------------------
  54. # This block of tests checks that it is possible to set the cache_size of a
  55. # database to a small (< 10) value. More specifically:
  56. #
  57. # cache-2.1.*: Test that "PRAGMA cache_size" appears to work with small
  58. # values.
  59. # cache-2.2.*: Test that "PRAGMA main.cache_size" appears to work with
  60. # small values.
  61. # cache-2.3.*: Test cache_size=1 correctly spills/flushes the cache.
  62. # cache-2.4.*: Test cache_size=0 correctly spills/flushes the cache.
  63. #
  64. #
  65. db_delete_and_reopen
  66. do_execsql_test cache-2.0 {
  67. PRAGMA auto_vacuum=OFF;
  68. PRAGMA journal_mode=DELETE;
  69. CREATE TABLE t1(a, b);
  70. CREATE TABLE t2(c, d);
  71. INSERT INTO t1 VALUES('x', 'y');
  72. INSERT INTO t2 VALUES('i', 'j');
  73. } {delete}
  74. for {set i 0} {$i < 20} {incr i} {
  75. do_execsql_test cache-2.1.$i.1 "PRAGMA cache_size = $i"
  76. do_execsql_test cache-2.1.$i.2 "PRAGMA cache_size" $i
  77. do_execsql_test cache-2.1.$i.3 "SELECT * FROM t1" {x y}
  78. do_execsql_test cache-2.1.$i.4 "PRAGMA cache_size" $i
  79. }
  80. for {set i 0} {$i < 20} {incr i} {
  81. do_execsql_test cache-2.2.$i.1 "PRAGMA main.cache_size = $i"
  82. do_execsql_test cache-2.2.$i.2 "PRAGMA main.cache_size" $i
  83. do_execsql_test cache-2.2.$i.3 "SELECT * FROM t1" {x y}
  84. do_execsql_test cache-2.2.$i.4 "PRAGMA main.cache_size" $i
  85. }
  86. # Tests for cache_size = 1.
  87. #
  88. do_execsql_test cache-2.3.1 {
  89. PRAGMA cache_size = 1;
  90. BEGIN;
  91. INSERT INTO t1 VALUES(1, 2);
  92. PRAGMA lock_status;
  93. } {main reserved temp closed}
  94. do_test cache-2.3.2 { pager_cache_size db } 2
  95. do_execsql_test cache-2.3.3 {
  96. INSERT INTO t2 VALUES(1, 2);
  97. PRAGMA lock_status;
  98. } {main exclusive temp closed}
  99. do_test cache-2.3.4 { pager_cache_size db } 2
  100. do_execsql_test cache-2.3.5 COMMIT
  101. do_test cache-2.3.6 { pager_cache_size db } 1
  102. do_execsql_test cache-2.3.7 {
  103. SELECT * FROM t1 UNION SELECT * FROM t2;
  104. } {1 2 i j x y}
  105. do_test cache-2.3.8 { pager_cache_size db } 1
  106. # Tests for cache_size = 0.
  107. #
  108. do_execsql_test cache-2.4.1 {
  109. PRAGMA cache_size = 0;
  110. BEGIN;
  111. INSERT INTO t1 VALUES(1, 2);
  112. PRAGMA lock_status;
  113. } {main reserved temp closed}
  114. do_test cache-2.4.2 { pager_cache_size db } 2
  115. do_execsql_test cache-2.4.3 {
  116. INSERT INTO t2 VALUES(1, 2);
  117. PRAGMA lock_status;
  118. } {main exclusive temp closed}
  119. do_test cache-2.4.4 { pager_cache_size db } 2
  120. do_execsql_test cache-2.4.5 COMMIT
  121. do_test cache-2.4.6 { pager_cache_size db } 0
  122. do_execsql_test cache-2.4.7 {
  123. SELECT * FROM t1 UNION SELECT * FROM t2;
  124. } {1 2 i j x y}
  125. do_test cache-2.4.8 { pager_cache_size db } 0
  126. sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
  127. finish_test