sl@0
|
1 |
# 2008 January 8
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
#
|
sl@0
|
12 |
# This file contains additional tests to verify that SQLite database
|
sl@0
|
13 |
# file survive a power loss or OS crash.
|
sl@0
|
14 |
#
|
sl@0
|
15 |
# $Id: crash4.test,v 1.3 2008/01/16 17:46:38 drh Exp $
|
sl@0
|
16 |
|
sl@0
|
17 |
set testdir [file dirname $argv0]
|
sl@0
|
18 |
source $testdir/tester.tcl
|
sl@0
|
19 |
|
sl@0
|
20 |
ifcapable !crashtest {
|
sl@0
|
21 |
finish_test
|
sl@0
|
22 |
return
|
sl@0
|
23 |
}
|
sl@0
|
24 |
|
sl@0
|
25 |
|
sl@0
|
26 |
# A sequence of SQL commands:
|
sl@0
|
27 |
#
|
sl@0
|
28 |
set sql_cmd_list {
|
sl@0
|
29 |
{CREATE TABLE a(id INTEGER, name CHAR(50))}
|
sl@0
|
30 |
{INSERT INTO a(id,name) VALUES(1,'one')}
|
sl@0
|
31 |
{INSERT INTO a(id,name) VALUES(2,'two')}
|
sl@0
|
32 |
{INSERT INTO a(id,name) VALUES(3,'three')}
|
sl@0
|
33 |
{INSERT INTO a(id,name) VALUES(4,'four')}
|
sl@0
|
34 |
{INSERT INTO a(id,name) VALUES(5,'five')}
|
sl@0
|
35 |
{INSERT INTO a(id,name) VALUES(6,'six')}
|
sl@0
|
36 |
{INSERT INTO a(id,name) VALUES(7,'seven')}
|
sl@0
|
37 |
{INSERT INTO a(id,name) VALUES(8,'eight')}
|
sl@0
|
38 |
{INSERT INTO a(id,name) VALUES(9,'nine')}
|
sl@0
|
39 |
{INSERT INTO a(id,name) VALUES(10,'ten')}
|
sl@0
|
40 |
{UPDATE A SET name='new text for row 3' WHERE id=3}
|
sl@0
|
41 |
}
|
sl@0
|
42 |
|
sl@0
|
43 |
# Assume that a database is created by evaluating the SQL statements
|
sl@0
|
44 |
# in $sql_cmd_list. Compute a set of checksums that capture the state
|
sl@0
|
45 |
# of the database after each statement. Also include a checksum for
|
sl@0
|
46 |
# the state of the database prior to any of these statements.
|
sl@0
|
47 |
#
|
sl@0
|
48 |
set crash4_cksum_set {}
|
sl@0
|
49 |
lappend crash4_cksum_set [allcksum db]
|
sl@0
|
50 |
foreach cmd $sql_cmd_list {
|
sl@0
|
51 |
db eval $cmd
|
sl@0
|
52 |
lappend crash4_cksum_set [allcksum db]
|
sl@0
|
53 |
}
|
sl@0
|
54 |
|
sl@0
|
55 |
# Run the sequence of SQL statements shown above repeatedly.
|
sl@0
|
56 |
# Close and reopen the database right before the UPDATE statement.
|
sl@0
|
57 |
# On each repetition, introduce database corruption typical of
|
sl@0
|
58 |
# what might be seen in a power loss or OS crash.
|
sl@0
|
59 |
#
|
sl@0
|
60 |
# Slowly increase the delay before the crash, repeating the test
|
sl@0
|
61 |
# over and over. Stop testing when the entire sequence of SQL
|
sl@0
|
62 |
# statements runs to completing without hitting the crash.
|
sl@0
|
63 |
#
|
sl@0
|
64 |
for {set cnt 1; set fin 0} {!$fin} {incr cnt} {
|
sl@0
|
65 |
db close
|
sl@0
|
66 |
file delete -force test.db test.db-journal
|
sl@0
|
67 |
do_test crash4-1.$cnt.1 {
|
sl@0
|
68 |
set seed [expr {int(abs(rand()*10000))}]
|
sl@0
|
69 |
set delay [expr {int($cnt/50)+1}]
|
sl@0
|
70 |
set file [expr {($cnt&1)?"test.db":"test.db-journal"}]
|
sl@0
|
71 |
set c [crashsql -delay $delay -file $file -seed $seed -tclbody {
|
sl@0
|
72 |
db eval {CREATE TABLE a(id INTEGER, name CHAR(50))}
|
sl@0
|
73 |
db eval {INSERT INTO a(id,name) VALUES(1,'one')}
|
sl@0
|
74 |
db eval {INSERT INTO a(id,name) VALUES(2,'two')}
|
sl@0
|
75 |
db eval {INSERT INTO a(id,name) VALUES(3,'three')}
|
sl@0
|
76 |
db eval {INSERT INTO a(id,name) VALUES(4,'four')}
|
sl@0
|
77 |
db eval {INSERT INTO a(id,name) VALUES(5,'five')}
|
sl@0
|
78 |
db eval {INSERT INTO a(id,name) VALUES(6,'six')}
|
sl@0
|
79 |
db eval {INSERT INTO a(id,name) VALUES(7,'seven')}
|
sl@0
|
80 |
db eval {INSERT INTO a(id,name) VALUES(8,'eight')}
|
sl@0
|
81 |
db eval {INSERT INTO a(id,name) VALUES(9,'nine')}
|
sl@0
|
82 |
db eval {INSERT INTO a(id,name) VALUES(10,'ten')}
|
sl@0
|
83 |
db close
|
sl@0
|
84 |
sqlite3 db test.db
|
sl@0
|
85 |
db eval {UPDATE A SET name='new text for row 3' WHERE id=3}
|
sl@0
|
86 |
db close
|
sl@0
|
87 |
} {}]
|
sl@0
|
88 |
if {$c==[list 0 {}]} {
|
sl@0
|
89 |
set ::fin 1
|
sl@0
|
90 |
set c [list 1 {child process exited abnormally}]
|
sl@0
|
91 |
}
|
sl@0
|
92 |
set c
|
sl@0
|
93 |
} {1 {child process exited abnormally}}
|
sl@0
|
94 |
sqlite3 db test.db
|
sl@0
|
95 |
integrity_check crash4-1.$cnt.2
|
sl@0
|
96 |
do_test crash4-1.$cnt.3 {
|
sl@0
|
97 |
set x [lsearch $::crash4_cksum_set [allcksum db]]
|
sl@0
|
98 |
expr {$x>=0}
|
sl@0
|
99 |
} {1}
|
sl@0
|
100 |
}
|
sl@0
|
101 |
|
sl@0
|
102 |
finish_test
|