18d22a174Sdan# 2010 April 19 28d22a174Sdan# 38d22a174Sdan# The author disclaims copyright to this source code. In place of 48d22a174Sdan# a legal notice, here is a blessing: 58d22a174Sdan# 68d22a174Sdan# May you do good and not evil. 78d22a174Sdan# May you find forgiveness for yourself and forgive others. 88d22a174Sdan# May you share freely, never taking more than you give. 98d22a174Sdan# 108d22a174Sdan#*********************************************************************** 118d22a174Sdan# This file implements regression tests for SQLite library. The 128d22a174Sdan# focus of this file is testing the operation of the library in 138d22a174Sdan# "PRAGMA journal_mode=WAL" mode. 148d22a174Sdan# 155a299f91Sdan# More specifically, this file contains regression tests for the 165a299f91Sdan# sqlite3_wal_hook() mechanism, including the sqlite3_wal_autocheckpoint() 175a299f91Sdan# and "PRAGMA wal_autocheckpoint" convenience interfaces. 185a299f91Sdan# 198d22a174Sdan 208d22a174Sdanset testdir [file dirname $argv0] 218d22a174Sdansource $testdir/tester.tcl 22*10f5a50eSdansource $testdir/wal_common.tcl 238d22a174Sdan 245cf53537Sdanifcapable !wal {finish_test ; return } 255cf53537Sdan 26833bf968Sdrhset ::wal_hook [list] 27833bf968Sdrhproc wal_hook {zDb nEntry} { 28833bf968Sdrh lappend ::wal_hook $zDb $nEntry 298d22a174Sdan return 0 308d22a174Sdan} 315a299f91Sdandb wal_hook wal_hook 328d22a174Sdan 338d22a174Sdando_test walhook-1.1 { 345a299f91Sdan execsql { 355a299f91Sdan PRAGMA page_size = 1024; 3665bddc12Sdan PRAGMA auto_vacuum = 0; 375a299f91Sdan PRAGMA journal_mode = wal; 385a299f91Sdan PRAGMA synchronous = normal; 395a299f91Sdan CREATE TABLE t1(i PRIMARY KEY, j); 405a299f91Sdan } 41833bf968Sdrh set ::wal_hook 428d22a174Sdan} {main 3} 435a299f91Sdan 448d22a174Sdando_test walhook-1.2 { 45833bf968Sdrh set ::wal_hook [list] 468d22a174Sdan execsql { INSERT INTO t1 VALUES(1, 'one') } 47833bf968Sdrh set ::wal_hook 488d22a174Sdan} {main 5} 498d22a174Sdando_test walhook-1.3 { 505def0843Sdrh proc wal_hook {args} { db eval {PRAGMA wal_checkpoint}; return 0 } 518d22a174Sdan execsql { INSERT INTO t1 VALUES(2, 'two') } 528d22a174Sdan file size test.db 538d22a174Sdan} [expr 3*1024] 548d22a174Sdando_test walhook-1.4 { 55833bf968Sdrh proc wal_hook {zDb nEntry} { 565a299f91Sdan execsql { PRAGMA wal_checkpoint } 578d22a174Sdan return 0 588d22a174Sdan } 598d22a174Sdan execsql { CREATE TABLE t2(a, b) } 608d22a174Sdan file size test.db 618d22a174Sdan} [expr 4*1024] 628d22a174Sdan 638d22a174Sdando_test walhook-1.5 { 645a299f91Sdan sqlite3 db2 test.db 65833bf968Sdrh proc wal_hook {zDb nEntry} { 665a299f91Sdan execsql { PRAGMA wal_checkpoint } db2 678d22a174Sdan return 0 688d22a174Sdan } 698d22a174Sdan execsql { CREATE TABLE t3(a PRIMARY KEY, b) } 708d22a174Sdan file size test.db 718d22a174Sdan} [expr 6*1024] 728d22a174Sdan 735a299f91Sdandb2 close 745a299f91Sdandb close 755a299f91Sdansqlite3 db test.db 765a299f91Sdando_test walhook-2.1 { 775a299f91Sdan execsql { PRAGMA synchronous = NORMAL } 785a299f91Sdan execsql { PRAGMA wal_autocheckpoint } 795a299f91Sdan} {1000} 805a299f91Sdando_test walhook-2.2 { 815a299f91Sdan execsql { PRAGMA wal_autocheckpoint = 10} 825a299f91Sdan} {10} 835a299f91Sdando_test walhook-2.3 { 845a299f91Sdan execsql { PRAGMA wal_autocheckpoint } 855a299f91Sdan} {10} 865a299f91Sdan 875a299f91Sdan# 885a299f91Sdan# The database connection is configured with "PRAGMA wal_autocheckpoint = 10". 895a299f91Sdan# Check that transactions are written to the log file until it contains at 905a299f91Sdan# least 10 frames, then the database is checkpointed. Subsequent transactions 915a299f91Sdan# are written into the start of the log file. 925a299f91Sdan# 935a299f91Sdanforeach {tn sql dbpages logpages} { 945a299f91Sdan 4 "CREATE TABLE t4(x PRIMARY KEY, y)" 6 3 955a299f91Sdan 5 "INSERT INTO t4 VALUES(1, 'one')" 6 5 965a299f91Sdan 6 "INSERT INTO t4 VALUES(2, 'two')" 6 7 975a299f91Sdan 7 "INSERT INTO t4 VALUES(3, 'three')" 6 9 985a299f91Sdan 8 "INSERT INTO t4 VALUES(4, 'four')" 8 11 995a299f91Sdan 9 "INSERT INTO t4 VALUES(5, 'five')" 8 11 1005a299f91Sdan} { 1015a299f91Sdan do_test walhook-2.$tn { 1025a299f91Sdan execsql $sql 1035a299f91Sdan list [file size test.db] [file size test.db-wal] 104*10f5a50eSdan } [list [expr $dbpages*1024] [wal_file_size $logpages 1024]] 1055a299f91Sdan} 1065a299f91Sdan 1078d22a174Sdancatch { db2 close } 1088d22a174Sdancatch { db close } 1098d22a174Sdanfinish_test 110