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