xref: /sqlite-3.40.0/test/pcache.test (revision 062d4cb0)
1# 2008 August 29
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# This file is focused on testing the pcache module.
13#
14# $Id: pcache.test,v 1.1 2008/08/29 09:10:03 danielk1977 Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19
20# The pcache module limits the number of pages available to purgeable
21# caches to the sum of the 'cache_size' values for the set of open
22# caches. This block of tests, pcache-1.*, test that the library behaves
23# corrctly when it is forced to exceed this limit.
24#
25do_test pcache-1.1 {
26  db close
27  pcache_stats
28} {current 0 max 0 min 0 recyclable 0}
29
30do_test pcache-1.2 {
31  sqlite3 db test.db
32  execsql "PRAGMA cache_size=10"
33  pcache_stats
34} {current 1 max 10 min 10 recyclable 1}
35
36do_test pcache-1.3 {
37  execsql {
38    BEGIN;
39    CREATE TABLE t1(a, b, c);
40    CREATE TABLE t2(a, b, c);
41    CREATE TABLE t3(a, b, c);
42    CREATE TABLE t4(a, b, c);
43    CREATE TABLE t5(a, b, c);
44  }
45  pcache_stats
46} {current 6 max 10 min 10 recyclable 0}
47
48do_test pcache-1.4 {
49  execsql {
50    CREATE TABLE t6(a, b, c);
51    CREATE TABLE t7(a, b, c);
52    CREATE TABLE t8(a, b, c);
53    CREATE TABLE t9(a, b, c);
54  }
55  pcache_stats
56} {current 10 max 10 min 10 recyclable 0}
57
58do_test pcache-1.5 {
59  sqlite3 db2 test.db
60  execsql "PRAGMA cache_size=10" db2
61  pcache_stats
62} {current 11 max 20 min 20 recyclable 1}
63
64do_test pcache-1.6 {
65  execsql {
66    BEGIN;
67    SELECT * FROM sqlite_master;
68  } db2
69  pcache_stats
70} {current 11 max 20 min 20 recyclable 0}
71
72# At this point connection db2 has a read lock on the database file and a
73# single pinned page in its cache. Connection [db] is holding 10 dirty
74# pages. It cannot recycle them because of the read lock held by db2.
75#
76do_test pcache-1.6 {
77  execsql {
78    CREATE INDEX i1 ON t1(a, b);
79    CREATE INDEX i2 ON t2(a, b);
80    CREATE INDEX i3 ON t3(a, b);
81    CREATE INDEX i4 ON t4(a, b);
82    CREATE INDEX i5 ON t5(a, b);
83    CREATE INDEX i6 ON t6(a, b);
84    CREATE INDEX i7 ON t7(a, b);
85    CREATE INDEX i8 ON t8(a, b);
86    CREATE INDEX i9 ON t9(a, b);
87  }
88  pcache_stats
89} {current 20 max 20 min 20 recyclable 0}
90
91do_test pcache-1.7 {
92  execsql {
93    CREATE TABLE t10(a, b, c);
94  }
95  pcache_stats
96} {current 21 max 20 min 20 recyclable 0}
97
98# Rolling back the transaction held by db2 at this point releases a pinned
99# page. Because the number of allocated pages is greater than the
100# configured maximum, this page should be freed immediately instead of
101# recycled.
102#
103do_test pcache-1.8 {
104  execsql {ROLLBACK} db2
105  pcache_stats
106} {current 20 max 20 min 20 recyclable 0}
107
108do_test pcache-1.9 {
109  execsql COMMIT
110  pcache_stats
111} {current 20 max 20 min 20 recyclable 20}
112
113do_test pcache-1.10 {
114  db2 close
115  pcache_stats
116} {current 10 max 10 min 10 recyclable 10}
117
118do_test pcache-1.11 {
119  execsql { PRAGMA cache_size = 20 }
120  pcache_stats
121} {current 10 max 20 min 10 recyclable 10}
122
123do_test pcache-1.12 {
124  execsql {
125    SELECT * FROM t1 ORDER BY a; SELECT * FROM t1;
126    SELECT * FROM t2 ORDER BY a; SELECT * FROM t2;
127    SELECT * FROM t3 ORDER BY a; SELECT * FROM t3;
128    SELECT * FROM t4 ORDER BY a; SELECT * FROM t4;
129    SELECT * FROM t5 ORDER BY a; SELECT * FROM t5;
130    SELECT * FROM t6 ORDER BY a; SELECT * FROM t6;
131    SELECT * FROM t7 ORDER BY a; SELECT * FROM t7;
132    SELECT * FROM t8 ORDER BY a; SELECT * FROM t8;
133    SELECT * FROM t9 ORDER BY a; SELECT * FROM t9;
134  }
135  pcache_stats
136} {current 19 max 20 min 10 recyclable 19}
137
138do_test pcache-1.13 {
139  execsql { PRAGMA cache_size = 15 }
140  pcache_stats
141} {current 15 max 15 min 10 recyclable 15}
142
143finish_test
144
145