blob: 631ec834e3873b57421cf6eb548ff5e929b6486e [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
dan10f5a502010-06-23 15:55:43 +000022source $testdir/wal_common.tcl
dan8d22a172010-04-19 18:03:51 +000023
dan5cf53532010-05-01 16:40:20 +000024ifcapable !wal {finish_test ; return }
25
drh833bf962010-04-28 14:42:19 +000026set ::wal_hook [list]
27proc wal_hook {zDb nEntry} {
28 lappend ::wal_hook $zDb $nEntry
dan8d22a172010-04-19 18:03:51 +000029 return 0
30}
dan5a299f92010-05-03 11:05:08 +000031db wal_hook wal_hook
dan8d22a172010-04-19 18:03:51 +000032
33do_test walhook-1.1 {
dan5a299f92010-05-03 11:05:08 +000034 execsql {
35 PRAGMA page_size = 1024;
dan65bddc12010-05-07 12:49:22 +000036 PRAGMA auto_vacuum = 0;
dan5a299f92010-05-03 11:05:08 +000037 PRAGMA journal_mode = wal;
38 PRAGMA synchronous = normal;
39 CREATE TABLE t1(i PRIMARY KEY, j);
40 }
drh833bf962010-04-28 14:42:19 +000041 set ::wal_hook
dan8d22a172010-04-19 18:03:51 +000042} {main 3}
dan5a299f92010-05-03 11:05:08 +000043
dan8d22a172010-04-19 18:03:51 +000044do_test walhook-1.2 {
drh833bf962010-04-28 14:42:19 +000045 set ::wal_hook [list]
dan8d22a172010-04-19 18:03:51 +000046 execsql { INSERT INTO t1 VALUES(1, 'one') }
drh833bf962010-04-28 14:42:19 +000047 set ::wal_hook
dan8d22a172010-04-19 18:03:51 +000048} {main 5}
49do_test walhook-1.3 {
drh5def0842010-05-05 20:00:25 +000050 proc wal_hook {args} { db eval {PRAGMA wal_checkpoint}; return 0 }
dan8d22a172010-04-19 18:03:51 +000051 execsql { INSERT INTO t1 VALUES(2, 'two') }
52 file size test.db
53} [expr 3*1024]
dan8d22a172010-04-19 18:03:51 +000054do_test walhook-1.4 {
drh833bf962010-04-28 14:42:19 +000055 proc wal_hook {zDb nEntry} {
dan5a299f92010-05-03 11:05:08 +000056 execsql { PRAGMA wal_checkpoint }
dan8d22a172010-04-19 18:03:51 +000057 return 0
58 }
59 execsql { CREATE TABLE t2(a, b) }
60 file size test.db
61} [expr 4*1024]
62
63do_test walhook-1.5 {
dan5a299f92010-05-03 11:05:08 +000064 sqlite3 db2 test.db
65 proc wal_hook {zDb nEntry} {
66 execsql { PRAGMA wal_checkpoint } db2
dan8d22a172010-04-19 18:03:51 +000067 return 0
68 }
69 execsql { CREATE TABLE t3(a PRIMARY KEY, b) }
70 file size test.db
71} [expr 6*1024]
72
dan5a299f92010-05-03 11:05:08 +000073db2 close
74db close
75sqlite3 db test.db
76do_test walhook-2.1 {
77 execsql { PRAGMA synchronous = NORMAL }
78 execsql { PRAGMA wal_autocheckpoint }
79} {1000}
80do_test walhook-2.2 {
81 execsql { PRAGMA wal_autocheckpoint = 10}
82} {10}
83do_test walhook-2.3 {
84 execsql { PRAGMA wal_autocheckpoint }
85} {10}
86
87#
88# The database connection is configured with "PRAGMA wal_autocheckpoint = 10".
89# Check that transactions are written to the log file until it contains at
90# least 10 frames, then the database is checkpointed. Subsequent transactions
91# are written into the start of the log file.
92#
93foreach {tn sql dbpages logpages} {
94 4 "CREATE TABLE t4(x PRIMARY KEY, y)" 6 3
95 5 "INSERT INTO t4 VALUES(1, 'one')" 6 5
96 6 "INSERT INTO t4 VALUES(2, 'two')" 6 7
97 7 "INSERT INTO t4 VALUES(3, 'three')" 6 9
98 8 "INSERT INTO t4 VALUES(4, 'four')" 8 11
99 9 "INSERT INTO t4 VALUES(5, 'five')" 8 11
100} {
101 do_test walhook-2.$tn {
102 execsql $sql
103 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000104 } [list [expr $dbpages*1024] [wal_file_size $logpages 1024]]
dan5a299f92010-05-03 11:05:08 +0000105}
106
dan8d22a172010-04-19 18:03:51 +0000107catch { db2 close }
108catch { db close }
109finish_test