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