os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/ioerr2.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/ioerr2.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,135 @@
     1.4 +# 2007 April 2
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +# This file implements regression tests for SQLite library.  The
    1.15 +# focus of this file is testing for correct handling of I/O errors
    1.16 +# such as writes failing because the disk is full.
    1.17 +# 
    1.18 +# The tests in this file use special facilities that are only
    1.19 +# available in the SQLite test fixture.
    1.20 +#
    1.21 +# $Id: ioerr2.test,v 1.9 2008/08/20 14:49:25 danielk1977 Exp $
    1.22 +
    1.23 +set testdir [file dirname $argv0]
    1.24 +source $testdir/tester.tcl
    1.25 +
    1.26 +ifcapable !integrityck {
    1.27 +  finish_test
    1.28 +  return
    1.29 +}
    1.30 +
    1.31 +do_test ioerr2-1.1 {
    1.32 +  execsql {
    1.33 +    PRAGMA cache_size = 10;
    1.34 +    PRAGMA default_cache_size = 10;
    1.35 +    CREATE TABLE t1(a, b, PRIMARY KEY(a, b));
    1.36 +    INSERT INTO t1 VALUES(randstr(400,400),randstr(400,400));
    1.37 +    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
    1.38 +    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 4
    1.39 +    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 8
    1.40 +    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 16
    1.41 +    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 32
    1.42 +  }
    1.43 +} {}
    1.44 +
    1.45 +set ::cksum [execsql {SELECT md5sum(a, b) FROM t1}]
    1.46 +proc check_db {testname} {
    1.47 +
    1.48 +  # Make sure no I/O errors are simulated in this proc.
    1.49 +  set ::sqlite_io_error_hit 0
    1.50 +  set ::sqlite_io_error_persist 0
    1.51 +  set ::sqlite_io_error_pending 0
    1.52 +
    1.53 +  # Run an integrity-check. If "disk I/O error" is returned, the
    1.54 +  # pager must be in error state. In this case open a new database
    1.55 +  # connection. Otherwise, try a ROLLBACK, in case a transaction 
    1.56 +  # is still active.
    1.57 +  set rc [catch {execsql {PRAGMA integrity_check}} msg]
    1.58 +  if {$rc && ($msg eq "disk I/O error" || $msg eq "database is locked")} {
    1.59 +    db close
    1.60 +    sqlite3 db test.db
    1.61 +    set refcnt 0
    1.62 +  } else {
    1.63 +    if {$rc || $msg ne "ok"} {
    1.64 +      error $msg
    1.65 +    }
    1.66 +    catch {execsql ROLLBACK}
    1.67 +  }
    1.68 +
    1.69 +  # Check that the database checksum is still $::cksum, and that
    1.70 +  # the integrity-check passes.
    1.71 +  set ck [execsql {SELECT md5sum(a, b) FROM t1}]
    1.72 +  do_test ${testname}.cksum [list set ck $ck] $::cksum
    1.73 +  integrity_check ${testname}.integrity
    1.74 +  do_test ${testname}.refcnt {
    1.75 +    lindex [sqlite3_pager_refcounts db] 0
    1.76 +  } 0
    1.77 +}
    1.78 +
    1.79 +check_db ioerr2-2
    1.80 +
    1.81 +set sql {
    1.82 +  PRAGMA cache_size = 10;
    1.83 +  PRAGMA default_cache_size = 10;
    1.84 +  BEGIN;
    1.85 +  DELETE FROM t1 WHERE (oid%7)==0;
    1.86 +  INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) 
    1.87 +    WHERE (random()%7)==0;
    1.88 +  UPDATE t1 SET a = randstr(400,400), b = randstr(400,400) 
    1.89 +    WHERE (random()%7)==0;
    1.90 +  ROLLBACK;
    1.91 +}
    1.92 +
    1.93 +foreach bPersist [list 0 1] {
    1.94 +  set ::go 1
    1.95 +  for {set ::N 1} {$::go} {incr ::N} {
    1.96 +    db close
    1.97 +    sqlite3 db test.db
    1.98 +    set ::sqlite_io_error_hit 0
    1.99 +    set ::sqlite_io_error_persist $bPersist
   1.100 +    set ::sqlite_io_error_pending $::N
   1.101 +
   1.102 +    foreach {::go res} [catchsql $sql] {}
   1.103 +    check_db ioerr2-3.$bPersist.$::N
   1.104 +  }
   1.105 +}
   1.106 +foreach bPersist [list 0 1] {
   1.107 +  set ::go 1
   1.108 +  for {set ::N 1} {$::go} {incr ::N} {
   1.109 +    set ::sqlite_io_error_hit 0
   1.110 +    set ::sqlite_io_error_persist $bPersist
   1.111 +    set ::sqlite_io_error_pending $::N
   1.112 +
   1.113 +    foreach {::go res} [catchsql $sql] {}
   1.114 +    check_db ioerr2-4.[expr {$bPersist+2}].$::N
   1.115 +  }
   1.116 +}
   1.117 +
   1.118 +do_test ioerr2-5 {
   1.119 +  execsql {
   1.120 +    CREATE TABLE t2 AS SELECT * FROM t1;
   1.121 +    PRAGMA temp_store = memory;
   1.122 +  }
   1.123 +  set ::sqlite_io_error_persist 0
   1.124 +  set ::go 1
   1.125 +  set rc [catch {
   1.126 +    for {set ::N 2} {$::N<200} {incr ::N} {
   1.127 +      db eval {SELECT * FROM t1 WHERE rowid IN (1, 5, 10, 15, 20)} {
   1.128 +        set ::sqlite_io_error_hit 0
   1.129 +        set ::sqlite_io_error_pending $::N
   1.130 +        set sql {UPDATE t2 SET b = randstr(400,400)}
   1.131 +        foreach {::go res} [catchsql $sql] {}
   1.132 +      }
   1.133 +    }
   1.134 +  } msg]
   1.135 +  list $rc $msg
   1.136 +} {1 {callback requested query abort}}
   1.137 +
   1.138 +finish_test