File: indexfault.test

package info (click to toggle)
sqlcipher 3.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 70,888 kB
  • sloc: ansic: 195,357; tcl: 14,300; sh: 3,782; yacc: 1,245; makefile: 958; cs: 299; cpp: 128
file content (342 lines) | stat: -rw-r--r-- 10,637 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# 2011 August 08
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#

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

ifcapable !mergesort {
  finish_test
  return
}

set testprefix indexfault

# Set up the custom fault-injector. This is further configured by using
# different values for $::custom_filter and different implementations
# of Tcl proc [xCustom] for each test case.
#
proc install_custom_faultsim {} {
  set ::FAULTSIM(custom)            [list      \
    -injectinstall   custom_injectinstall    \
    -injectstart     custom_injectstart      \
    -injectstop      custom_injectstop       \
    -injecterrlist   {{1 {disk I/O error}}}  \
    -injectuninstall custom_injectuninstall  \
  ]
  proc custom_injectinstall {} {
    testvfs shmfault -default true
    shmfault filter $::custom_filter
    shmfault script xCustom
  }
  proc custom_injectuninstall {} {
    catch {db  close}
    catch {db2 close}
    shmfault delete
  }
  set ::custom_ifail -1
  set ::custom_nfail -1
  proc custom_injectstart {iFail} {
    set ::custom_ifail $iFail
    set ::custom_nfail 0
  }
  proc custom_injectstop {} {
    set ::custom_ifail -1
    return $::custom_nfail
  }
}
proc uninstall_custom_faultsim {} {
  unset -nocomplain ::FAULTSIM(custom)
}


#-------------------------------------------------------------------------
# These tests - indexfault-1.* - Build an index on a smallish table with
# all different kinds of fault-injection. The CREATE INDEX is run once
# with default options and once with a 50KB soft-heap-limit.
#
do_execsql_test 1.0 {
  BEGIN;
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(randomblob(202));
    INSERT INTO t1 SELECT randomblob(202) FROM t1;     --     2
    INSERT INTO t1 SELECT randomblob(202) FROM t1;     --     4
    INSERT INTO t1 SELECT randomblob(202) FROM t1;     --     8
    INSERT INTO t1 SELECT randomblob(202) FROM t1;     --    16
    INSERT INTO t1 SELECT randomblob(202) FROM t1;     --    32
    INSERT INTO t1 SELECT randomblob(202) FROM t1;     --    64
    INSERT INTO t1 SELECT randomblob(202) FROM t1;     --   128
    INSERT INTO t1 SELECT randomblob(202) FROM t1;     --   256
  COMMIT;
}
faultsim_save_and_close

do_faultsim_test 1.1 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { CREATE INDEX i1 ON t1(x) }
  faultsim_test_result {0 {}} 
  faultsim_integrity_check
}
ifcapable memorymanage {
  set soft_limit [sqlite3_soft_heap_limit 50000]
  do_faultsim_test 2.1 -prep {
    faultsim_restore_and_reopen
  } -body {
    execsql { CREATE INDEX i1 ON t1(x) }
    faultsim_test_result {0 {}} 
  }
  sqlite3_soft_heap_limit $soft_limit
}

#-------------------------------------------------------------------------
# These are similar to the indexfault-1.* tests, except they create an
# index with more than one column.
#
sqlite3 db test.db
do_execsql_test 2.0 {
  BEGIN;
    DROP TABLE IF EXISTS t1;
    CREATE TABLE t1(t,u,v,w,x,y,z);
    INSERT INTO t1 VALUES(
      randomblob(30), randomblob(30), randomblob(30), randomblob(30),
      randomblob(30), randomblob(30), randomblob(30)
    );
    INSERT INTO t1 SELECT 
      randomblob(30), randomblob(30), randomblob(30), randomblob(30),
      randomblob(30), randomblob(30), randomblob(30) FROM t1;         -- 2
    INSERT INTO t1 SELECT 
      randomblob(30), randomblob(30), randomblob(30), randomblob(30),
      randomblob(30), randomblob(30), randomblob(30) FROM t1;         -- 4
    INSERT INTO t1 SELECT 
      randomblob(30), randomblob(30), randomblob(30), randomblob(30),
      randomblob(30), randomblob(30), randomblob(30) FROM t1;         -- 8
    INSERT INTO t1 SELECT 
      randomblob(30), randomblob(30), randomblob(30), randomblob(30),
      randomblob(30), randomblob(30), randomblob(30) FROM t1;         -- 16
    INSERT INTO t1 SELECT 
      randomblob(30), randomblob(30), randomblob(30), randomblob(30),
      randomblob(30), randomblob(30), randomblob(30) FROM t1;         -- 32
    INSERT INTO t1 SELECT 
      randomblob(30), randomblob(30), randomblob(30), randomblob(30),
      randomblob(30), randomblob(30), randomblob(30) FROM t1;         -- 64
    INSERT INTO t1 SELECT 
      randomblob(30), randomblob(30), randomblob(30), randomblob(30),
      randomblob(30), randomblob(30), randomblob(30) FROM t1;         -- 128
  COMMIT;
}
faultsim_save_and_close

do_faultsim_test 2.1 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { CREATE INDEX i1 ON t1(t,u,v,w,x,y,z) }
  faultsim_test_result {0 {}} 
  faultsim_integrity_check
}
ifcapable memorymanage {
  set soft_limit [sqlite3_soft_heap_limit 50000]
  do_faultsim_test 2.2 -prep {
    faultsim_restore_and_reopen
  } -body {
    execsql { CREATE INDEX i1 ON t1(t,u,v,w,x,y,z) }
    faultsim_test_result {0 {}} 
  }
  sqlite3_soft_heap_limit $soft_limit
}

#-------------------------------------------------------------------------
# The following tests - indexfault-2.* - all attempt to build a index
# on table t1 in the main database with injected IO errors. Individual
# test cases work as follows:
#
#   3.1: IO errors injected into xOpen() calls.
#   3.2: As 7.1, but with a low (50KB) soft-heap-limit.
#
#   3.3: IO errors injected into the first 200 write() calls made on the
#        second temporary file.
#   3.4: As 7.3, but with a low (50KB) soft-heap-limit.
#
#   3.5: After a certain amount of data has been read from the main database
#        file (and written into the temporary b-tree), sqlite3_release_memory()
#        is called to free as much memory as possible. This causes the temp
#        b-tree to be flushed to disk. So that before its contents can be 
#        transfered to a PMA they must be read back from disk - creating extra
#        opportunities for IO errors.
#
install_custom_faultsim

# Set up a table to build indexes on. Save the setup using the 
# [faultsim_save_and_close] mechanism.
# 
sqlite3 db test.db
do_execsql_test 3.0 {
  BEGIN;
    DROP TABLE IF EXISTS t1;
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(randomblob(11000));
    INSERT INTO t1 SELECT randomblob(11001) FROM t1;     --     2
    INSERT INTO t1 SELECT randomblob(11002) FROM t1;     --     4
    INSERT INTO t1 SELECT randomblob(11003) FROM t1;     --     8
    INSERT INTO t1 SELECT randomblob(11004) FROM t1;     --    16
    INSERT INTO t1 SELECT randomblob(11005) FROM t1;     --    32
    INSERT INTO t1 SELECT randomblob(11006) FROM t1;     --    64
    INSERT INTO t1 SELECT randomblob(11007) FROM t1;     --   128
    INSERT INTO t1 SELECT randomblob(11008) FROM t1;     --   256
    INSERT INTO t1 SELECT randomblob(11009) FROM t1;     --   512
  COMMIT;
}
faultsim_save_and_close

set ::custom_filter xOpen
proc xCustom {args} {
  incr ::custom_ifail -1
  if {$::custom_ifail==0} {
    incr ::custom_nfail
    return "SQLITE_IOERR"
  }
  return "SQLITE_OK"
}
do_faultsim_test 3.1 -faults custom -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { CREATE INDEX i1 ON t1(x) }
  faultsim_test_result {0 {}} 
}
ifcapable memorymanage {
  set soft_limit [sqlite3_soft_heap_limit 50000]
  do_faultsim_test 3.2 -faults custom -prep {
    faultsim_restore_and_reopen
  } -body {
    execsql { CREATE INDEX i1 ON t1(x) }
    faultsim_test_result {0 {}} 
  }
  sqlite3_soft_heap_limit $soft_limit
}

set ::custom_filter {xOpen xWrite}
proc xCustom {method args} {
  if {$method == "xOpen"} {
    if {[lindex $args 0] == ""} {
      incr ::nTmpOpen 1
      if {$::nTmpOpen == 3} { return "failme" }
    }
    return "SQLITE_OK"
  }
  if {$::custom_ifail<200 && [lindex $args 1] == "failme"} {
    incr ::custom_ifail -1
    if {$::custom_ifail==0} {
      incr ::custom_nfail
      return "SQLITE_IOERR"
    }
  }
  return "SQLITE_OK"
}

do_faultsim_test 3.3 -faults custom -prep {
  faultsim_restore_and_reopen
  set ::nTmpOpen 0
} -body {
  execsql { CREATE INDEX i1 ON t1(x) }
  faultsim_test_result {0 {}} 
}

ifcapable memorymanage {
  set soft_limit [sqlite3_soft_heap_limit 50000]
  do_faultsim_test 3.4 -faults custom -prep {
    faultsim_restore_and_reopen
    set ::nTmpOpen 0
  } -body {
    execsql { CREATE INDEX i1 ON t1(x) }
    faultsim_test_result {0 {}} 
  }
  sqlite3_soft_heap_limit $soft_limit
}

uninstall_custom_faultsim

#-------------------------------------------------------------------------
# Test 4: After a certain amount of data has been read from the main database
# file (and written into the temporary b-tree), sqlite3_release_memory() is
# called to free as much memory as possible. This causes the temp b-tree to be
# flushed to disk. So that before its contents can be transfered to a PMA they
# must be read back from disk - creating extra opportunities for IO errors.
# 
install_custom_faultsim

catch { db close }
forcedelete test.db
sqlite3 db test.db

do_execsql_test 4.0 {
  BEGIN;
    DROP TABLE IF EXISTS t1;
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(randomblob(11000));
    INSERT INTO t1 SELECT randomblob(11001) FROM t1;     --     2
    INSERT INTO t1 SELECT randomblob(11002) FROM t1;     --     4
    INSERT INTO t1 SELECT randomblob(11003) FROM t1;     --     8
    INSERT INTO t1 SELECT randomblob(11004) FROM t1;     --    16
    INSERT INTO t1 SELECT randomblob(11005) FROM t1;     --    32
    INSERT INTO t1 SELECT randomblob(11005) FROM t1;     --    64
  COMMIT;
}
faultsim_save_and_close

testvfs tvfs 
tvfs script xRead
tvfs filter xRead
set ::nRead 0
proc xRead {method file args} {
  if {[file tail $file] == "test.db"} { incr ::nRead }
}

do_test 4.1 {
  sqlite3 db test.db -vfs tvfs
  execsql { CREATE INDEX i1 ON t1(x) }
} {}

db close
tvfs delete

set ::custom_filter xRead
proc xCustom {method file args} {
  incr ::nReadCall
  if {$::nReadCall >= ($::nRead/5)} {
    if {$::nReadCall == ($::nRead/5)} {
      set nByte [sqlite3_release_memory [expr 64*1024*1024]]
      sqlite3_soft_heap_limit 20000
    }
    if {$file == ""} {
      incr ::custom_ifail -1
      if {$::custom_ifail==0} {
        incr ::custom_nfail
        return "SQLITE_IOERR"
      }
    }
  }
  return "SQLITE_OK"
}

do_faultsim_test 4.2 -faults custom -prep {
  faultsim_restore_and_reopen
  set ::nReadCall 0
  sqlite3_soft_heap_limit 0
} -body {
  execsql { CREATE INDEX i1 ON t1(x) }
  faultsim_test_result {0 {}} 
}

uninstall_custom_faultsim

finish_test