dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 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. |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 14 | # |
| 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. |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 18 | # |
| 19 | |
| 20 | set testdir [file dirname $argv0] |
| 21 | source $testdir/tester.tcl |
| 22 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 23 | ifcapable !wal {finish_test ; return } |
| 24 | |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 25 | proc log_file_size {nFrame pgsz} { |
| 26 | expr {12 + ($pgsz+16)*$nFrame} |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 27 | } |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 28 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 29 | set ::wal_hook [list] |
| 30 | proc wal_hook {zDb nEntry} { |
| 31 | lappend ::wal_hook $zDb $nEntry |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 32 | return 0 |
| 33 | } |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 34 | db wal_hook wal_hook |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 35 | |
| 36 | do_test walhook-1.1 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 37 | execsql { |
| 38 | PRAGMA page_size = 1024; |
| 39 | PRAGMA journal_mode = wal; |
| 40 | PRAGMA synchronous = normal; |
| 41 | CREATE TABLE t1(i PRIMARY KEY, j); |
| 42 | } |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 43 | set ::wal_hook |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 44 | } {main 3} |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 45 | |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 46 | do_test walhook-1.2 { |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 47 | set ::wal_hook [list] |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 48 | execsql { INSERT INTO t1 VALUES(1, 'one') } |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 49 | set ::wal_hook |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 50 | } {main 5} |
| 51 | do_test walhook-1.3 { |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 52 | proc wal_hook {args} { return 1 } |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 53 | execsql { INSERT INTO t1 VALUES(2, 'two') } |
| 54 | file size test.db |
| 55 | } [expr 3*1024] |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 56 | do_test walhook-1.4 { |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 57 | proc wal_hook {zDb nEntry} { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 58 | execsql { PRAGMA wal_checkpoint } |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 59 | return 0 |
| 60 | } |
| 61 | execsql { CREATE TABLE t2(a, b) } |
| 62 | file size test.db |
| 63 | } [expr 4*1024] |
| 64 | |
| 65 | do_test walhook-1.5 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 66 | sqlite3 db2 test.db |
| 67 | proc wal_hook {zDb nEntry} { |
| 68 | execsql { PRAGMA wal_checkpoint } db2 |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 69 | return 0 |
| 70 | } |
| 71 | execsql { CREATE TABLE t3(a PRIMARY KEY, b) } |
| 72 | file size test.db |
| 73 | } [expr 6*1024] |
| 74 | |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 75 | db2 close |
| 76 | db close |
| 77 | sqlite3 db test.db |
| 78 | do_test walhook-2.1 { |
| 79 | execsql { PRAGMA synchronous = NORMAL } |
| 80 | execsql { PRAGMA wal_autocheckpoint } |
| 81 | } {1000} |
| 82 | do_test walhook-2.2 { |
| 83 | execsql { PRAGMA wal_autocheckpoint = 10} |
| 84 | } {10} |
| 85 | do_test walhook-2.3 { |
| 86 | execsql { PRAGMA wal_autocheckpoint } |
| 87 | } {10} |
| 88 | |
| 89 | # |
| 90 | # The database connection is configured with "PRAGMA wal_autocheckpoint = 10". |
| 91 | # Check that transactions are written to the log file until it contains at |
| 92 | # least 10 frames, then the database is checkpointed. Subsequent transactions |
| 93 | # are written into the start of the log file. |
| 94 | # |
| 95 | foreach {tn sql dbpages logpages} { |
| 96 | 4 "CREATE TABLE t4(x PRIMARY KEY, y)" 6 3 |
| 97 | 5 "INSERT INTO t4 VALUES(1, 'one')" 6 5 |
| 98 | 6 "INSERT INTO t4 VALUES(2, 'two')" 6 7 |
| 99 | 7 "INSERT INTO t4 VALUES(3, 'three')" 6 9 |
| 100 | 8 "INSERT INTO t4 VALUES(4, 'four')" 8 11 |
| 101 | 9 "INSERT INTO t4 VALUES(5, 'five')" 8 11 |
| 102 | } { |
| 103 | do_test walhook-2.$tn { |
| 104 | execsql $sql |
| 105 | list [file size test.db] [file size test.db-wal] |
| 106 | } [list [expr $dbpages*1024] [log_file_size $logpages 1024]] |
| 107 | } |
| 108 | |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 109 | catch { db2 close } |
| 110 | catch { db close } |
| 111 | finish_test |