summaryrefslogtreecommitdiff
path: root/test/unixexcl.test
blob: d6762178dd2eb90f2c27e8bd9e683c969a1a6cc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 2011 March 30
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file contains tests for the "unix-excl" VFS module (part of 
# os_unix.c).
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/lock_common.tcl
source $testdir/malloc_common.tcl

if {$::tcl_platform(platform)!="unix" || [info commands test_syscall]==""} {
  finish_test
  return
} 
set testprefix unixexcl



# Test that when using VFS "unix-excl", the first time the database is read
# a process-wide exclusive lock is taken on it. This means other connections
# within the process may still access the db normally, but connections from
# outside the process cannot.
#
do_multiclient_test tn {
  do_test unixexcl-1.$tn.1 {
    sql1 {
      CREATE TABLE t1(a, b);
      INSERT INTO t1 VALUES('hello', 'world');
    }
  } {}
  do_test unixexcl-1.$tn.2 { sql2 { SELECT * FROM t1 } } {hello world}
  do_test unixexcl-1.$tn.3 {
    code1 {
      db close
      sqlite3 db test.db -vfs unix-excl
      db eval { SELECT * FROM t1 }
    }
  } {hello world}
  if {$tn==1} {
    do_test unixexcl-1.$tn.4.multiproc { 
      csql2 { SELECT * FROM t1 } 
    } {1 {database is locked}}
  } else {
    do_test unixexcl-1.$tn.4.singleproc { 
      csql2 { SELECT * FROM t1 } 
    } {0 {hello world}}
  }
}

# Test that when using VFS "unix-excl", if a file is opened in read-only mode
# the behaviour is the same as if VFS "unix" were used.
#
do_multiclient_test tn {
  do_test unixexcl-2.$tn.1 {
    sql1 {
      CREATE TABLE t1(a, b);
      INSERT INTO t1 VALUES('hello', 'world');
    }
  } {}
  do_test unixexcl-2.$tn.2 { sql2 { SELECT * FROM t1 } } {hello world}
  do_test unixexcl-2.$tn.3 {
    code1 {
      db close
      sqlite3 db test.db -readonly yes -vfs unix-excl
      db eval { SELECT * FROM t1 }
    }
  } {hello world}
  do_test unixexcl-2.$tn.4 { 
    csql2 { SELECT * FROM t1 } 
  } {0 {hello world}}
}

do_multiclient_test tn {
  do_test unixexcl-3.$tn.1 {
    code1 { db close; sqlite3 db file:test.db?psow=0 -vfs unix-excl -uri 1 }
    code2 { db2 close; sqlite3 db2 file:test.db?psow=0 -vfs unix-excl -uri 1 }
    sql1 {
      PRAGMA auto_vacuum = 0;
      PRAGMA journal_mode = WAL;
      CREATE TABLE t1(a, b);
      INSERT INTO t1 VALUES(1, 2);
    }
  } {wal}

  if {$tn==1} {
    do_test unixexcl-3.$tn.1.multiproc {
      csql2 { SELECT * FROM t1; }
    } {1 {database is locked}}
  } else {
    do_test unixexcl-3.$tn.1.singleproc {
      sql2 { SELECT * FROM t1; }
    } {1 2}

    do_test unixexcl-3.$tn.2 {
      sql2 { 
        BEGIN;
          SELECT * FROM t1;
      }
    } {1 2}
    do_test unixexcl-3.$tn.3 {
      sql1 { PRAGMA wal_checkpoint; INSERT INTO t1 VALUES(3, 4); }
    } {0 5 5}
    do_test unixexcl-3.$tn.4 {
      sql2 { SELECT * FROM t1; }
    } {1 2}
    do_test unixexcl-3.$tn.5 {
      sql1 { SELECT * FROM t1; }
    } {1 2 3 4}
    do_test unixexcl-3.$tn.6 {
      sql2 { COMMIT; SELECT * FROM t1; }
    } {1 2 3 4}
    do_test unixexcl-3.$tn.7 {
      sql1 { PRAGMA wal_checkpoint; }
    } {0 7 7}
  }
}

finish_test