xref: /sqlite-3.40.0/test/walnoshm.test (revision fda06bef)
1# 2010 November 1
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 that WAL databases may be accessed without
13# using the xShm primitives if the connection is in exclusive-mode.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18set testprefix walnoshm
19ifcapable !wal {finish_test ; return }
20
21db close
22testvfs tvfsshm
23testvfs tvfs -default 1 -iversion 1
24sqlite3 db test.db
25
26#--------------------------------------------------------------------------
27# Test that when using a version 1 VFS, a database can only be converted
28# to WAL mode after setting locking_mode=EXCLUSIVE. Also, test that if a
29# WAL database is opened using heap-memory for the WAL index, the connection
30# cannot change back to locking_mode=NORMAL while the database is still in
31# WAL mode.
32#
33do_execsql_test 1.1 {
34  CREATE TABLE t1(x, y);
35  INSERT INTO t1 VALUES(1, 2);
36}
37
38do_execsql_test 1.2 {
39  PRAGMA journal_mode = WAL;
40  SELECT * FROM t1;
41} {delete 1 2}
42do_test 1.3 { file exists test.db-wal } {0}
43
44do_execsql_test 1.4 {
45  PRAGMA locking_mode = exclusive;
46  PRAGMA journal_mode = WAL;
47  SELECT * FROM t1;
48} {exclusive wal 1 2}
49do_test 1.5 { file exists test.db-wal } {1}
50
51do_execsql_test 1.6 { INSERT INTO t1 VALUES(3, 4) }
52
53do_execsql_test 1.7 {
54  PRAGMA locking_mode = normal;
55} {exclusive}
56do_execsql_test 1.8 {
57  PRAGMA journal_mode = delete;
58  PRAGMA main.locking_mode;
59} {delete exclusive}
60do_execsql_test 1.9 {
61  PRAGMA locking_mode = normal;
62} {normal}
63do_execsql_test 1.10 {
64  SELECT * FROM t1;
65} {1 2 3 4}
66do_test 1.11 { file exists test.db-wal } {0}
67
68#-------------------------------------------------------------------------
69#
70# 2.1.*: Test that a connection using a version 1 VFS can open a WAL database
71#        and convert it to rollback mode if it is set to use
72#        locking_mode=exclusive.
73#
74# 2.2.*: Test that if the exclusive lock cannot be obtained while attempting
75#        the above, the operation fails and the WAL file is not opened.
76#
77do_execsql_test 2.1.1 {
78  CREATE TABLE t2(x, y);
79  INSERT INTO t2 VALUES('a', 'b');
80  INSERT INTO t2 VALUES('c', 'd');
81}
82do_execsql_test 2.1.2 {
83  PRAGMA locking_mode = exclusive;
84  PRAGMA journal_mode = WAL;
85  INSERT INTO t2 VALUES('e', 'f');
86  INSERT INTO t2 VALUES('g', 'h');
87} {exclusive wal}
88
89do_test 2.1.3 {
90  forcecopy test.db     test2.db
91  forcecopy test.db-wal test2.db-wal
92  sqlite3 db2 test2.db
93  catchsql { SELECT * FROM t2 } db2
94} {1 {unable to open database file}}
95do_test 2.1.4 {
96  catchsql { PRAGMA journal_mode = delete } db2
97} {1 {unable to open database file}}
98do_test 2.1.5 {
99  execsql {
100    PRAGMA locking_mode = exclusive;
101    PRAGMA journal_mode = delete;
102    SELECT * FROM t2;
103  } db2
104} {exclusive delete a b c d e f g h}
105
106do_test 2.2.1 {
107  forcecopy test.db     test2.db
108  forcecopy test.db-wal test2.db-wal
109  sqlite3 db3 test2.db -vfs tvfsshm
110  sqlite3 db2 test2.db
111  execsql { SELECT * FROM t2 } db3
112} {a b c d e f g h}
113
114do_test 2.2.2 {
115  execsql  { PRAGMA locking_mode = exclusive }  db2
116  catchsql { PRAGMA journal_mode = delete } db2
117} {1 {database is locked}}
118
119do_test 2.2.3 {
120  # This is to test that [db2] is not holding a PENDING lock (which can
121  # happen when an attempt to obtain an EXCLUSIVE lock fails).
122  sqlite3 db4 test2.db -vfs tvfsshm
123  execsql { SELECT * FROM t2 } db4
124} {a b c d e f g h}
125
126do_test 2.2.4 {
127  catchsql { SELECT * FROM t2 } db2
128} {1 {database is locked}}
129
130do_test 2.2.5 {
131  db4 close
132  sqlite3 db4 test2.db -vfs tvfsshm
133  execsql { SELECT * FROM t2 } db4
134} {a b c d e f g h}
135
136do_test 2.2.6 {
137  db3 close
138  db4 close
139  execsql { SELECT * FROM t2 } db2
140} {a b c d e f g h}
141
142db2 close
143db close
144
145#-------------------------------------------------------------------------
146#
147# 3.1: Test that if locking_mode=EXCLUSIVE is set after the wal file is
148#      opened, it is possible to drop back to locking_mode=NORMAL.
149#
150# 3.2: Test that if locking_mode=EXCLUSIVE is set before the wal file is
151#      opened, it is not.
152#
153do_test 3.1 {
154  sqlite3 db test.db -vfs tvfsshm
155  execsql {
156    SELECT * FROM t1;
157    PRAGMA locking_mode = EXCLUSIVE;
158    INSERT INTO t1 VALUES(5, 6);
159    PRAGMA locking_mode = NORMAL;
160    INSERT INTO t1 VALUES(7, 8);
161  }
162  sqlite3 db2 test.db -vfs tvfsshm
163  execsql { SELECT * FROM t1 } db2
164} {1 2 3 4 5 6 7 8}
165db close
166db2 close
167do_test 3.2 {
168  sqlite3 db  test.db -vfs tvfsshm
169  execsql {
170    PRAGMA locking_mode = EXCLUSIVE;
171    INSERT INTO t1 VALUES(9, 10);
172    PRAGMA locking_mode = NORMAL;
173    INSERT INTO t1 VALUES(11, 12);
174  }
175  sqlite3 db2 test.db -vfs tvfsshm
176  catchsql { SELECT * FROM t1 } db2
177} {1 {database is locked}}
178db close
179db2 close
180
181tvfs delete
182tvfsshm delete
183
184finish_test
185