blob: cda57ce470cd1b1351db47c4d026ff84824d716b [file] [log] [blame]
dan8d22a172010-04-19 18:03:51 +00001# 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.
dan5a299f92010-05-03 11:05:08 +000014#
15# More specifically, this file contains regression tests for the
16# sqlite3_wal_hook() mechanism, including the sqlite3_wal_autocheckpoint()
17# and "PRAGMA wal_autocheckpoint" convenience interfaces.
dan8d22a172010-04-19 18:03:51 +000018#
19
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
dan5cf53532010-05-01 16:40:20 +000023ifcapable !wal {finish_test ; return }
24
dan5a299f92010-05-03 11:05:08 +000025proc log_file_size {nFrame pgsz} {
26 expr {12 + ($pgsz+16)*$nFrame}
dan8d22a172010-04-19 18:03:51 +000027}
dan8d22a172010-04-19 18:03:51 +000028
drh833bf962010-04-28 14:42:19 +000029set ::wal_hook [list]
30proc wal_hook {zDb nEntry} {
31 lappend ::wal_hook $zDb $nEntry
dan8d22a172010-04-19 18:03:51 +000032 return 0
33}
dan5a299f92010-05-03 11:05:08 +000034db wal_hook wal_hook
dan8d22a172010-04-19 18:03:51 +000035
36do_test walhook-1.1 {
dan5a299f92010-05-03 11:05:08 +000037 execsql {
38 PRAGMA page_size = 1024;
dan65bddc12010-05-07 12:49:22 +000039 PRAGMA auto_vacuum = 0;
dan5a299f92010-05-03 11:05:08 +000040 PRAGMA journal_mode = wal;
41 PRAGMA synchronous = normal;
42 CREATE TABLE t1(i PRIMARY KEY, j);
43 }
drh833bf962010-04-28 14:42:19 +000044 set ::wal_hook
dan8d22a172010-04-19 18:03:51 +000045} {main 3}
dan5a299f92010-05-03 11:05:08 +000046
dan8d22a172010-04-19 18:03:51 +000047do_test walhook-1.2 {
drh833bf962010-04-28 14:42:19 +000048 set ::wal_hook [list]
dan8d22a172010-04-19 18:03:51 +000049 execsql { INSERT INTO t1 VALUES(1, 'one') }
drh833bf962010-04-28 14:42:19 +000050 set ::wal_hook
dan8d22a172010-04-19 18:03:51 +000051} {main 5}
52do_test walhook-1.3 {
drh5def0842010-05-05 20:00:25 +000053 proc wal_hook {args} { db eval {PRAGMA wal_checkpoint}; return 0 }
dan8d22a172010-04-19 18:03:51 +000054 execsql { INSERT INTO t1 VALUES(2, 'two') }
55 file size test.db
56} [expr 3*1024]
dan8d22a172010-04-19 18:03:51 +000057do_test walhook-1.4 {
drh833bf962010-04-28 14:42:19 +000058 proc wal_hook {zDb nEntry} {
dan5a299f92010-05-03 11:05:08 +000059 execsql { PRAGMA wal_checkpoint }
dan8d22a172010-04-19 18:03:51 +000060 return 0
61 }
62 execsql { CREATE TABLE t2(a, b) }
63 file size test.db
64} [expr 4*1024]
65
66do_test walhook-1.5 {
dan5a299f92010-05-03 11:05:08 +000067 sqlite3 db2 test.db
68 proc wal_hook {zDb nEntry} {
69 execsql { PRAGMA wal_checkpoint } db2
dan8d22a172010-04-19 18:03:51 +000070 return 0
71 }
72 execsql { CREATE TABLE t3(a PRIMARY KEY, b) }
73 file size test.db
74} [expr 6*1024]
75
dan5a299f92010-05-03 11:05:08 +000076db2 close
77db close
78sqlite3 db test.db
79do_test walhook-2.1 {
80 execsql { PRAGMA synchronous = NORMAL }
81 execsql { PRAGMA wal_autocheckpoint }
82} {1000}
83do_test walhook-2.2 {
84 execsql { PRAGMA wal_autocheckpoint = 10}
85} {10}
86do_test walhook-2.3 {
87 execsql { PRAGMA wal_autocheckpoint }
88} {10}
89
90#
91# The database connection is configured with "PRAGMA wal_autocheckpoint = 10".
92# Check that transactions are written to the log file until it contains at
93# least 10 frames, then the database is checkpointed. Subsequent transactions
94# are written into the start of the log file.
95#
96foreach {tn sql dbpages logpages} {
97 4 "CREATE TABLE t4(x PRIMARY KEY, y)" 6 3
98 5 "INSERT INTO t4 VALUES(1, 'one')" 6 5
99 6 "INSERT INTO t4 VALUES(2, 'two')" 6 7
100 7 "INSERT INTO t4 VALUES(3, 'three')" 6 9
101 8 "INSERT INTO t4 VALUES(4, 'four')" 8 11
102 9 "INSERT INTO t4 VALUES(5, 'five')" 8 11
103} {
104 do_test walhook-2.$tn {
105 execsql $sql
106 list [file size test.db] [file size test.db-wal]
107 } [list [expr $dbpages*1024] [log_file_size $logpages 1024]]
108}
109
dan8d22a172010-04-19 18:03:51 +0000110catch { db2 close }
111catch { db close }
112finish_test