xref: /sqlite-3.40.0/test/ioerr2.test (revision 8d96eb0f)
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.1 2007/04/02 16:46:23 danielk1977 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  } else {
54    if {$rc} {error $msg}
55    catch {execsql ROLLBACK}
56  }
57
58  # Check that the database checksum is still $::cksum, and that
59  # the integrity-check passes.
60  set ck [execsql {SELECT md5sum(a, b) FROM t1}]
61  do_test ${testname}.cksum [list set ck $ck] $::cksum
62  integrity_check ${testname}.integrity
63}
64
65check_db ioerr2-2
66
67set sql {
68  PRAGMA cache_size = 10;
69  PRAGMA default_cache_size = 10;
70  BEGIN;
71  DELETE FROM t1 WHERE (oid%7)==0;
72  INSERT INTO t1 SELECT randstr(400,400), randstr(400,400)
73    WHERE (random()%7)==0;
74  UPDATE t1 SET a = randstr(400,400), b = randstr(400,400)
75    WHERE (random()%7)==0;
76  ROLLBACK;
77}
78
79foreach bPersist [list 0 1] {
80  set ::go 1
81  for {set ::N 1} {$::go} {incr ::N} {
82    set ::sqlite_io_error_hit 0
83    set ::sqlite_io_error_persist $bPersist
84    set ::sqlite_io_error_pending $::N
85
86    foreach {::go res} [catchsql $sql] {}
87    check_db ioerr2-3.$bPersist.$::N
88  }
89}
90
91finish_test
92