dan | 7fb8990 | 2016-08-12 16:21:15 +0000 | [diff] [blame] | 1 | # 2016 Aug 12 |
| 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 script is using the sqlite_interrupt() API to |
| 13 | # interrupt WAL checkpoint operations. |
| 14 | # |
| 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | source $testdir/wal_common.tcl |
| 19 | set testprefix interrupt2 |
| 20 | |
| 21 | db close |
| 22 | testvfs tvfs -default 1 |
| 23 | |
| 24 | tvfs filter xWrite |
| 25 | tvfs script write_cb |
| 26 | |
| 27 | set ::trigger_interrupt 0 |
| 28 | proc write_cb {method args} { |
| 29 | set filename [lindex $args 0] |
| 30 | if {[file tail $filename]=="test.db" && $::trigger_interrupt} { |
| 31 | if {$::trigger_interrupt} { |
| 32 | incr ::trigger_interrupt -1 |
| 33 | if {$::trigger_interrupt==0} { sqlite3_interrupt db } |
| 34 | } |
| 35 | } |
| 36 | return 0 |
| 37 | } |
| 38 | |
| 39 | sqlite3 db test.db |
| 40 | do_execsql_test 1.0 { |
| 41 | CREATE TABLE t1(a, b); |
| 42 | CREATE INDEX t1a ON t1(a); |
| 43 | CREATE INDEX t1b ON t1(b); |
| 44 | PRAGMA journal_mode = wal; |
| 45 | |
| 46 | WITH ii(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM ii WHERE i<1000 ) |
| 47 | INSERT INTO t1 SELECT i, i FROM ii; |
| 48 | } {wal} |
| 49 | |
| 50 | foreach idelay { |
| 51 | 5 |
| 52 | 10 |
| 53 | 15 |
| 54 | 20 |
| 55 | } { |
| 56 | |
| 57 | set ::trigger_interrupt $idelay |
| 58 | do_catchsql_test 1.$idelay.1 { PRAGMA wal_checkpoint; } {1 interrupted} |
| 59 | do_execsql_test 1.$idelay.2 { SELECT count(*) FROM t1 } 1000 |
| 60 | |
| 61 | set ::trigger_interrupt $idelay |
| 62 | do_test 1.$idelay.3 { |
| 63 | list [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] $msg |
| 64 | } {1 {SQLITE_INTERRUPT - interrupted}} |
| 65 | do_execsql_test 1.$idelay.4 { SELECT count(*) FROM t1 } 1000 |
| 66 | } |
| 67 | |
| 68 | #------------------------------------------------------------------------- |
| 69 | # Check that if there are other SQL statements running, a checkpoint does |
| 70 | # not clear the isInterrupted flag. |
| 71 | # |
| 72 | do_execsql_test 2.0 { |
| 73 | CREATE TEMP TABLE z1(a, b); |
| 74 | INSERT INTO z1 SELECT * FROM t1; |
| 75 | } |
| 76 | |
| 77 | do_test 2.1 { |
| 78 | set i 10 |
| 79 | set res [list [catch { |
| 80 | set i 10 |
| 81 | db eval {SELECT * FROM z1} { |
| 82 | incr i -1 |
| 83 | if {$i==0} { |
| 84 | set ::trigger_interrupt 10 |
| 85 | set cres [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] |
| 86 | lappend cres $msg |
| 87 | } |
| 88 | } |
| 89 | } msg] $msg] |
| 90 | |
| 91 | list $cres $res |
| 92 | } {{1 {SQLITE_INTERRUPT - interrupted}} {1 interrupted}} |
| 93 | |
| 94 | do_execsql_test 2.0 { |
| 95 | SELECT count(*) FROM t1 |
| 96 | UNION ALL |
| 97 | SELECT count(*) FROM z1 |
| 98 | } {1000 1000} |
| 99 | |
| 100 | #------------------------------------------------------------------------- |
| 101 | # Check the effect of an interrupt during sqlite3_close(). |
| 102 | # |
| 103 | db_save_and_close |
| 104 | |
| 105 | db_restore_and_reopen |
| 106 | do_test 3.1.1 { |
| 107 | set ::trigger_interrupt 10 |
| 108 | db eval { SELECT * FROM sqlite_master } |
| 109 | db close |
| 110 | set {} {} |
| 111 | } {} |
| 112 | do_test 3.1.2 { |
| 113 | list [file exists test.db] [file exists test.db-wal] |
| 114 | } {1 1} |
| 115 | |
| 116 | db_restore_and_reopen |
| 117 | do_test 3.2.1 { |
| 118 | db eval { SELECT * FROM sqlite_master } |
| 119 | db close |
| 120 | set {} {} |
| 121 | } {} |
| 122 | do_test 3.2.2 { |
| 123 | list [file exists test.db] [file exists test.db-wal] |
| 124 | } {1 0} |
| 125 | |
| 126 | #------------------------------------------------------------------------- |
| 127 | # Check the effect of an interrupt during an automatic checkpoint |
| 128 | # |
| 129 | db_restore_and_reopen |
| 130 | do_test 4.0 { |
| 131 | execsql { PRAGMA wal_autocheckpoint = 10 } |
| 132 | set ::trigger_interrupt 10 |
| 133 | execsql { CREATE TABLE t2(x, y) } |
| 134 | } {} |
| 135 | |
| 136 | # The auto-checkpoint in test 4.0 should have been interrupted. So this |
| 137 | # db write should cause the wal file to grow. |
| 138 | do_test 4.1 { |
| 139 | set nFrame1 [wal_frame_count test.db-wal 1024] |
| 140 | execsql { CREATE TABLE t3(x, y) } |
| 141 | set nFrame2 [wal_frame_count test.db-wal 1024] |
| 142 | expr $nFrame2 > $nFrame1 |
| 143 | } {1} |
| 144 | |
| 145 | # The auto-checkpoint in test 4.0 should not have been interrupted. So |
| 146 | # this db write should not cause the wal file to grow. |
| 147 | do_test 4.2 { |
| 148 | set nFrame1 [wal_frame_count test.db-wal 1024] |
| 149 | execsql { CREATE TABLE t4(x, y) } |
| 150 | set nFrame2 [wal_frame_count test.db-wal 1024] |
| 151 | expr $nFrame2 == $nFrame1 |
| 152 | } {1} |
| 153 | |
| 154 | finish_test |