1# 2010 April 19 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 the operation of the library in 13# "PRAGMA journal_mode=WAL" mode. 14# 15 16set testdir [file dirname $argv0] 17source $testdir/tester.tcl 18 19do_test walmode-1.1 { 20 set sqlite_sync_count 0 21 execsql { PRAGMA page_size = 1024 } 22 execsql { PRAGMA journal_mode = wal } 23} {wal} 24do_test walmode-1.2 { 25 file size test.db 26} {1024} 27do_test walmode-1.3 { 28 set sqlite_sync_count 29} {4} 30do_test walmode-1.4 { 31 file exists test.db-wal 32} {0} 33do_test walmode-1.5 { 34 execsql { CREATE TABLE t1(a, b) } 35 file size test.db 36} {1024} 37do_test walmode-1.6 { 38 file exists test.db-wal 39} {1} 40do_test walmode-1.7 { 41 db close 42 file exists test.db-wal 43} {0} 44 45# There is now a database file with the read and write versions set to 2 46# in the file system. This file should default to WAL mode. 47# 48do_test walmode-2.1 { 49 sqlite3 db test.db 50 file exists test.db-wal 51} {0} 52do_test walmode-2.2 { 53 execsql { SELECT * FROM sqlite_master } 54 file exists test.db-wal 55} {1} 56do_test walmode-2.3 { 57 db close 58 file exists test.db-wal 59} {0} 60 61# If the first statement executed is "PRAGMA journal_mode = wal", and 62# the file is already configured for WAL (read and write versions set 63# to 2), then there should be no need to write the database. The 64# statement should cause the client to connect to the log file. 65# 66set sqlite_sync_count 0 67do_test walmode-3.1 { 68 sqlite3 db test.db 69 execsql { PRAGMA journal_mode = wal } 70} {wal} 71do_test walmode-3.2 { 72 list $sqlite_sync_count [file exists test.db-wal] [file size test.db-wal] 73} {0 1 0} 74 75# Test that changing back to journal_mode=persist works. 76# 77do_test walmode-4.1 { 78 execsql { INSERT INTO t1 VALUES(1, 2) } 79 execsql { PRAGMA journal_mode = persist } 80} {persist} 81do_test walmode-4.2 { 82 list [file exists test.db-journal] [file exists test.db-wal] 83} {1 0} 84do_test walmode-4.3 { 85 execsql { SELECT * FROM t1 } 86} {1 2} 87do_test walmode-4.4 { 88 db close 89 sqlite3 db test.db 90 execsql { SELECT * FROM t1 } 91} {1 2} 92do_test walmode-4.5 { 93 list [file exists test.db-journal] [file exists test.db-wal] 94} {1 0} 95 96# Test that nothing goes wrong if a connection is prevented from changing 97# from WAL to rollback mode because a second connection has the database 98# open. Or from rollback to WAL. 99# 100do_test walmode-4.1 { 101 sqlite3 db2 test.db 102 execsql { PRAGMA main.journal_mode } db2 103} {delete} 104do_test walmode-4.2 { 105 execsql { PRAGMA main.journal_mode = wal } db 106} {wal} 107do_test walmode-4.3 { 108 execsql { SELECT * FROM t1 } db2 109} {1 2} 110do_test walmode-4.4 { 111 catchsql { PRAGMA journal_mode = delete } db 112} {1 {database is locked}} 113do_test walmode-4.5 { 114 execsql { PRAGMA main.journal_mode } db 115} {wal} 116do_test walmode-4.6 { 117 db2 close 118 execsql { PRAGMA journal_mode = delete } db 119} {delete} 120do_test walmode-4.7 { 121 execsql { PRAGMA main.journal_mode } db 122} {delete} 123do_test walmode-4.8 { 124 list [file exists test.db-journal] [file exists test.db-wal] 125} {0 0} 126do_test walmode-4.9 { 127 sqlite3 db2 test.db 128 execsql { 129 BEGIN; 130 SELECT * FROM t1; 131 } db2 132} {1 2} 133do_test walmode-4.11 { 134 execsql { PRAGMA main.journal_mode } db 135} {delete} 136do_test walmode-4.10 { 137 catchsql { PRAGMA main.journal_mode = wal } db 138} {1 {database is locked}} 139do_test walmode-4.11 { 140 execsql { PRAGMA main.journal_mode } db 141} {delete} 142 143 144catch { db close } 145catch { db2 close } 146finish_test 147 148