blob: 917f515c9417aa4a6b59dc98291fb680bf1a2456 [file] [log] [blame]
danielk19770190d1d2005-12-19 14:18:11 +00001# 2005 November 30
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: malloc5.test,v 1.1 2005/12/19 14:18:12 danielk1977 Exp $
13
14#---------------------------------------------------------------------------
15# NOTES ON EXPECTED BEHAVIOUR
16#
17#---------------------------------------------------------------------------
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22do_test malloc5-1.1 {
23 # Simplest possible test. Call [db release_memory] when there is exactly
24 # one unused page in a single pager cache. This test case set's the
25 # value of the ::pgalloc variable, which is used in subsequent tests.
26 #
27 # Note: Even though executing this statement on an empty database
28 # modifies 2 pages (the root of sqlite_master and the new root page),
29 # the sqlite_master root (page 1) is never freed because the btree layer
30 # retains a reference to it for the entire transaction.
31 execsql {
32 BEGIN;
33 CREATE TABLE abc(a, b, c);
34 }
35 set ::pgalloc [db release_memory]
36 expr $::pgalloc > 0
37} {1}
38do_test malloc5-1.2 {
39 # Test that the transaction started in the above test is still active.
40 # Because the page freed had been written to, freeing it required a
41 # journal sync and exclusive lock on the database file. Test the file
42 # appears to be locked.
43 sqlite3 db2 test.db
44 catchsql {
45 SELECT * FROM abc;
46 } db2
47} {1 {database is locked}}
48do_test malloc5-1.3 {
49 # Again call [db release_memory] when there is exactly one unused page
50 # in the cache. The same amount of memory is required, but no journal-sync
51 # or exclusive lock should be established.
52 execsql {
53 COMMIT;
54 BEGIN;
55 SELECT * FROM abc;
56 }
57 db release_memory
58} $::pgalloc
59do_test malloc5-1.4 {
60 # Database should not be locked this time.
61 catchsql {
62 SELECT * FROM abc;
63 } db2
64} {0 {}}
65do_test malloc5-1.5 {
66 # Manipulate the cache so that it contains two unused pages. One requires
67 # a journal-sync to free, the other does not.
68 execsql {
69 SELECT * FROM abc;
70 CREATE TABLE def(d, e, f);
71 }
72 db release_memory 500
73} $::pgalloc
74do_test malloc5-1.6 {
75 # Database should not be locked this time. The above test case only
76 # requested 500 bytes of memory, which can be obtained by freeing the page
77 # that does not require an fsync().
78 catchsql {
79 SELECT * FROM abc;
80 } db2
81} {0 {}}
82do_test malloc5-1.7 {
83 # Release another 500 bytes of memory. This time we require a sync(),
84 # so the database file will be locked afterwards.
85 db release_memory 500
86} $::pgalloc
87do_test malloc5-1.8 {
88 catchsql {
89 SELECT * FROM abc;
90 } db2
91} {1 {database is locked}}
92do_test malloc5-1.9 {
93 execsql {
94 COMMIT;
95 }
96} {}
97
98
99do_test malloc5-2.1 {
100 # Put some data in tables abc and def. Both tables are still wholly
101 # contained within their root pages.
102 execsql {
103 INSERT INTO abc VALUES(1, 2, 3);
104 INSERT INTO abc VALUES(4, 5, 6);
105 INSERT INTO def VALUES(7, 8, 9);
106 INSERT INTO def VALUES(10,11,12);
107 }
108} {}
109do_test malloc5-2.2 {
110 # Load the root-page for table def into the cache. Then query table abc.
111 # Halfway through the query call sqlite3_release_memory(). The goal of this
112 # test is to make sure we don't free pages that are in use (specifically,
113 # the root of table abc).
114 set nRelease 0
115 execsql {
116 BEGIN;
117 SELECT * FROM def;
118 }
119 db eval {SELECT * FROM abc} {
120 incr nRelease [db release_memory]
121 lappend data $a $b $c
122 }
123 list $nRelease $data
124} [list $pgalloc [list 1 2 3 4 5 6]]
125
126finish_test
127
128