xref: /sqlite-3.40.0/test/ioerr2.test (revision 8a29dfde)
1# 2007 April 2
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 for correct handling of I/O errors
13# such as writes failing because the disk is full.
14#
15# The tests in this file use special facilities that are only
16# available in the SQLite test fixture.
17#
18# $Id: ioerr2.test,v 1.7 2008/03/07 19:51:15 drh Exp $
19
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
23ifcapable !integrityck {
24  finish_test
25  return
26}
27
28do_test ioerr2-1.1 {
29  execsql {
30    PRAGMA cache_size = 10;
31    PRAGMA default_cache_size = 10;
32    CREATE TABLE t1(a, b, PRIMARY KEY(a, b));
33    INSERT INTO t1 VALUES(randstr(400,400),randstr(400,400));
34    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
35    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 4
36    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 8
37    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 16
38    INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 32
39  }
40} {}
41
42set ::cksum [execsql {SELECT md5sum(a, b) FROM t1}]
43proc check_db {testname} {
44
45  # Make sure no I/O errors are simulated in this proc.
46  set ::sqlite_io_error_hit 0
47  set ::sqlite_io_error_persist 0
48  set ::sqlite_io_error_pending 0
49
50  # Run an integrity-check. If "disk I/O error" is returned, the
51  # pager must be in error state. In this case open a new database
52  # connection. Otherwise, try a ROLLBACK, in case a transaction
53  # is still active.
54  set rc [catch {execsql {PRAGMA integrity_check}} msg]
55  if {$rc && ($msg eq "disk I/O error" || $msg eq "database is locked")} {
56    db close
57    sqlite3 db test.db
58    set refcnt 0
59  } else {
60    if {$rc || $msg ne "ok"} {
61      error $msg
62    }
63    catch {execsql ROLLBACK}
64  }
65
66  # Check that the database checksum is still $::cksum, and that
67  # the integrity-check passes.
68  set ck [execsql {SELECT md5sum(a, b) FROM t1}]
69  do_test ${testname}.cksum [list set ck $ck] $::cksum
70  integrity_check ${testname}.integrity
71  do_test ${testname}.refcnt {
72    lindex [sqlite3_pager_refcounts db] 0
73  } 0
74}
75
76check_db ioerr2-2
77
78set sql {
79  PRAGMA cache_size = 10;
80  PRAGMA default_cache_size = 10;
81  BEGIN;
82  DELETE FROM t1 WHERE (oid%7)==0;
83  INSERT INTO t1 SELECT randstr(400,400), randstr(400,400)
84    WHERE (random()%7)==0;
85  UPDATE t1 SET a = randstr(400,400), b = randstr(400,400)
86    WHERE (random()%7)==0;
87  ROLLBACK;
88}
89
90foreach bPersist [list 0 1] {
91  set ::go 1
92  for {set ::N 1} {$::go} {incr ::N} {
93    db close
94    sqlite3 db test.db
95    set ::sqlite_io_error_hit 0
96    set ::sqlite_io_error_persist $bPersist
97    set ::sqlite_io_error_pending $::N
98
99    foreach {::go res} [catchsql $sql] {}
100    check_db ioerr2-3.$bPersist.$::N
101  }
102}
103foreach bPersist [list 0 1] {
104  set ::go 1
105  for {set ::N 1} {$::go} {incr ::N} {
106    set ::sqlite_io_error_hit 0
107    set ::sqlite_io_error_persist $bPersist
108    set ::sqlite_io_error_pending $::N
109
110    foreach {::go res} [catchsql $sql] {}
111    check_db ioerr2-4.[expr {$bPersist+2}].$::N
112  }
113}
114
115finish_test
116