xref: /sqlite-3.40.0/test/shared6.test (revision 856cc0fd)
1# 2009 April 01
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: shared6.test,v 1.2 2009/04/01 18:25:54 danielk1977 Exp $
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16ifcapable !shared_cache { finish_test ; return }
17
18do_test shared6-1.1.1 {
19  execsql {
20    CREATE TABLE t1(a, b);
21    CREATE TABLE t2(c, d);
22    CREATE TABLE t3(e, f);
23  }
24} {}
25do_test shared6-1.1.2 {
26  set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
27  sqlite3_enable_shared_cache
28} {1}
29
30do_test shared6-1.1.3 {
31  sqlite3 db1 test.db
32  sqlite3 db2 test.db
33} {}
34
35# Exclusive shared-cache locks. Test the following:
36#
37#   1.2.1: If [db1] has an exclusive lock, [db2] cannot read.
38#   1.2.2: If [db1] has an exclusive lock, [db1] can read.
39#   1.2.3: If [db1] has a non-exclusive write-lock, [db2] can read.
40#
41do_test shared6-1.2.1 {
42  execsql { SELECT * FROM t1 } db2    ;# Cache a compiled statement
43  execsql { BEGIN EXCLUSIVE } db1
44  catchsql { SELECT * FROM t1 } db2   ;# Execute the cached compiled statement
45} {1 {database table is locked}}
46do_test shared6-1.2.2 {
47  execsql { SELECT * FROM t1 } db1
48} {}
49do_test shared6-1.2.3 {
50  execsql {
51    COMMIT;
52    BEGIN;
53    INSERT INTO t2 VALUES(3, 4);
54  } db1
55  execsql { SELECT * FROM t1 } db2
56} {}
57do_test shared6-1.2.X {
58  execsql { COMMIT } db1
59} {}
60
61# Regular shared-cache locks. Verify the following:
62#
63#   1.3.1: If [db1] has a write-lock on t1, [db1] can read from t1.
64#   1.3.2: If [db1] has a write-lock on t1, [db2] can read from t2.
65#   1.3.3: If [db1] has a write-lock on t1, [db2] cannot read from t1.
66#   1.3.4: If [db1] has a write-lock on t1, [db2] cannot write to t1.
67#   1.3.5: If [db1] has a read-lock on t1, [db2] can read from t1.
68#   1.3.6: If [db1] has a read-lock on t1, [db2] cannot write to t1.
69#
70do_test shared6-1.3.1 {
71  execsql {
72    BEGIN;
73    INSERT INTO t1 VALUES(1, 2);
74  } db1
75  execsql { SELECT * FROM t1 } db1
76} {1 2}
77do_test shared6-1.3.2 {
78  execsql { SELECT * FROM t2 } db2
79} {3 4}
80do_test shared6-1.3.3 {
81  catchsql { SELECT * FROM t1 } db2
82} {1 {database table is locked: t1}}
83do_test shared6-1.3.4 {
84  catchsql { INSERT INTO t2 VALUES(1, 2) } db2
85} {1 {database table is locked}}
86do_test shared6-1.3.5 {
87  execsql {
88    COMMIT;
89    BEGIN;
90    SELECT * FROM t1;
91  } db1
92  execsql { SELECT * FROM t1 } db2
93} {1 2}
94do_test shared6-1.3.5 {
95  catchsql { INSERT INTO t1 VALUES(5, 6) } db2
96} {1 {database table is locked: t1}}
97do_test shared6-1.3.X {
98  execsql { COMMIT } db1
99} {}
100
101# Read-uncommitted mode.
102#
103# For these tests, connection [db2] is in read-uncommitted mode.
104#
105#   1.4.1: If [db1] has a write-lock on t1, [db2] can still read from t1.
106#   1.4.2: If [db1] has a write-lock on the db schema (sqlite_master table),
107#          [db2] cannot read from the schema.
108#   1.4.3: If [db1] has a read-lock on t1, [db2] cannot write to t1.
109#
110do_test shared6-1.4.1 {
111  execsql { PRAGMA read_uncommitted = 1 } db2
112  execsql {
113    BEGIN;
114    INSERT INTO t1 VALUES(5, 6);
115  } db1
116  execsql { SELECT * FROM t1 } db2
117} {1 2 5 6}
118do_test shared6-1.4.2 {
119  execsql { CREATE TABLE t4(a, b) } db1
120  catchsql { SELECT * FROM t1 } db2
121} {1 {database table is locked}}
122do_test shared6-1.4.3 {
123  execsql {
124    COMMIT;
125    BEGIN;
126    SELECT * FROM t1;
127  } db1
128  catchsql { INSERT INTO t1 VALUES(7, 8) } db2
129} {1 {database table is locked: t1}}
130
131do_test shared6-1.X {
132  db1 close
133  db2 close
134} {}
135
136# The following tests - shared6-2.* - test that two database connections
137# that connect to the same file using different VFS implementations do
138# not share a cache.
139if {$::tcl_platform(platform) eq "unix"} {
140  do_test shared6-2.1 {
141    sqlite3 db1 test.db -vfs unix
142    sqlite3 db2 test.db -vfs unix
143    sqlite3 db3 test.db -vfs unix-none
144    sqlite3 db4 test.db -vfs unix-none
145  } {}
146
147  do_test shared6-2.2 {
148    execsql { BEGIN; INSERT INTO t1 VALUES(9, 10); } db1
149    catchsql { SELECT * FROM t1 } db2
150  } {1 {database table is locked: t1}}
151  do_test shared6-2.3 {
152    execsql { SELECT * FROM t1 } db3
153  } {1 2 5 6}
154
155  do_test shared6-2.3 {
156    execsql { COMMIT } db1
157    execsql { BEGIN; INSERT INTO t1 VALUES(11, 12); } db3
158    catchsql { SELECT * FROM t1 } db4
159  } {1 {database table is locked: t1}}
160
161  do_test shared6-2.4 {
162    execsql { SELECT * FROM t1 } db1
163  } {1 2 5 6 9 10}
164
165  do_test shared6-2.5 {
166    execsql { COMMIT } db3
167  } {}
168
169  do_test shared6-2.X {
170    db1 close
171    db2 close
172    db3 close
173    db4 close
174  } {}
175}
176
177sqlite3_enable_shared_cache $::enable_shared_cache
178finish_test
179
180