blob: 0abadc3ef80bcbb1a8267691199c9ffb85b63d4b [file] [log] [blame]
danielk1977aef0bf62005-12-30 16:28:01 +00001# 2005 December 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# This file implements regression tests for SQLite library. The
12# focus of this file is testing the SELECT statement.
13#
danielk1977da184232006-01-05 11:34:32 +000014# $Id: shared.test,v 1.2 2006/01/05 11:34:34 danielk1977 Exp $
danielk1977aef0bf62005-12-30 16:28:01 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18db close
19
20ifcapable !shared_cache {
21 finish_test
22 return
23}
danielk1977da184232006-01-05 11:34:32 +000024set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
danielk1977aef0bf62005-12-30 16:28:01 +000025
26# Test organization:
27#
28# shared-1.*: Simple test to verify basic sanity of table level locking when
29# two connections share a pager cache.
30# shared-2.*: Test that a read transaction can co-exist with a
31# write-transaction, including a simple test to ensure the
32# external locking protocol is still working.
danielk1977da184232006-01-05 11:34:32 +000033# shared-3.*: Simple test of read-uncommitted mode.
danielk1977aef0bf62005-12-30 16:28:01 +000034#
35
36do_test shared-1.1 {
37 # Open a second database on the file test.db. It should use the same pager
38 # cache and schema as the original connection. Verify that only 1 file is
39 # opened.
40 sqlite3 db2 test.db
41 sqlite3 db test.db
42 set ::sqlite_open_file_count
43} {1}
44do_test shared-1.2 {
45 # Add a table and a single row of data via the first connection.
46 # Ensure that the second connection can see them.
47 execsql {
48 CREATE TABLE abc(a, b, c);
49 INSERT INTO abc VALUES(1, 2, 3);
50 } db
51 execsql {
52 SELECT * FROM abc;
53 } db2
54} {1 2 3}
55do_test shared-1.3 {
56 # Have the first connection begin a transaction and obtain a read-lock
57 # on table abc. This should not prevent the second connection from
58 # querying abc.
59 execsql {
60 BEGIN;
61 SELECT * FROM abc;
62 }
63 execsql {
64 SELECT * FROM abc;
65 } db2
66} {1 2 3}
67do_test shared-1.4 {
68 # Try to insert a row into abc via connection 2. This should fail because
69 # of the read-lock connection 1 is holding on table abc (obtained in the
70 # previous test case).
71 catchsql {
72 INSERT INTO abc VALUES(4, 5, 6);
73 } db2
74} {1 {database is locked}}
75do_test shared-1.5 {
danielk1977da184232006-01-05 11:34:32 +000076 # Using connection 2 (the one without the open transaction), try to create
77 # a new table. This should fail because of the open read transaction
78 # held by connection 1.
79 catchsql {
80 CREATE TABLE def(d, e, f);
81 } db2
82} {1 {database is locked}}
83do_test shared-1.6 {
84 # Upgrade connection 1's transaction to a write transaction. Create
85 # a new table - def - and insert a row into it. Because the connection 1
86 # transaction modifies the schema, it should not be possible for
87 # connection 2 to access the database at all until the connection 1
88 # has finished the transaction.
danielk1977aef0bf62005-12-30 16:28:01 +000089 execsql {
90 CREATE TABLE def(d, e, f);
danielk1977aef0bf62005-12-30 16:28:01 +000091 INSERT INTO def VALUES('IV', 'V', 'VI');
92 }
93} {}
94do_test shared-1.7 {
95 # Read from the sqlite_master table with connection 1 (inside the
danielk1977da184232006-01-05 11:34:32 +000096 # transaction). Then test that we can not do this with connection 2. This
97 # is because of the schema-modified lock established by connection 1
98 # in the previous test case.
danielk1977aef0bf62005-12-30 16:28:01 +000099 execsql {
100 SELECT * FROM sqlite_master;
101 }
102 catchsql {
danielk1977da184232006-01-05 11:34:32 +0000103 SELECT * FROM sqlite_master;
danielk1977aef0bf62005-12-30 16:28:01 +0000104 } db2
105} {1 {database is locked}}
106do_test shared-1.8 {
danielk1977aef0bf62005-12-30 16:28:01 +0000107 # Commit the connection 1 transaction.
108 execsql {
109 COMMIT;
110 }
111} {}
112
113do_test shared-2.1 {
114 # Open connection db3 to the database. Use a different path to the same
115 # file so that db3 does *not* share the same pager cache as db and db2
116 # (there should be two open file handles).
117 sqlite3 db3 ./test.db
118 set ::sqlite_open_file_count
119} {2}
120do_test shared-2.2 {
121 # Start read transactions on db and db2 (the shared pager cache). Ensure
122 # db3 cannot write to the database.
123 execsql {
124 BEGIN;
125 SELECT * FROM abc;
126 }
127 execsql {
128 BEGIN;
129 SELECT * FROM abc;
130 } db2
131 catchsql {
132 INSERT INTO abc VALUES(1, 2, 3);
133 } db2
134} {1 {database is locked}}
135do_test shared-2.3 {
136 # Turn db's transaction into a write-transaction. db3 should still be
137 # able to read from table def (but will not see the new row). Connection
138 # db2 should not be able to read def (because of the write-lock).
139
140# Todo: The failed "INSERT INTO abc ..." statement in the above test
141# has started a write-transaction on db2 (should this be so?). This
142# would prevent connection db from starting a write-transaction. So roll the
143# db2 transaction back and replace it with a new read transaction.
144 execsql {
145 ROLLBACK;
146 BEGIN;
147 SELECT * FROM abc;
148 } db2
149
150 execsql {
151 INSERT INTO def VALUES('VII', 'VIII', 'IX');
152 }
153 concat [
154 catchsql { SELECT * FROM def; } db3
155 ] [
156 catchsql { SELECT * FROM def; } db2
157 ]
danielk1977da184232006-01-05 11:34:32 +0000158} {0 {IV V VI} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000159do_test shared-2.4 {
160 # Commit the open transaction on db. db2 still holds a read-transaction.
161 # This should prevent db3 from writing to the database, but not from
162 # reading.
163 execsql {
164 COMMIT;
165 }
166 concat [
167 catchsql { SELECT * FROM def; } db3
168 ] [
169 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
170 ]
danielk1977da184232006-01-05 11:34:32 +0000171} {0 {IV V VI VII VIII IX} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000172
danielk1977da184232006-01-05 11:34:32 +0000173catchsql COMMIT db2
174
175do_test shared-3.1.1 {
176 # This test case starts a linear scan of table 'seq' using a
177 # read-uncommitted connection. In the middle of the scan, rows are added
178 # to the end of the seq table (ahead of the current cursor position).
179 # The uncommitted rows should be included in the results of the scan.
180 execsql "
181 CREATE TABLE seq(i, x);
182 INSERT INTO seq VALUES(1, '[string repeat X 500]');
183 INSERT INTO seq VALUES(2, '[string repeat X 500]');
184 "
185 execsql {SELECT * FROM sqlite_master} db2
186 execsql {PRAGMA read_uncommitted = 1} db2
187
188 set ret [list]
189 db2 eval {SELECT i FROM seq} {
190 if {$i < 4} {
191 execsql {
192 INSERT INTO seq SELECT i + (SELECT max(i) FROM seq), x FROM seq;
193 }
194 }
195 lappend ret $i
196 }
197 set ret
198} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
199do_test shared-3.1.2 {
200 # Another linear scan through table seq using a read-uncommitted connection.
201 # This time, delete each row as it is read. Should not affect the results of
202 # the scan, but the table should be empty after the scan is concluded
203 # (test 3.1.3 verifies this).
204 set ret [list]
205 db2 eval {SELECT i FROM seq} {
206 db eval {DELETE FROM seq WHERE i = $i}
207 lappend ret $i
208 }
209 set ret
210} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
211do_test shared-3.1.3 {
212 execsql {
213 SELECT * FROM seq;
214 }
215} {}
danielk1977aef0bf62005-12-30 16:28:01 +0000216
217catch {db close}
218catch {db2 close}
219catch {db3 close}
220
221finish_test
222sqlite3_enable_shared_cache $::enable_shared_cache
223