xref: /sqlite-3.40.0/test/cffault.test (revision 45f31be8)
1# 2011 November 16
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#
12# This file contains fault-injection test cases for the
13# sqlite3_db_cacheflush API.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18set testprefix cacheflush
19source $testdir/malloc_common.tcl
20
21# Run the supplied SQL on a copy of the database currently stored on
22# disk in file $dbfile.
23proc diskquery {dbfile sql} {
24  forcecopy $dbfile dq.db
25  sqlite3 dq dq.db
26  set res [execsql $sql dq]
27  dq close
28  set res
29}
30
31do_execsql_test 1.0 {
32  CREATE TABLE t1(a PRIMARY KEY, b);
33  CREATE INDEX i1 ON t1(b);
34  INSERT INTO t1 VALUES(1, 2);
35  INSERT INTO t1 VALUES(3, 4);
36  INSERT INTO t1 VALUES(5, 6);
37  INSERT INTO t1 VALUES(7, 8);
38}
39faultsim_save_and_close
40
41do_faultsim_test 1.1 -prep {
42  faultsim_restore_and_reopen
43  db eval {
44    BEGIN;
45      UPDATE t1 SET b=b+1;
46  }
47} -body {
48  sqlite3_db_cacheflush db
49} -test {
50  if {[sqlite3_get_autocommit db]} { error "Transaction rolled back!" }
51  faultsim_test_result {0 {}} {1 {disk I/O error}}
52  catch { db eval COMMIT }
53  faultsim_integrity_check
54}
55
56do_faultsim_test 1.2 -prep {
57  faultsim_restore_and_reopen
58  db eval {
59    BEGIN;
60      UPDATE t1 SET b=b+1;
61  }
62} -body {
63  set result [list]
64  db eval { SELECT * FROM t1 } {
65    if {$a==5} { catch { sqlite3_db_cacheflush db } }
66    lappend result $a $b
67  }
68  set result
69} -test {
70  faultsim_test_result {0 {1 3 3 5 5 7 7 9}} {1 {disk I/O error}}
71  catch { db eval COMMIT }
72  faultsim_integrity_check
73}
74
75#-------------------------------------------------------------------------
76reset_db
77do_execsql_test 2.0 {
78  CREATE TABLE t1(a PRIMARY KEY, b, c);
79  CREATE INDEX i1 ON t1(b);
80  CREATE INDEX i2 ON t1(c, b);
81  INSERT INTO t1 VALUES(1, 2,  randomblob(600));
82  INSERT INTO t1 VALUES(3, 4,  randomblob(600));
83  INSERT INTO t1 VALUES(5, 6,  randomblob(600));
84  INSERT INTO t1 VALUES(7, 8,  randomblob(600));
85  INSERT INTO t1 VALUES(9, 10, randomblob(600));
86}
87faultsim_save_and_close
88
89do_faultsim_test 2.1 -prep {
90  faultsim_restore_and_reopen
91  db eval {
92    BEGIN;
93      UPDATE t1 SET b=b+1;
94  }
95} -body {
96  set result [list]
97  db eval { SELECT * FROM t1 } {
98    if {$a==5} { catch { sqlite3_db_cacheflush db } }
99    lappend result $a $b
100  }
101  set result
102} -test {
103  faultsim_test_result {0 {1 3 3 5 5 7 7 9 9 11}} {1 {disk I/O error}}
104  catch { db eval { INSERT INTO t1 VALUES(11, 12, randomblob(600)) } }
105  catch { db eval COMMIT }
106  faultsim_integrity_check
107}
108
109do_faultsim_test 2.2 -prep {
110  faultsim_restore_and_reopen
111  db eval {
112    BEGIN;
113      UPDATE t1 SET b=b+1;
114  }
115} -body {
116  sqlite3_db_cacheflush db
117} -test {
118  if {[sqlite3_get_autocommit db]} { error "Transaction rolled back!" }
119  faultsim_test_result {0 {}} {1 {disk I/O error}}
120  catch { db eval { SELECT * FROM t1 } }
121  catch { db eval COMMIT }
122  faultsim_integrity_check
123}
124
125do_faultsim_test 2.3 -prep {
126  faultsim_restore_and_reopen
127  db eval {
128    BEGIN;
129      UPDATE t1 SET b=b-1;
130  }
131} -body {
132  sqlite3_db_cacheflush db
133} -test {
134  if {[sqlite3_get_autocommit db]} { error "Transaction rolled back!" }
135  faultsim_test_result {0 {}} {1 {disk I/O error}}
136  catch { db eval { INSERT INTO t1 VALUES(11, 12, randomblob(600)) } }
137  catch { db eval COMMIT }
138  faultsim_integrity_check
139}
140
141do_faultsim_test 2.4 -prep {
142  faultsim_restore_and_reopen
143  db eval {
144    BEGIN;
145      UPDATE t1 SET b=b-1;
146  }
147} -body {
148  catch { sqlite3_db_cacheflush db }
149  catch { sqlite3_db_release_memory db }
150  catch { sqlite3_db_cacheflush db }
151  execsql { SELECT a, b FROM t1 }
152} -test {
153  faultsim_test_result {0 {1 1 3 3 5 5 7 7 9 9}} {1 {disk I/O error}}
154  catchsql ROLLBACK
155  faultsim_integrity_check
156}
157
158finish_test
159