/ Check-in [b627e153]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Change the OOM and IO error test cases in walfault.test so that each test case runs both types of error simulation.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b627e1536822bb7e3ef91867661a53be0efc13ef
User & Date: dan 2010-06-01 17:46:38
Context
2010-06-01
19:15
Changes to the way faults are injected into xShmXXX VFS calls. check-in: 716d99f3 user: dan tags: trunk
17:46
Change the OOM and IO error test cases in walfault.test so that each test case runs both types of error simulation. check-in: b627e153 user: dan tags: trunk
15:44
Delay the decision to restart the log file until data is actually ready to be written to the log file (instead of at the start of a write transaction). check-in: b1abfaaf user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Show Whitespace Changes Patch

Changes to test/malloc_common.tcl.

18
19
20
21
22
23
24
25



































































































26
27
28
29
30
31
32
33
34
35
36
37













38




39
40
41
42

43
44
45
46
47

48
49
50
51
52
53
54
55
56
57



58
59
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74
75
76
77
78



79
80
81
82
83
84
85
#
ifcapable builtin_test {
  set MEMDEBUG 1
} else {
  set MEMDEBUG 0
  return 0
}




































































































# The following procs are used as [do_faultsim_test] when injecting OOM
# faults into test cases.
#
proc oom_injectstart {nRepeat iFail} {
  sqlite3_memdebug_fail $iFail -repeat $nRepeat
}
proc oom_injectstop {} {
  sqlite3_memdebug_fail -1
}

# This command is only useful when used by the -test script of a 
# [do_faultsim_test] test case.













#




proc faultsim_test_result {args} {
  upvar testrc testrc testresult testresult testnfail testnfail
  set t [list $testrc $testresult]
  set r [concat $args [list {1 {out of memory}}]]

  if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch $r $t]<0 } {
    error "nfail=$testnfail rc=$testrc result=$testresult"
  }
}


# Usage do_faultsim_test NAME ?OPTIONS...? 
#
# The first argument, <test number>, is used as a prefix of the test names
# taken by tests executed by this command. Options are as follows. All
# options take a single argument.
#
#     -injectstart      Script to enable fault-injection.
#
#     -injectstop       Script to disable fault-injection.
#



#     -prep             Script to execute before -body.
#
#     -body             Script to execute (with fault injection).
#
#     -test             Script to execute after -body.
#
proc do_faultsim_test {testname args} {

  set DEFAULT(-injectstart) {oom_injectstart 0}
  set DEFAULT(-injectstop)  {oom_injectstop}

  set DEFAULT(-prep)        ""
  set DEFAULT(-body)        ""
  set DEFAULT(-test)        ""

  array set O [array get DEFAULT]
  array set O $args
  foreach o [array names O] {
    if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
  }

  proc faultsim_test_proc {testrc testresult testnfail} $O(-test)




  set stop 0
  for {set iFail 1} {!$stop} {incr iFail} {

    # Evaluate the -prep script.
    #
    eval $O(-prep)








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|


<
>





>
|









>
>
>






|



>











>
>
>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#
ifcapable builtin_test {
  set MEMDEBUG 1
} else {
  set MEMDEBUG 0
  return 0
}


#--------------------------------------------------------------------------
# Usage do_faultsim_test NAME ?OPTIONS...? 
#
#     -faults           List of fault types to simulate.
#
#     -prep             Script to execute before -body.
#
#     -body             Script to execute (with fault injection).
#
#     -test             Script to execute after -body.
#
proc do_faultsim_test {name args} {
  set DEFAULT(-faults)        [list       \
    oom-transient     oom-persistent      \
    ioerr-transient   ioerr-persistent    \
  ]
  set DEFAULT(-prep)          ""
  set DEFAULT(-body)          ""
  set DEFAULT(-test)          ""

  array set O [array get DEFAULT]
  array set O $args
  foreach o [array names O] {
    if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
  }

  set A(oom-transient) [list                 \
    -injectstart   {oom_injectstart 0}       \
    -injectstop    oom_injectstop            \
    -injecterrlist {{1 {out of memory}}}     \
  ]
  set A(oom-persistent) [list                \
    -injectstart {oom_injectstart 1000000}   \
    -injectstop oom_injectstop               \
    -injecterrlist {{1 {out of memory}}}     \
  ]
  
  set A(ioerr-transient) [list               \
    -injectstart   {ioerr_injectstart 0}     \
    -injectstop    ioerr_injectstop          \
    -injecterrlist {{1 {disk I/O error}}}    \
  ]

  set A(ioerr-persistent) [list              \
    -injectstart   {ioerr_injectstart 1}     \
    -injectstop    ioerr_injectstop          \
    -injecterrlist {{1 {disk I/O error}}}    \
  ]

  foreach f $O(-faults) {
    if {[info exists A($f)]==0} { error "unknown fault: $f" }
  }
  set testspec [list -prep $O(-prep) -body $O(-body) -test $O(-test)]
  foreach f $O(-faults) {
    eval do_one_faultsim_test "$name-$f" $A($f) $testspec
  }
}

#-------------------------------------------------------------------------
# Procedures to save and restore the current file-system state:
#
#   faultsim_save_and_close
#   faultsim_restore_and_reopen
#
proc faultsim_save_and_close {} {
  foreach {a => b} {
      test.db          =>  testX.db 
      test.db-wal      =>  testX.db-wal 
      test.db-journal  =>  testX.db-journal
  } {
    if {[file exists $a]} {
      file copy -force $a $b
    } else {
      file delete -force $b
    }
  }
  catch { db close }
  return ""
}
proc faultsim_restore_and_reopen {} {
  catch { db close }
  foreach {a => b} {
      testX.db          =>  test.db 
      testX.db-wal      =>  test.db-wal 
      testX.db-journal  =>  test.db-journal
  } {
    if {[file exists $a]} {
      file copy -force $a $b
    } else {
      file delete -force $b
    }
  }
  sqlite3 db test.db
  sqlite3_extended_result_codes db 1
  sqlite3_db_config_lookaside db 0 0 0
}


# The following procs are used as [do_faultsim_test] when injecting OOM
# faults into test cases.
#
proc oom_injectstart {nRepeat iFail} {
  sqlite3_memdebug_fail $iFail -repeat $nRepeat
}
proc oom_injectstop {} {
  sqlite3_memdebug_fail -1
}



proc ioerr_injectstart {persist iFail} {
  set ::sqlite_io_error_persist $persist
  set ::sqlite_io_error_pending $iFail
}
proc ioerr_injectstop {} {
  set sv $::sqlite_io_error_hit
  set ::sqlite_io_error_persist 0
  set ::sqlite_io_error_pending 0
  set ::sqlite_io_error_hardhit 0
  set ::sqlite_io_error_hit     0
  set ::sqlite_io_error_pending 0
  return $sv
}

# This command is not called directly. It is used by the 
# [faultsim_test_result] command created by [do_faultsim_test] and used
# by -test scripts.
#
proc faultsim_test_result_int {args} {
  upvar testrc testrc testresult testresult testnfail testnfail
  set t [list $testrc $testresult]

  set r $args
  if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch $r $t]<0 } {
    error "nfail=$testnfail rc=$testrc result=$testresult"
  }
}

#--------------------------------------------------------------------------
# Usage do_one_faultsim_test NAME ?OPTIONS...? 
#
# The first argument, <test number>, is used as a prefix of the test names
# taken by tests executed by this command. Options are as follows. All
# options take a single argument.
#
#     -injectstart      Script to enable fault-injection.
#
#     -injectstop       Script to disable fault-injection.
#
#     -injecterrlist    List of generally acceptable test results (i.e. error
#                       messages). Example: [list {1 {out of memory}}]
#
#     -prep             Script to execute before -body.
#
#     -body             Script to execute (with fault injection).
#
#     -test             Script to execute after -body.
#
proc do_one_faultsim_test {testname args} {

  set DEFAULT(-injectstart)   {oom_injectstart 0}
  set DEFAULT(-injectstop)    {oom_injectstop}
  set DEFAULT(-injecterrlist) [list {1 {out of memory}}]
  set DEFAULT(-prep)          ""
  set DEFAULT(-body)          ""
  set DEFAULT(-test)          ""

  array set O [array get DEFAULT]
  array set O $args
  foreach o [array names O] {
    if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
  }

  proc faultsim_test_proc {testrc testresult testnfail} $O(-test)
  proc faultsim_test_result {args} "
    uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)]
  "

  set stop 0
  for {set iFail 1} {!$stop} {incr iFail} {

    # Evaluate the -prep script.
    #
    eval $O(-prep)

Changes to test/walfault.test.

15
16
17
18
19
20
21







22
23
24
25




26
27
28
29
30
31
32
..
45
46
47
48
49
50
51




52

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70



71
72
73


74
75

76


77








78


79
80
81
82







83

84
85
86
87


88
89
90
91
92
93
94

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl

ifcapable !wal {finish_test ; return }








do_faultsim_test walfault-oom-1 -prep {
  catch { db close }
  file delete -force test.db test.db-wal test.db-journal
  sqlite3 db test.db




} -body {
  db eval { PRAGMA main.journal_mode = WAL }
} -test {

  faultsim_test_result {0 wal}

  # Test that the connection that encountered an error as part of 
................................................................................
  set jm2 [db2 one {SELECT * FROM sqlite_master ; PRAGMA main.journal_mode}]
  db2 close

  if { $jm!=$jm2 } { error "Journal modes do not match: $jm $jm2" }
  if { $testrc==0 && $jm!="wal" } { error "Journal mode is not WAL" }
}





do_malloc_test walfault-oom-2 -tclprep {

  execsql {
    PRAGMA journal_mode = WAL;
    BEGIN;
      CREATE TABLE x(y, z, UNIQUE(y, z));
      INSERT INTO x VALUES(randomblob(100), randomblob(100));
    COMMIT;
    PRAGMA wal_checkpoint;

    INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
    INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
    INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
  }
  file copy -force test.db testX.db
  file copy -force test.db-wal testX.db-wal
  db close
  file rename -force testX.db test.db
  file rename -force testX.db-wal test.db-wal




  sqlite3 db test.db
  sqlite3_extended_result_codes db 1
  sqlite3_db_config_lookaside db 0 0 0


} -sqlbody {
  SELECT count(*) FROM x;

}











do_ioerr_test walfault-ioerr-1 -sqlprep {


  PRAGMA auto_vacuum = 1;
  PRAGMA journal_mode = WAL;
  CREATE TABLE abc(a PRIMARY KEY);
  INSERT INTO abc VALUES(randomblob(1500));







} -sqlbody {

  DELETE FROM abc;
  PRAGMA wal_checkpoint;
}
catch {db close}



# A [testvfs] callback for the VFS created by [do_shmfault_test]. This
# callback injects SQLITE_IOERR faults into methods for which an entry
# in array ::shmfault_ioerr_methods is defined. For example, to enable
# errors in xShmOpen:
#
#   set ::shmfault_ioerr_methods(xShmOpen) 1







>
>
>
>
>
>
>
|
|

<
>
>
>
>







 







>
>
>
>
|
>












|
|
<
<
<
|
>
>
>
|
|
|
>
>
|
|
>
|
>
>

>
>
>
>
>
>
>
>
|
>
>




>
>
>
>
>
>
>
|
>



|
>
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39
40
41
42
..
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl

ifcapable !wal {finish_test ; return }

#-------------------------------------------------------------------------
# This test case, walfault-1-*, simulates faults while executing a
#
#   PRAGMA journal_mode = WAL;
#
# statement immediately after creating a new database.
#
do_test walfault-1-pre-1 {
  db close
  file delete -force test.db test.db-wal test.db-journal

  faultsim_save_and_close
} {}
do_faultsim_test walfault-1 -prep {
  faultsim_restore_and_reopen
} -body {
  db eval { PRAGMA main.journal_mode = WAL }
} -test {

  faultsim_test_result {0 wal}

  # Test that the connection that encountered an error as part of 
................................................................................
  set jm2 [db2 one {SELECT * FROM sqlite_master ; PRAGMA main.journal_mode}]
  db2 close

  if { $jm!=$jm2 } { error "Journal modes do not match: $jm $jm2" }
  if { $testrc==0 && $jm!="wal" } { error "Journal mode is not WAL" }
}

#--------------------------------------------------------------------------
# Test case walfault-2-* tests fault injection during recovery of a 
# short WAL file (a dozen frames or thereabouts).
#
do_test walfault-2-pre-1 {
  sqlite3 db test.db
  execsql {
    PRAGMA journal_mode = WAL;
    BEGIN;
      CREATE TABLE x(y, z, UNIQUE(y, z));
      INSERT INTO x VALUES(randomblob(100), randomblob(100));
    COMMIT;
    PRAGMA wal_checkpoint;

    INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
    INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
    INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
  }
  execsql {
    SELECT count(*) FROM x



  }
} {8}
do_test walfault-2-pre-2 {
  faultsim_save_and_close
  faultsim_restore_and_reopen
  execsql { SELECT count(*) FROM x }
} {8}
do_faultsim_test walfault-2 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { SELECT count(*) FROM x }
} -test {

  # Test that all the rows in the WAL were recovered.
  faultsim_test_result {0 8}

  # Run the integrity_check to make sure nothing strange has occurred.
  set ic [db eval { PRAGMA integrity_check }]
  if {$ic != "ok"} { error "Integrity check: $ic" }
}

#--------------------------------------------------------------------------
# Test fault injection while writing and checkpointing a small WAL file.
#
do_test walfault-3-pre-1 {
  sqlite3 db test.db
  execsql {
    PRAGMA auto_vacuum = 1;
    PRAGMA journal_mode = WAL;
    CREATE TABLE abc(a PRIMARY KEY);
    INSERT INTO abc VALUES(randomblob(1500));
  }
  db close
  faultsim_save_and_close
} {}

do_faultsim_test walfault-3 -prep {
  faultsim_restore_and_reopen
} -body {
  db eval {
    DELETE FROM abc;
    PRAGMA wal_checkpoint;
  }
} -test {
  faultsim_test_result {0 {}}
}

# A [testvfs] callback for the VFS created by [do_shmfault_test]. This
# callback injects SQLITE_IOERR faults into methods for which an entry
# in array ::shmfault_ioerr_methods is defined. For example, to enable
# errors in xShmOpen:
#
#   set ::shmfault_ioerr_methods(xShmOpen) 1