blob: ddbe3415c599a89bce2bec5a9d6c49956374ae17 [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#
14# $Id: shared.test,v 1.1 2005/12/30 16:28:02 danielk1977 Exp $
15
16set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20db close
21
22ifcapable !shared_cache {
23 finish_test
24 return
25}
26
27# Test organization:
28#
29# shared-1.*: Simple test to verify basic sanity of table level locking when
30# two connections share a pager cache.
31# shared-2.*: Test that a read transaction can co-exist with a
32# write-transaction, including a simple test to ensure the
33# external locking protocol is still working.
34#
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 {
76 # Using connection 2 (the one without the open transaction), create a
77 # new table and add a row to it. This is permitted as the transaction
78 # started by connection 1 is currently a read transaction.
79 execsql {
80 CREATE TABLE def(d, e, f);
81 INSERT INTO def VALUES('I', 'II', 'III');
82 } db2
83} {}
84do_test shared-1.6 {
85 # Upgrade connection 1's transaction to a write transaction. Insert
86 # a row into table def - the table just created by connection 2.
87 #
88 # Connection 1 is able to see table def, even though it was created
89 # "after" the connection 1 transaction was started. This is because no
90 # lock was established on the sqlite_master table.
91
92# Todo: Remove this. Because the implementation does not include
93# shared-schemas yet, we need to run some query (that will fail at
94# OP_VerifyCookie) so that connection 1 picks up the schema change
95# made via connection 2. Otherwise the sqlite3_prepare("INSERT INTO def...")
96# below will fail.
97execsql {
98 SELECT * FROM sqlite_master;
99}
100
101 execsql {
102 INSERT INTO def VALUES('IV', 'V', 'VI');
103 }
104} {}
105do_test shared-1.7 {
106 # Read from the sqlite_master table with connection 1 (inside the
107 # transaction). Then test that we can no longer create a table
108 # with connection 2. This is because of the read-lock on sqlite_master.
109 execsql {
110 SELECT * FROM sqlite_master;
111 }
112 catchsql {
113 CREATE TABLE ghi(g, h, i);
114 } db2
115} {1 {database is locked}}
116do_test shared-1.8 {
117 # Check that connection 2 can read the sqlite_master table. Then
118 # create a table using connection 1 (this should write-lock the
119 # sqlite_master table). Then try to read sqlite_master again using
120 # connection 2 and verify that the write-lock prevents this.
121 execsql {
122 SELECT * FROM sqlite_master;
123 } db2
124 execsql {
125 CREATE TABLE ghi(g, h, i);
126 }
127 catchsql {
128 SELECT * FROM sqlite_master;
129 } db2
130} {1 {database is locked}}
131do_test shared-1.9 {
132 # Commit the connection 1 transaction.
133 execsql {
134 COMMIT;
135 }
136} {}
137
138do_test shared-2.1 {
139 # Open connection db3 to the database. Use a different path to the same
140 # file so that db3 does *not* share the same pager cache as db and db2
141 # (there should be two open file handles).
142 sqlite3 db3 ./test.db
143 set ::sqlite_open_file_count
144} {2}
145do_test shared-2.2 {
146 # Start read transactions on db and db2 (the shared pager cache). Ensure
147 # db3 cannot write to the database.
148 execsql {
149 BEGIN;
150 SELECT * FROM abc;
151 }
152 execsql {
153 BEGIN;
154 SELECT * FROM abc;
155 } db2
156 catchsql {
157 INSERT INTO abc VALUES(1, 2, 3);
158 } db2
159} {1 {database is locked}}
160do_test shared-2.3 {
161 # Turn db's transaction into a write-transaction. db3 should still be
162 # able to read from table def (but will not see the new row). Connection
163 # db2 should not be able to read def (because of the write-lock).
164
165# Todo: The failed "INSERT INTO abc ..." statement in the above test
166# has started a write-transaction on db2 (should this be so?). This
167# would prevent connection db from starting a write-transaction. So roll the
168# db2 transaction back and replace it with a new read transaction.
169 execsql {
170 ROLLBACK;
171 BEGIN;
172 SELECT * FROM abc;
173 } db2
174
175 execsql {
176 INSERT INTO def VALUES('VII', 'VIII', 'IX');
177 }
178 concat [
179 catchsql { SELECT * FROM def; } db3
180 ] [
181 catchsql { SELECT * FROM def; } db2
182 ]
183} {0 {I II III IV V VI} 1 {database is locked}}
184do_test shared-2.4 {
185 # Commit the open transaction on db. db2 still holds a read-transaction.
186 # This should prevent db3 from writing to the database, but not from
187 # reading.
188 execsql {
189 COMMIT;
190 }
191 concat [
192 catchsql { SELECT * FROM def; } db3
193 ] [
194 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
195 ]
196} {0 {I II III IV V VI VII VIII IX} 1 {database is locked}}
197
198
199catch {db close}
200catch {db2 close}
201catch {db3 close}
202
203finish_test
204sqlite3_enable_shared_cache $::enable_shared_cache
205