Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
interrupt2.test160 linesDownload Raw Back to test
1# 2016 Aug 122#3# The author disclaims copyright to this source code.  In place of4# 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.  The12# focus of this script is using the sqlite_interrupt() API to 13# interrupt WAL checkpoint operations.14#15 16set testdir [file dirname $argv0]17source $testdir/tester.tcl18source $testdir/wal_common.tcl19set testprefix interrupt220 21if {[permutation]=="journaltest" || [permutation]=="inmemory_journal"} {22  finish_test23  return24}25 26db close27testvfs tvfs -default 128 29tvfs filter xWrite30tvfs script write_cb31 32set ::trigger_interrupt 033proc write_cb {method args} {34  set filename [lindex $args 0]35  if {[file tail $filename]=="test.db" && $::trigger_interrupt} {36    if {$::trigger_interrupt} {37      incr ::trigger_interrupt -138      if {$::trigger_interrupt==0} { sqlite3_interrupt db }39    }40  }41  return 042}43 44sqlite3 db test.db 45do_execsql_test 1.0 {46  CREATE TABLE t1(a, b);47  CREATE INDEX t1a ON t1(a);48  CREATE INDEX t1b ON t1(b);49  PRAGMA journal_mode = wal;50 51  WITH ii(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM ii WHERE i<1000 )52  INSERT INTO t1 SELECT i, i FROM ii;53} {wal}54 55foreach idelay {56  557  1058  1559  2060} {61 62  set ::trigger_interrupt $idelay63  do_catchsql_test 1.$idelay.1 { PRAGMA wal_checkpoint; } {1 interrupted}64  do_execsql_test  1.$idelay.2 { SELECT count(*) FROM t1 } 100065 66  set ::trigger_interrupt $idelay67  do_test 1.$idelay.3 { 68    list [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] $msg69  } {1 {SQLITE_INTERRUPT - interrupted}}70  do_execsql_test  1.$idelay.4 { SELECT count(*) FROM t1 } 100071}72 73#-------------------------------------------------------------------------74# Check that if there are other SQL statements running, a checkpoint does75# not clear the isInterrupted flag.76#77do_execsql_test 2.0 {78  CREATE TEMP TABLE z1(a, b);79  INSERT INTO z1 SELECT * FROM t1;80}81 82do_test 2.1 {83  set i 1084  set res [list [catch {85    set i 1086    db eval {SELECT * FROM z1} {87      incr i -188      if {$i==0} {89        set ::trigger_interrupt 1090        set cres [catch { sqlite3_wal_checkpoint_v2 db truncate } msg] 91        lappend cres $msg92      }93    }94  } msg] $msg]95 96  list $cres $res97} {{1 {SQLITE_INTERRUPT - interrupted}} {1 interrupted}}98 99do_execsql_test 2.0 {100  SELECT count(*) FROM t1101  UNION ALL102  SELECT count(*) FROM z1103} {1000 1000}104 105#-------------------------------------------------------------------------106# Check the effect of an interrupt during sqlite3_close().107#108db_save_and_close109 110db_restore_and_reopen111do_test 3.1.1 {112  set ::trigger_interrupt 10113  db eval { SELECT * FROM sqlite_master }114  db close115  set {} {}116} {}117do_test 3.1.2 {118  list [file exists test.db] [file exists test.db-wal]119} {1 1}120 121db_restore_and_reopen122do_test 3.2.1 {123  db eval { SELECT * FROM sqlite_master }124  db close125  set {} {}126} {}127do_test 3.2.2 {128  list [file exists test.db] [file exists test.db-wal]129} {1 0}130 131#-------------------------------------------------------------------------132# Check the effect of an interrupt during an automatic checkpoint133#134db_restore_and_reopen135do_test 4.0 { 136  execsql { PRAGMA wal_autocheckpoint = 10 }137  set ::trigger_interrupt 10138  execsql { CREATE TABLE t2(x, y) }139} {}140 141# The auto-checkpoint in test 4.0 should have been interrupted. So this142# db write should cause the wal file to grow.143do_test 4.1 {144  set nFrame1 [wal_frame_count test.db-wal 1024]145  execsql { CREATE TABLE t3(x, y) }146  set nFrame2 [wal_frame_count test.db-wal 1024]147  expr $nFrame2 > $nFrame1148} {1}149 150# The auto-checkpoint in test 4.0 should not have been interrupted. So 151# this db write should not cause the wal file to grow.152do_test 4.2 {153  set nFrame1 [wal_frame_count test.db-wal 1024]154  execsql { CREATE TABLE t4(x, y) }155  set nFrame2 [wal_frame_count test.db-wal 1024]156  expr $nFrame2 == $nFrame1157} {1}158 159finish_test160