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 |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame^] | 22 | source $testdir/wal_common.tcl |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 23 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 24 | ifcapable !wal {finish_test ; return } |
| 25 | |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 26 | set ::wal_hook [list] |
| 27 | proc wal_hook {zDb nEntry} { |
| 28 | lappend ::wal_hook $zDb $nEntry |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 29 | return 0 |
| 30 | } |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 31 | db wal_hook wal_hook |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 32 | |
| 33 | do_test walhook-1.1 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 34 | execsql { |
| 35 | PRAGMA page_size = 1024; |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 36 | PRAGMA auto_vacuum = 0; |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 37 | PRAGMA journal_mode = wal; |
| 38 | PRAGMA synchronous = normal; |
| 39 | CREATE TABLE t1(i PRIMARY KEY, j); |
| 40 | } |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 41 | set ::wal_hook |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 42 | } {main 3} |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 43 | |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 44 | do_test walhook-1.2 { |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 45 | set ::wal_hook [list] |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 46 | execsql { INSERT INTO t1 VALUES(1, 'one') } |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 47 | set ::wal_hook |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 48 | } {main 5} |
| 49 | do_test walhook-1.3 { |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 50 | proc wal_hook {args} { db eval {PRAGMA wal_checkpoint}; return 0 } |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 51 | execsql { INSERT INTO t1 VALUES(2, 'two') } |
| 52 | file size test.db |
| 53 | } [expr 3*1024] |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 54 | do_test walhook-1.4 { |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 55 | proc wal_hook {zDb nEntry} { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 56 | execsql { PRAGMA wal_checkpoint } |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 57 | return 0 |
| 58 | } |
| 59 | execsql { CREATE TABLE t2(a, b) } |
| 60 | file size test.db |
| 61 | } [expr 4*1024] |
| 62 | |
| 63 | do_test walhook-1.5 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 64 | sqlite3 db2 test.db |
| 65 | proc wal_hook {zDb nEntry} { |
| 66 | execsql { PRAGMA wal_checkpoint } db2 |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 67 | return 0 |
| 68 | } |
| 69 | execsql { CREATE TABLE t3(a PRIMARY KEY, b) } |
| 70 | file size test.db |
| 71 | } [expr 6*1024] |
| 72 | |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 73 | db2 close |
| 74 | db close |
| 75 | sqlite3 db test.db |
| 76 | do_test walhook-2.1 { |
| 77 | execsql { PRAGMA synchronous = NORMAL } |
| 78 | execsql { PRAGMA wal_autocheckpoint } |
| 79 | } {1000} |
| 80 | do_test walhook-2.2 { |
| 81 | execsql { PRAGMA wal_autocheckpoint = 10} |
| 82 | } {10} |
| 83 | do_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 | # |
| 93 | foreach {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] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame^] | 104 | } [list [expr $dbpages*1024] [wal_file_size $logpages 1024]] |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 105 | } |
| 106 | |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 107 | catch { db2 close } |
| 108 | catch { db close } |
| 109 | finish_test |