summaryrefslogtreecommitdiff
path: root/test/wal6.test
blob: 2498907ea908549f9ecc636b0ba1ba3170565fa3 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 2010 December 1
#
# 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 implements regression tests for SQLite library.  The
# focus of this file is testing the operation of the library in
# "PRAGMA journal_mode=WAL" mode.
#

set testdir [file dirname $argv0]
set testprefix wal6
source $testdir/tester.tcl
source $testdir/lock_common.tcl
source $testdir/wal_common.tcl
source $testdir/malloc_common.tcl
ifcapable !wal {finish_test ; return }

#-------------------------------------------------------------------------
# Changing to WAL mode in one connection forces the change in others.
#
db close
forcedelete test.db

set all_journal_modes {delete persist truncate memory off}
foreach jmode $all_journal_modes {

  do_test wal6-1.0.$jmode {
    sqlite3 db test.db
    execsql "PRAGMA journal_mode = $jmode;"
  } $jmode

  do_test wal6-1.1.$jmode {
    execsql {
      CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
      INSERT INTO t1 VALUES(1,2);
      SELECT * FROM t1;
    }
  } {1 2}

# Under Windows, you'll get an error trying to delete
# a file this is already opened.  Close the first connection
# so the other tests work.
if {$tcl_platform(platform)=="windows"} {
  if {$jmode=="persist" || $jmode=="truncate"} {
    db close
  }
}

  do_test wal6-1.2.$jmode {
    sqlite3 db2 test.db
    execsql {
    PRAGMA journal_mode=WAL;
    INSERT INTO t1 VALUES(3,4);
    SELECT * FROM t1 ORDER BY a;
    } db2
  } {wal 1 2 3 4}

if {$tcl_platform(platform)=="windows"} {
  if {$jmode=="persist" || $jmode=="truncate"} {
    sqlite3 db test.db
  }
}

  do_test wal6-1.3.$jmode {
    execsql {
      SELECT * FROM t1 ORDER BY a;
    }
  } {1 2 3 4}

  db close
  db2 close
  forcedelete test.db

}

#-------------------------------------------------------------------------
# Test that SQLITE_BUSY_SNAPSHOT is returned as expected.
#
reset_db
sqlite3 db2 test.db

do_execsql_test 2.1 {
  PRAGMA journal_mode = WAL;
  CREATE TABLE t1(a PRIMARY KEY, b TEXT);
  INSERT INTO t1 VALUES(1, 'one');
  INSERT INTO t1 VALUES(2, 'two');
  BEGIN;
    SELECT * FROM t1;
} {wal 1 one 2 two}

do_test 2.2 {
  execsql {
    SELECT * FROM t1;
    INSERT INTO t1 VALUES(3, 'three');
  } db2
} {1 one 2 two}

do_catchsql_test 2.3 { 
  INSERT INTO t1 VALUES('x', 'x') 
} {1 {database is locked}}

do_test 2.4 { 
  list [sqlite3_errcode db] [sqlite3_extended_errcode db]
} {SQLITE_BUSY SQLITE_BUSY_SNAPSHOT}

do_execsql_test 2.5 {
  SELECT * FROM t1;
  COMMIT;
  INSERT INTO t1 VALUES('x', 'x') 
} {1 one 2 two}

proc test3 {prefix} {
  do_test $prefix.1 {
    execsql { SELECT count(*) FROM t1 } 
  } {0}
  do_test $prefix.2 {
    execsql { INSERT INTO t1 VALUES('x', 'x') } db2
  } {}
  do_test $prefix.3 {
    execsql { INSERT INTO t1 VALUES('y', 'y') }
  } {}
  do_test $prefix.4 {
    execsql { SELECT count(*) FROM t1 } 
  } {2}
}

do_execsql_test 2.6.1 { DELETE FROM t1 }
test3 2.6.2

db func test3 test3
do_execsql_test 2.6.3 { DELETE FROM t1 }
db eval {SELECT test3('2.6.4')}

do_test 2.x {
  db2 close
} {}

#-------------------------------------------------------------------------
# Check that if BEGIN IMMEDIATE fails, it does not leave the user with
# an open read-transaction (unless one was already open before the BEGIN 
# IMMEDIATE). Even if there are other active VMs.
#

proc test4 {prefix} {
  do_test $prefix.1 {
    catchsql { BEGIN IMMEDIATE }
  } {1 {database is locked}}

  do_test $prefix.2 {
    execsql { COMMIT } db2
  } {}

  do_test $prefix.3 {
    execsql { BEGIN IMMEDIATE }
  } {}
  do_test $prefix.4 {
    execsql { COMMIT }
  } {}
}

reset_db
sqlite3 db2 test.db
do_execsql_test 3.1 { 
  PRAGMA journal_mode = WAL;
  CREATE TABLE ab(a PRIMARY KEY, b);
} {wal}

do_test 3.2.1 { 
  execsql { 
    BEGIN;
      INSERT INTO ab VALUES(1, 2);
  } db2
} {}
test4 3.2.2

db func test4 test4
do_test 3.3.1 {
  execsql { 
    BEGIN;
      INSERT INTO ab VALUES(3, 4);
  } db2
} {}

db eval {SELECT test4('3.3.2')}

do_test 3.x {
  db2 close
} {}

finish_test