blob: 8e9c4644d154f090dd804b4979cb6b7010a667a2 [file] [log] [blame]
dan661d71a2011-03-30 19:08:03 +00001# 2011 March 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# This file contains tests for the "unix-excl" VFS module (part of
13# os_unix.c).
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18source $testdir/lock_common.tcl
19source $testdir/malloc_common.tcl
20
21if {$::tcl_platform(platform)!="unix" || [info commands test_syscall]==""} {
22 finish_test
23 return
24}
25set testprefix unixexcl
26
27
28
29# Test that when using VFS "unix-excl", the first time the database is read
30# a process-wide exclusive lock is taken on it. This means other connections
31# within the process may still access the db normally, but connections from
32# outside the process cannot.
33#
34do_multiclient_test tn {
35 do_test unixexcl-1.$tn.1 {
36 sql1 {
37 CREATE TABLE t1(a, b);
38 INSERT INTO t1 VALUES('hello', 'world');
39 }
40 } {}
41 do_test unixexcl-1.$tn.2 { sql2 { SELECT * FROM t1 } } {hello world}
42 do_test unixexcl-1.$tn.3 {
43 code1 {
44 db close
45 sqlite3 db test.db -vfs unix-excl
46 db eval { SELECT * FROM t1 }
47 }
48 } {hello world}
49 if {$tn==1} {
50 do_test unixexcl-1.$tn.4.multiproc {
51 csql2 { SELECT * FROM t1 }
52 } {1 {database is locked}}
53 } else {
54 do_test unixexcl-1.$tn.4.singleproc {
55 csql2 { SELECT * FROM t1 }
56 } {0 {hello world}}
57 }
58}
59
60# Test that when using VFS "unix-excl", if a file is opened in read-only mode
61# the behaviour is the same as if VFS "unix" were used.
62#
63do_multiclient_test tn {
64 do_test unixexcl-2.$tn.1 {
65 sql1 {
66 CREATE TABLE t1(a, b);
67 INSERT INTO t1 VALUES('hello', 'world');
68 }
69 } {}
70 do_test unixexcl-2.$tn.2 { sql2 { SELECT * FROM t1 } } {hello world}
71 do_test unixexcl-2.$tn.3 {
72 code1 {
73 db close
74 sqlite3 db test.db -readonly yes -vfs unix-excl
75 db eval { SELECT * FROM t1 }
76 }
77 } {hello world}
78 do_test unixexcl-2.$tn.4 {
79 csql2 { SELECT * FROM t1 }
80 } {0 {hello world}}
81}
82
dan19969d92011-12-19 15:46:51 +000083do_multiclient_test tn {
84 do_test unixexcl-3.$tn.1 {
drhcc8d10a2011-12-23 02:07:10 +000085 code1 { db close; sqlite3 db file:test.db?psow=0 -vfs unix-excl -uri 1 }
86 code2 { db2 close; sqlite3 db2 file:test.db?psow=0 -vfs unix-excl -uri 1 }
dan19969d92011-12-19 15:46:51 +000087 sql1 {
dan5d8341a2012-01-12 16:41:30 +000088 PRAGMA auto_vacuum = 0;
dan19969d92011-12-19 15:46:51 +000089 PRAGMA journal_mode = WAL;
drhe243de52016-03-08 15:14:26 +000090 PRAGMA synchronous = FULL;
dan19969d92011-12-19 15:46:51 +000091 CREATE TABLE t1(a, b);
92 INSERT INTO t1 VALUES(1, 2);
93 }
94 } {wal}
95
96 if {$tn==1} {
97 do_test unixexcl-3.$tn.1.multiproc {
98 csql2 { SELECT * FROM t1; }
99 } {1 {database is locked}}
100 } else {
101 do_test unixexcl-3.$tn.1.singleproc {
102 sql2 { SELECT * FROM t1; }
103 } {1 2}
104
105 do_test unixexcl-3.$tn.2 {
106 sql2 {
107 BEGIN;
108 SELECT * FROM t1;
109 }
110 } {1 2}
111 do_test unixexcl-3.$tn.3 {
112 sql1 { PRAGMA wal_checkpoint; INSERT INTO t1 VALUES(3, 4); }
dandd973542014-02-13 19:27:08 +0000113 } {0 5 5}
dan19969d92011-12-19 15:46:51 +0000114 do_test unixexcl-3.$tn.4 {
115 sql2 { SELECT * FROM t1; }
116 } {1 2}
117 do_test unixexcl-3.$tn.5 {
118 sql1 { SELECT * FROM t1; }
119 } {1 2 3 4}
120 do_test unixexcl-3.$tn.6 {
121 sql2 { COMMIT; SELECT * FROM t1; }
122 } {1 2 3 4}
123 do_test unixexcl-3.$tn.7 {
124 sql1 { PRAGMA wal_checkpoint; }
dandd973542014-02-13 19:27:08 +0000125 } {0 7 7}
dan19969d92011-12-19 15:46:51 +0000126 }
127}
128
dan661d71a2011-03-30 19:08:03 +0000129finish_test